我遵循一个开发过程,即为每个新功能或故事卡创建一个新的本地分支。完成后,我合并到主分支,然后推。

随着时间的推移,由于懒惰或健忘,我最终会得到一个很大的本地分支列表,其中一些(如spike)可能还没有合并。

我知道如何列出我所有的本地分支,我知道如何删除一个分支,但我想知道是否有一个git命令,允许我删除我所有的本地分支?

下面是git分支——merged命令的输出。

user@machine:~/projects/application[master]$ git branch --merged
  STORY-123-Short-Description
  STORY-456-Another-Description
  STORY-789-Blah-Blah
* master

所有尝试删除grep -v \*列出的分支(根据下面的答案)都会导致错误:

error: branch 'STORY-123-Short-Description' not found.
error: branch 'STORY-456-Another-Description' not found.
error: branch 'STORY-789-Blah-Blah' not found.

我用的是:

git 1.7.4.1  
ubuntu 10.04  
GNU bash, version 4.1.5(1)-release  
GNU grep 2.5.4  

当前回答

我写了一个shell脚本,以删除除开发之外的所有本地分支

branches=$(git branch | tr -d " *")
output=""
for branch in $branches 
do
  if [[ $branch != "develop" ]]; then
    output="$output $branch"
  fi
done
git branch -d $output

其他回答

不建议解析git分支的输出,这对于Stack Overflow的读者来说不是一个很好的答案。

git branch is what is known as a porcelain command. Porcelain commands are not designed to be machine parsed and the output may change between different versions of Git. There are user configuration options that change the output of git branch in a way that makes it difficult to parse (for instance, colorization). If a user has set color.branch then you will get control codes in the output, this will lead to error: branch 'foo' not found. if you attempt to pipe it into another command. You can bypass this with the --no-color flag to git branch, but who knows what other user configurations might break things. git branch may do other things that are annoying to parse, like put an asterisk next to the currently checked out branch

git的维护者对围绕git分支输出编写脚本有这样的看法

为了找出当前的分支是什么,随意/粗心的用户可能有 围绕git分支编写脚本,这是错误的。我们积极劝阻 反对使用任何瓷器命令,包括git分支,在 脚本,因为命令的输出可能更改为 帮助人类消费用例。

建议手动编辑. Git目录下的文件(如. Git /refs/heads)的答案同样有问题(refs可能在. Git / packs -refs中,或者Git可能在未来改变它们的内部布局)。

Git提供for-each-ref命令来检索分支列表。

Git 2.7。X将引入——merged选项,这样您就可以执行如下操作来查找并删除合并到HEAD中的所有分支

for mergedBranch in $(git for-each-ref --format '%(refname:short)' --merged HEAD refs/heads/)
do
    git branch -d ${mergedBranch}
done

Git 2.6。X和更老的版本,您将需要列出所有本地分支,然后分别测试它们,以查看它们是否已经合并(这将显著地慢一些,并且稍微复杂一些)。

for branch in $(git for-each-ref --format '%(refname:short)' refs/heads/)
do
    git merge-base --is-ancestor ${branch} HEAD && git branch -d ${branch}
done

我建议一个更温和的答案。这里的许多答案都使用-D,这是强制删除的,无论更改是否已合并。这里有一行代码,它不影响那些没有合并变更的分支。

git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d

或者你也可以试试其他例子,只要把-D改成-D就行了。我知道OP问如何删除,但在大多数情况下,使用-d更安全。

对于powershell,这将工作:

git branch --format '%(refname:lstrip=2)' --merged `
    | Where-Object { $_ -ne 'master' } `
    | ForEach-Object { git branch -d $_ }

如果你想保持主,开发和所有远程分支。删除所有不在Github上的本地分支。

$ git fetch --prune

$ git branch | grep -v "origin" | grep -v "develop" | grep -v "master" | xargs git branch -D

1]它将删除远程库中不再使用的远程引用。

2]这将得到你所有分支的列表。从列表中删除包含master、develop或origin(远程分支)的分支。删除列表中的所有分支。

警告-这删除自己的本地分支以及。所以,当你合并了你的分支,并在合并后进行清理时,删除。

同时删除多个本地分支

# delete all local unmerged branches
git branch --no-merged | egrep -v "(^\*|master|dev)" | xargs git branch -D
# delete all local branches (merged and unmerged).
git branch | egrep -v "(^\*|master|dev)" | xargs git branch -D  

删除远程分支

# Deleting non-existent tracking branches
git remote prune <remote> --dry-run
# Deleting a single remote branch
git push <remote> --delete <branch>
# Deleting many remote branches at once
git branch -r --merged | egrep -v "(^\*|master|dev)" | sed 's/origin\///' | xargs -n 1 git push origin --delete