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

例子:

分支机构(本地和远程)

主人原始/主起源/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,以获取分支列表(无远程)循环此列表并安全删除分支

其他回答

有时,你只是想有一个干净的记录,但需要确保能够回到以前在当地分支机构的工作。

如果是这样,最简单的方法可能就是归档当前的本地克隆并创建一个新的克隆。

因此,对于仅具有本地默认分支的存储库X

move X X-2021-11-10
git clone http://...../X

如果您需要完全相同的来源,请询问git

git -C X remote -v

基于Git提示:删除旧的本地分支,这看起来类似于jason.rickman的解决方案。为此,我使用Bash实现了一个名为Git-gone的自定义命令:

$ git gone
usage: git gone [-pndD] [<branch>=origin]
OPTIONS
  -p  prune remote branch
  -n  dry run: list the gone branches
  -d  delete the gone branches
  -D  delete the gone branches forcefully

EXAMPLES
git gone -pn    prune and dry run
git gone -d     delete the gone branches

git gone-pn结合了修剪和列出“goned”分支:

$ git gone -pn
  bport/fix-server-broadcast         b472d5d2b [origin/bport/fix-server-broadcast: gone] Bump modules
  fport/rangepos                     45c857d15 [origin/fport/rangepos: gone] Bump modules

然后,您可以使用git-gone-d或git-gone-d来扣动扳机。

笔记

我使用的正则表达式是“$BRANCH/.*:gone]”,其中$BRANCH通常是源代码。如果您的Git输出本地化为法语等,这可能不会起作用。Sebastian Wiesner还为Windows用户将其移植到Rust。那个也叫git gone。

另一个答案。

如果你正在寻找一些新奇的东西,这里是我在fzf中使用的别名。

LANG=C git for-each-ref --sort=-committerdate --format '%(refname:short) %(upstream:track)' refs/heads \
  | awk '$2 == "[gone]" {print $1}' \
  | fzf --multi --bind ctrl-a:select-all,ctrl-t:toggle-all \
        --preview='git log --color=always --graph --format=compact {1}' \
        --header='Select [gone] branch to remove' \
        --header-first --reverse \
  | xargs --no-run-if-empty git branch -d

解释:

强制LANG=C以避免[gone]模式上的本地化对每个引用使用git,以避免git分支中的任何格式更改使用awk过滤。我尝试使用--format=“%(if:equals=[gone])%…”,但这意味着要管理空行。使用fzf选择要删除的分支。添加快捷键:[ctrl-a]选择全部,[ctrl-t]反转选择([tab]照常选择)。您可以使用[enter]进行验证,也可以使用[esc]中止。我使用这个分支选择器作为确认阶段。在右侧显示所选分支的日志(以记住日期和内容…)。我使用下面列出的紧凑格式。您可以使用任何(不太宽)日志格式。--如果为空,则不运行。它不是强制性的,有助于避免在中止进程时出错(与没有分支名称的gitbranch-d相关)。

别名可能看起来很糟糕:

[alias]
      gone = "!f() { LANG=C git for-each-ref --sort=-committerdate --format '%(refname:short) %(upstream:track)' refs/heads | awk '$2 == \"[gone]\" {print $1}' | fzf --multi --bind ctrl-a:select-all,ctrl-t:toggle-all --preview='git log --color=always --graph --format=compact {1}' --header='Select [gone] branch to remove' --header-first --reverse | xargs --no-run-if-empty git branch -d ; }; f"
[pretty]
      compact = %Cred%h%Creset%C(auto)%d%Creset%n  Author: %an <%aE>%n  Date:  %cd%n     %s

(与上文相同,“)

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

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

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

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

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,以获取分支列表(无远程)循环此列表并安全删除分支