使用git远程修剪原点,我可以删除不在远程上的本地分支。
但是我还想删除从这些远程分支创建的本地分支(检查它们是否未合并会很好)。
我该怎么做呢?
使用git远程修剪原点,我可以删除不在远程上的本地分支。
但是我还想删除从这些远程分支创建的本地分支(检查它们是否未合并会很好)。
我该怎么做呢?
当前回答
基于上面的答案,我使用了更短的一行:
git remote prune origin | awk 'BEGIN{FS="origin/"};/pruned/{print $2}' | xargs -r git branch -d
此外,如果你已经修剪过了,并且有局部的树枝悬挂着,那么这将清理它们:
git branch -vv | awk '/^ .*gone/{print $1}' | xargs -r git branch -d
其他回答
在git帮助获取提供的信息中,有这样一个小项目:
-p, --prune
After fetching, remove any remote-tracking branches which no longer exist on the remote.
也许,git fetch -p是你要找的?
编辑:好吧,对于那些3年后还在争论这个答案的人,这里有一些关于我为什么给出这个答案的更多信息……
首先,OP说他们想“删除那些从远程分支创建的本地分支(这些分支不在远程上了)”。这在git中并不是完全可能的。举个例子。
假设我在中央服务器上有一个回购,它有两个分支,称为a和B。如果我将该回购克隆到我的本地系统,我的克隆将有本地引用(还不是实际的分支)称为origin/ a和origin/B。现在假设我做了以下事情:
git checkout -b A origin/A
git checkout -b Z origin/B
git checkout -b C <some hash>
这里的相关事实是,出于某种原因,我选择在我的本地回购上创建一个分支,该分支的名称与其起源不同,并且我还有一个本地分支,它(还)不存在于起源回购上。
Now let's say I remove both the A and B branches on the remote repo and update my local repo (git fetch of some form), which causes my local refs origin/A and origin/B to disappear. Now, my local repo has three branches still, A, Z, and C. None of these have a corresponding branch on the remote repo. Two of them were "created from ... remote branches", but even if I know that there used to be a branch called B on the origin, I have no way to know that Z was created from B, because it was renamed in the process, probably for a good reason. So, really, without some external process recording branch origin metadata, or a human who knows the history, it is impossible to tell which of the three branches, if any, the OP is targeting for removal. Without some external information that git does not automatically maintain for you, git fetch -p is about as close as you can get, and any automatic method for literally attempting what the OP asked runs the risk of either deleting too many branches, or missing some that the OP would otherwise want deleted.
还有其他的场景,比如,如果我在origin/A之外创建三个独立的分支,以测试三种不同的方法,然后origin/A消失了。现在我有三个分支,它们显然不能全部匹配名称,但它们是从origin/A创建的,因此OPs问题的字面解释需要删除所有三个分支。然而,如果你能找到一种可靠的方法来匹配它们,这可能并不理想……
这适用于我使用git 2.21.0 -它删除本地跟踪分支,这些分支合并到HEAD中,我之前在push上—set-upstream(我使用push.default=upstream,因为它最适合多个远程),并且上游分支已经被fetch—prune(或隐式地如果fetch。修剪=true在git配置):
git branch -vv --merged | grep ': gone]' | awk '{print $1}' | xargs git branch -d
-merged和-d的使用使得这是一个非常“安全”的删除。更激进的版本可以放弃-merged并使用-D
当遥控器本身不再存在时,我没有发现这里的答案有用。 我一直看到不存在的遥控器分支,但没有找到一个git命令来删除它们。
对我来说,解决方案是转到.git\refs\remotes目录,直接删除不再相关的文件。文件结构很容易理解。它与git branch -r的结构相同。
除了Schleis的答案(它工作得很完美),它还可以集成到Visual Studio中,所以在VS中打开git repo时修剪本地分支非常简单。
您可以使用外部工具功能调用sh.exe (git bash)与特定的目录和命令。它位于工具>外部工具菜单选项中(在VS 2022 17.1.0中)。我使用的参数如下:
命令:{Path-To-Git-Installation-Location} \ bin \ sh.exe
参数:——cd=$(SolutionDir) -c "git fetch -p;Git branch -r | awk '{print $1}' | egrep -v -f /dev/ fg /0 <(Git branch -vv | grep origin) | awk '{print $1}' | xargs Git branch -d"
初始目录:$(SolutionDir)
Git修剪本地分支的截图
值得注意的是,只有当你在VS中打开的解决方案在git repo目录中时,这才会起作用。
最后提醒-这可以通过Visual Studio的按键绑定用户界面在设置>通用>键盘和搜索工具进行按键绑定。ExternalCommand[n],其中n是外部命令表中的位置,您有这个外部工具的位置(它们可以在工具>外部工具对话框中重新排序)。请看下面的截图。
键盘绑定外部工具命令
Schleis的变种不适合我(Ubuntu 12.04),所以让我提出我的变种(清晰而闪亮:):
变体1(我更喜欢这个选项):
git for-each-ref --format='%(refname:short) %(upstream)' refs/heads/ | awk '$2 !~/^refs\/remotes/' | xargs git branch -D
版本2:
管制:
comm -23 <( git branch | grep -v "/" | grep -v "*" | sort ) <( git br -r | awk -F '/' '{print $2}' | sort ) | awk '{print "git branch -D " $1}'
b.移除分支:
comm -23 <( git branch | grep -v "/" | grep -v "*" | sort ) <( git br -r | awk -F '/' '{print $2}' | sort ) | xargs git branch -D