是否有一种简单的方法可以删除所有远程对等分支不再存在的跟踪分支?

例子:

分支机构(本地和远程)

主人原始/主起源/bug-fix-a起源/bug-fix-b起源/bug-fix-c

在本地,我只有一个主分支。现在我需要处理bug-fix-a,所以我检查它,处理它,并将更改推到远程。接下来,我对bug-fix-b做同样的操作。

分支机构(本地和远程)

主人bug-fix-abug-fix-b型原始/主起源/bug-fix-a起源/bug-fix-b起源/bug-fix-c

现在我有本地分支机构master,bug-fix-a,bug--fix-b。主分支维护者将把我的更改合并到主分支中,并删除他已经合并的所有分支。

因此,当前状态为:

分支机构(本地和远程)

主人bug-fix-abug-fix-b型原始/主起源/bug-fix-c

现在我想调用一些命令来删除分支(在本例中为bug-fix-a、bug-fix-b),这些分支在远程存储库中不再表示。

它类似于现有命令git remote prune origin,但更类似于git local prune origin。


当前回答

还有一个答案,因为没有一个解决方案符合我对优雅和跨平台的需求:

删除不在远程上的本地分支的命令:

for b in $(git for-each-ref --format='%(if:equals=[gone])%(upstream:track)%(then)%(refname:short)%(end)' refs/heads); do git branch -d $b; done

要将其与gitconfig集成,以便可以使用git branch prune运行:

Bash

git config --global alias.branch-prune '!git fetch -p && for b in $(git for-each-ref --format='\''%(if:equals=[gone])%(upstream:track)%(then)%(refname:short)%(end)'\'' refs/heads); do git branch -d $b; done'

PowerShell(PowerShell)

git config --global alias.branch-prune '!git fetch -p && for b in $(git for-each-ref --format=''%(if:equals=[gone])%(upstream:track)%(then)%(refname:short)%(end)'' refs/heads); do git branch -d $b; done'

(需要帮助查找PowerShell和bash的通用命令)

为什么这个答案是最好的?

提供了一个完整的解决方案:向git添加一个git branch prune命令在Windows PowerShell中工作正常核心思想是@jason.rickman对每个裁判使用git的防弹方法分析和过滤是使用--filter完成的,因此不需要外部依赖项

说明:

在~\.gitconfig中添加新别名。执行此操作后,只需执行git分支修剪在此别名中:使用--prune标志获取分支,该标志“修剪不再位于远程的远程跟踪分支”对每个ref和--filter使用git,以获取分支列表(无远程)循环此列表并安全删除分支

其他回答

这里有一个解决方法,我用它来处理鱼壳。在Mac OS X 10.11.5、fish 2.3.0和git 2.8.3上测试。

function git_clean_branches
  set base_branch develop

  # work from our base branch
  git checkout $base_branch

  # remove local tracking branches where the remote branch is gone
  git fetch -p

  # find all local branches that have been merged into the base branch
  # and delete any without a corresponding remote branch
  set local
  for f in (git branch --merged $base_branch | grep -v "\(master\|$base_branch\|\*\)" | awk '/\s*\w*\s*/ {print $1}')
    set local $local $f
  end

  set remote
  for f in (git branch -r | xargs basename)
    set remote $remote $f
  end

  for f in $local
    echo $remote | grep --quiet "\s$f\s"
    if [ $status -gt 0 ]
      git branch -d $f
    end
  end
end

几点注意事项

确保设置正确的base_branch。在本例中,我使用develop作为基本分支,但它可以是任何东西。

这一部分非常重要:grep-v“\(master\|$base_branch\|\*\)”。它确保您不会删除主分支或基本分支。

我使用gitbranch-d<branch>作为额外的预防措施,以便不删除任何尚未与上游或当前HEAD完全合并的分支。

一种简单的测试方法是用echo“将删除$f”替换gitbranch-d$f。

我想我还应该补充一句:使用风险自负!

我最近发布了一个解决方案,我已经使用了很长时间,它依赖于分支命名来包含问题编号,您可以看到这个答案,它链接到我的git_clean_branches版本,这是一个交互式脚本,用于删除已关闭问题的分支(通常通过壁球合并)。

我使用了一个简短的方法来完成这项任务,我建议你也这样做,因为这样可以节省一些时间,并提高你的可见度

只需将以下代码段添加到.bashrc(macos上的.bashprofile)中。

git-cleaner() { git fetch --all --prune && git branch --merged | grep -v -E "\bmaster|preprod|dmz\b" | xargs -n 1 git branch -d ;};

获取所有遥控器仅从git中获取合并的分支从此列表中删除“受保护/重要”分支删除其余部分(例如,清理和合并的分支)

您必须编辑grep正则表达式以满足您的需要(这里,它防止删除master、prepod和dmz)

你可以这样做:

git branch -vv | grep 'origin/.*: gone]' | awk '{print $1}' | xargs git branch -d

附笔。:正如Sam H。

首先执行以下操作:

git remote prune origin

这里的答案似乎都不适合我,或者完全按照我的意愿(删除本地分支,而不是远程分支)。所以我写了这篇文章,虽然有点拗口,但很管用。

touch /tmp/remote-refs && touch /tmp/local-refs && touch /tmp/diff-refs;
while shortened=$(git for-each-ref refs/remotes) && echo ${${shortened}//"\/remotes\/origin"} > /tmp/remote-refs &&
shortened=$(git for-each-ref refs/heads | grep -v "master\|develop") && echo ${${shortened}//"\/heads"} > /tmp/local-refs &&
diff=$(comm -13 /tmp/remote-refs /tmp/local-refs) && echo ${${diff}##*refs/} > /tmp/diff-refs &&
xargs git branch -D </tmp/diff-refs; do :; done

只需确保首先运行git remote prune origin