在 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 机器中被删除。