1. 程式人生 > >本地刪除無效的遠端分支

本地刪除無效的遠端分支

hi all:
如何讓已經失效的遠端 branch-name 在執行 git branch -a 的時候不再展示?

有同事push 了一個git分支,並且被我給pull 到本地了

同事在自己機器上執行了
git branch -d branch-name
git push origin :branch-name

我在自己機器執行
git branch -d branch-name //ok
git push origin :branch-name //提示:

error: unable to delete 'branch-name': remote ref does not exist
error: 無法推送一些引用到 '

[email protected]:phplib'

大家幫忙想想辦法讓已經失效的遠端 branch-name 在執行 git branch -a 的時候不再展示吧?謝謝

reply:

git remote prune origin
清理遠端分支,把本地不存在的遠端分支刪除

刪除了遠端的master分支後,客戶機再pull程式碼可能會有問題,提示預設的分支指向了不存在的ref。可參考下面的辦法解決:


刪除一個遠端分支時出現錯誤提示:

1 $ git push --delete origin foobar

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/foobar
To cisvr:~/reis.git
! [remote rejected] foobar (deletion of the current branch prohibited)
error: failed to push some refs to 'cisvr:~/reis.git'

也就是foobar是遠端倉庫的當前分支(由於使用git clone --bare生成裸倉庫造成的),因為遠端倉庫是裸倉庫,所以不能使用普通的git checkout命令切換分支。在裸倉庫中使用如下命令來切換當前分支:

1 $ git symbolic-ref HEAD refs/heads/devel

這樣就將裸倉庫的當前分支切換為devel分支,刪除foobar分支就沒問題了。

1 $ git push origin :foobar

這個命令實質上是修改了.git/HEAD檔案,使其內容為:

1 ref: refs/heads/devel