1. 程式人生 > >git 刪除 本地分支和遠端分支 出現的問題

git 刪除 本地分支和遠端分支 出現的問題

將分支合併到 master 後,我們需要刪除無用分支,本地刪除比較簡單:
	git branch -d/-D xxx

但是,有些分支,我們可能也是多人開發,推送到了遠端測試伺服器上,我們也需要刪除遠端的廢棄分支:
	1.先在本地刪除分支
		git branch -d xxx

	2.推送給遠端倉庫
		git push test :xxx		// 切記這個語法。
	/*
	    如果不再需要某個遠端分支了,比如搞定了某個特性並把它合併進了遠端的 master 分支(或任何其他存放穩定程式碼的地方),可以用這個非常無厘頭的語法來刪除它:
	    	git push  [遠端名] :[分支名]
    	如果想在伺服器上刪除 serverfix 分支,執行下面的命令:
			git push origin :serverfix
		會輸出:
			To 
[email protected]
:schacon/simplegit.git - [deleted] serverfix 咚!伺服器上的分支沒了。你最好特別留心這一頁,因為你一定會用到那個命令,而且你很可能會忘掉它的語法。有種方便記憶這條命令的方法: 記住我們不久前見過的 git push [遠端名] [本地分支]:[遠端分支] 語法,如果省略 [本地分支],那就等於是在說“在這裡提取空白然後把它變成[遠端分支]”。 */ /* 我執行刪除的時候,報瞭如下錯誤: remote: error: By default, deleting the current branch is denied, because the next remote: error: 'git clone' won't result in any file checked out, causing confusion. remote: error: remote: error: You can set 'receive.denyDeleteCurrent' configuration variable to remote: error: 'warn' or 'ignore' in the remote repository to allow deleting the remote: error: current branch, with or without a warning message. remote: error: remote: error: To squelch this message, you can set it to 'refuse'. remote: error: refusing to delete the current branch: refs/heads/LFF To xxx.xxx.xxx.xxx:yyy.git ! [remote rejected] LFF (deletion of the current branch prohibited) 看著錯誤提示,應該是需要我們在遠端 git 上設定 'receive.denyDeleteCurrent' 為 'warn' 或 'ignore' 來開啟刪除 // git 進行全域性設定 git config --global receive.denyDeleteCurrent warn // 再次刪除遠端分支 git push test :xxx 刪除成功!!! 又出現了一個問題,我這樣操作沒有問題了,本地和遠端的分支都被刪除了,但是不知道為啥,同事的 sourcetree 裡,一直會展示一個 'refs/remotes/origin/xxx',看著像是本地對遠端的引用: 參考文章: https://www.jianshu.com/p/884ff6252be5 執行下面命令: git remote prune origin 對 git 不熟悉,好多東西不瞭解! */ /* 這裡再記錄下,git 新增配置的一個筆記,只能說明對 git 太不熟悉了: 上面想新增 git 全域性配置,想著查詢下 git 的配置檔案,完全不知道哪裡有...不知道 linux 下 git 的配置目錄在哪裡,通過命令: find / -name '[A-Za-z]*git[A-Za-z]*' // 全域性搜尋,也沒找到 // 1.find 命令應該都是使用的 glob 萬用字元(這裡也忘記的差不多了...上面的 glob 寫的不太對) // 2.find 也支援正則,可能是 perl 正則 find / -regex '.*git.*' 而且 git config -l 檢視所有的 git 配置,居然啥也沒,導致我一度懷疑是不是用錯了~~ 在網上搜索了下 linux 的配置檔案路徑: 1./etc/gitconfig 2.~/.gitconfig 3.不同專案下的 .git/config git 官方文件也有說明,我居然沒有去了解... https://git-scm.com/book/zh/v1/%E8%87%AA%E5%AE%9A%E4%B9%89-Git-%E9%85%8D%E7%BD%AE-Git 我是自己測出來的,輸入 git config,會輸出 git config 的用法: 配置檔案位置 --global 使用全域性配置檔案 --system 使用系統級配置檔案 --local 使用版本庫級配置檔案 -f, --file <檔案> 使用指定的配置檔案 我們一般使用的全域性配置時,會新增 '--global' 引數,然後就依次嘗試了下,設定 user.name 這個配置項: git config --global user.name dongxuemin // 在 ~/ 下建立了 .gitconfig git config --system user.name dongxuemin // 在 /etc/ 下建立了 gitconfig git config --local 我沒測試 還是得好好熟悉 git 啊,不過慢慢發現問題,尋找問題,就熟悉了 */