mar*_*kas 3 git version-control branch github git-branch
我有一个本地和远程Git存储库.
在我的本地存储库中,我创建并切换到新分支: git checkout -b feature/niceNewFeature
我做我的编码git add .,git commit和git push我的feature/niceNewFeature分支远程存储库(如GitHub上).
在此之后,我创建了一个GitHub Pull Request来合并feature/niceNewFeature分支master- 在审查代码之后,我将这样做.
由于feature/niceNewFeature现在合并到master,我将在GitHub上删除它.
但是,在我的本地存储库中,如果执行git branch命令,仍会列出feature/niceNewFeature分支.
如何feature/niceNewFeature从本地存储库中删除分支,因为它已经与master远程存储库合并和删除?
mar*_*kas 10
git fetch --prune
git branch -lvv | cut -c3- | awk '/: gone]/ {print $1}' | xargs git branch -d
首先,删除远程不再存在的任何远程跟踪引用:
git fetch --prune(或git fetch -p)
如果你这样做,然后你运行git branch -vv(详细),你会看到虽然feature/niceNewFeature仍然列出了分支,但上游分支现在"已经消失":
feature/niceNewFeature 832f7a7 [origin/feature/niceNewFeature: gone] Some commit message.
Run Code Online (Sandbox Code Playgroud)
其次,让我们使用以下命令删除本地分支:
git branch -lvv | cut -c3- | awk '/: gone]/ {print $1}' | xargs git branch -d
它的作用基本上如下:
-l详细模式(vv)中的所有local()分支(这几乎与我们以前的输出一样)cut是每行开头的第一个和第二个字符(它们是空格)awk它过滤"消失的"(/: gone]/),得到它们的第一列({print $1}).请记住,"消失"意味着远程跟踪参考不再存在.feature/niceNewFeature在我们的例子中是要删除的分支的名称git branch -d)通过xargs命令管道传输到(删除),因此它将删除分支.请注意,分支必须在其上游分支中完全合并,因为仅使用-d选项,而不是-D(即--delete --force).