使用git remote prune origin
可以删除本地不再追踪的远程分支。
但是我也想删除从这些远程分支创建的本地分支(检查它们是否未合并会很好)。
我应该怎么做?
使用git remote prune origin
可以删除本地不再追踪的远程分支。
但是我也想删除从这些远程分支创建的本地分支(检查它们是否未合并会很好)。
我应该怎么做?
在 git bash 中尝试以下操作,以获取和修剪删除分支的引用,然后修剪跟踪已删除分支的本地分支:
git fetch -p && git branch -d `git branch -vv | grep ': gone]' | awk '{print $1}' | xargs`
记得首先检出一个不会被删除的分支,以避免阻止分支的删除。
检查目标
for target in $(git branch | grep -Eiv "master|develop|branchYouWantToLive"); do echo $target; done
使用for和子命令运行
for target in $(git branch | grep -Eiv "master|develop|branchYouWantToLive"); do git branch -D $target; done
您可以扩展其他关于分支的功能。
最后注意事项 - 可以通过 Visual Studio 键绑定用户界面在“设置”>“常规”>“键盘”中搜索 Tools.ExternalCommand[n] 来绑定快捷键,其中 n 是您将此外部工具放置在外部命令表中的位置(可以在“工具”>“外部工具”对话框中重新排序)。请参见下面的屏幕截图。
我来到这个页面是为了寻找答案:“如何删除本地检出的分支,而这些分支已经没有上游分支了”
我也不在意本地分支是否已经合并,因为将其导入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
gfa + UP
,就可以获取以gfa
开头的最新的shell历史记录项,然后我就完成了。这个方法已经在我身上使用了6年了。gfa && gco develop && gl && for branch in $(git for-each-ref --format '%(refname) %(upstream:track)' refs/heads | awk '$2 == "[gone]" {sub("refs/heads/", "", $1); print $1}' | egrep -v '\*|master|main|develop'); do git branch -d $branch; done
alias gfa='git fetch --all --prune --jobs=10'
alias gco='git checkout'
alias gl='git pull'
alias gb='git --no-pager branch'
我已经将被接受的答案转化为一个稳健的脚本。你可以在我的git-extensions仓库中找到它。
$ git-rprune --help
Remove old local branches that do not exist in REMOTE any more.
With --test, only print which local branches would be deleted.
Note: To do this automatically on each fetch / pull:
git config --global fetch.prune true
Usage: git-rprune REMOTE [-t|--test|-f|--force] [-?|-h|--help]
git prune
冲突吗? - Macadameanebin/
子目录中。我还将其重命名为 git-rprune
,以避免名称冲突;实际上,我从未直接调用该命令,而是使用别名 oprune=rprune origin
。迄今为止,我从未见过内置的 git prune
,因为它是一条不常见的低级命令。 - Ingo Karkatgit co master && git branch | sed s/\*/\ / | xargs git branch -d 2> /dev/null
git remote prune origin
。git remote prune origin --dry-run
来查看它会在不做任何更改的情况下做什么。使用GUI界面?手动操作虽然繁琐,但快捷简便。
$ git gui
npx git-removed-branches
(干运行) 或npx git-removed-branches --prune
(真实运行)。详细信息请参见以下答案。 - Cristian Diaconescu