使用git远程修剪原点,我可以删除不在远程上的本地分支。
但是我还想删除从这些远程分支创建的本地分支(检查它们是否未合并会很好)。
我该怎么做呢?
使用git远程修剪原点,我可以删除不在远程上的本地分支。
但是我还想删除从这些远程分支创建的本地分支(检查它们是否未合并会很好)。
我该怎么做呢?
当前回答
它将列出远程跟踪分支从remote中删除的本地分支
$ git remote prune origin --dry-run
如果你想从未被跟踪的本地去引用这些本地分支
$ git remote prune origin
其他回答
以下是我的解决方案:
git fetch -p
git branch -vv | grep ": gone" | awk '{print $1}' | xargs git branch -d
-p用于删除远程上不再存在的任何远程跟踪引用。因此,第一步将删除对远程分支的引用。 -vv用于显示每个head的sha1和commit主题行,以及与上游分支的关系(如果有的话)。第二步将获取所有本地分支,grep命令将过滤掉已删除的分支。
如果要删除所有已经合并到master中的本地分支,可以使用以下命令:
git branch --merged master | grep -v '^[ *]*master$' | xargs git branch -d
如果你使用main作为你的主分支,你应该相应地修改命令:
git branch --merged main | grep -v '^[ *]*main$' | xargs git branch -d
更多信息。
不知道如何一次性完成,但是git git branch -d <branchname>只会在完全合并的情况下删除本地分支。注意小写的d。
git branch -D <branchname>(注意大写D)将删除本地分支,无论其合并状态如何。
你可以通过一些简单的操作来做到这一点:
输出你所有的分支到一个临时文件:
git branch > branches.tmp
打开文件并删除分支以排除它们从本地删除(分支如develop/master/main/…) 通过cat命令将分支名称传递给xargs并删除分支:
cat branches.tmp | xargs git branch -D
I wanted something that would purge all local branches that were tracking a remote branch, on origin, where the remote branch has been deleted (gone). I did not want to delete local branches that were never set up to track a remote branch (i.e.: my local dev branches). Also, I wanted a simple one-liner that just uses git, or other simple CLI tools, rather than writing custom scripts. I ended up using a bit of grep and awk to make this simple command, then added it as an alias in my ~/.gitconfig.
[alias]
prune-branches = !git remote prune origin && git branch -vv | grep ': gone]' | awk '{print $1}' | xargs -r git branch -D
这是一个git配置-全局…命令可以方便地将其添加为git prune-branches:
git config --global alias.prune-branches '!git remote prune origin && git branch -vv | grep '"'"': gone]'"'"' | awk '"'"'{print $1}'"'"' | xargs -r git branch -d'
注意:对git分支使用-D标志可能非常危险。所以,在上面的config命令中,我使用-d选项来git分支而不是-d;我在实际配置中使用-D。我使用-D是因为我不想听到Git抱怨未合并的分支,我只是想让它们消失。您可能也需要这个功能。如果是这样,只需在配置命令的末尾使用-D而不是-D。