使用git远程修剪原点,我可以删除不在远程上的本地分支。

但是我还想删除从这些远程分支创建的本地分支(检查它们是否未合并会很好)。

我该怎么做呢?


当前回答

正如@tzacks所说…有一个NPM包非常方便。只做:

npx git-removed-branches --prune

(我本想评论一下,但名声不够好)

其他回答

在OS X上,支持cut -w

git branch -d $(git branch -vv | grep ': gone]' | cut -w -f 2 )

解释

删除以下分支

git branch -d ...

显示移除的分支

$ git branch -vv | grep ': gone]'
  chore/TECH-456   a4bdac8ac8 [origin/TECH-456: gone] chore: syntax error
  feature/TECH-678 0342f8e277 [origin/feature/TECH-678: gone] Added IsGross in new Income Details

保留分支名称

$ git branch -vv | grep ': gone]' | cut -w -f 2
chore/TECH-456
feature/TECH-678

检查目标

$(git分支| grep -Eiv“master|develop|branchYouWantToLive”);执行echo $target;完成

使用for &子命令运行

$(git分支| grep -Eiv“master|develop|branchYouWantToLive”);做git分支-D $target;完成

你可以扩展其他关于分支的东西。

在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 branch -d只会发出警告,而不是删除未合并的本地分支。

git branch -a | grep origin | tr -s ' ' | cut -d '/' -f3 | egrep -v -f /dev/fd/0 <(git branch -a | grep -v origin) | grep branch_prefix_that_I_care_about | xargs git branch -d

# translation
# git branch -a | grep origin | tr -s ' ' | cut -d '/' -f3
## this finds all remote branch names minus the "remote/origin/" part
#
# <(git branch -a | grep -v origin)
## this finds all local branch names and redirects it into the previous command
#
# egrep -v -f /dev/fd/0 STUFF
## this is doing some weird magic that I'm grokking as "take the set difference from whatever was piped in"
#
#
# overall translation: (STUFF TO CONSIDER) | egrep magic <(STUFF TO REMOVE FROM CONSIDERATION) | do cool things with resulting stuff

我很确定git remote prune origin是你想要的。

你可以运行它作为git远程剪枝起源-演练,看看它会做什么不做任何改变。