在 Git 中修剪遠端分支
Stewart Nguyen
2022年4月22日
本文將展示如何修剪(清理)在遠端倉庫中刪除的遠端跟蹤分支。
例如,Alice 和 Bob 正在開發一個分支 feature/shared-branch
。Bob 建立一個拉取請求,合併 feature/shared-branch
,然後將其刪除。
她在 Alice 這邊執行 git pull origin feature/shared-branch
。
$ git branch -a
* feature/shared-branch
main
remotes/origin/feature/shared-branch
remotes/origin/main
$ git pull origin feature/shared-branch
fatal: couldn't find remote ref feature/shared-branch
儘管 remotes/origin/feature/shared-branch
出現在 git branch -a
下,執行 git pull origin feature/shared-branch
仍然會觸發錯誤,因為 feature/shared-branch
已被刪除遠端倉庫。
為了克服這個問題,Alice 應該清理 feature/shared-branch
的引用,它是 remotes/origin/feature/shared-branch
。她可能會執行 git remote prune origin
。
$ git remote prune origin
Pruning origin
URL: git@github.com:stwarts/git-demo.git
* [pruned] origin/feature/shared-branch
git remote prune origin
執行檢查。遠端倉庫中不存在的遠端跟蹤分支將被刪除。
feature/shared-branch
已被 Bob 刪除。在 Alice 執行 git remote prune origin
後,它的遠端跟蹤分支 remotes/origin/feature/shared-branch
將在 Alice 機器中被刪除。