如何删除已合并的分支?我可以一次删除所有分支,而不是逐个删除每个分支吗?
当前回答
Windoze友好的Python脚本(因为git sweep阻塞了Wesnoth存储库):
#!/usr/bin/env python
# Remove merged git branches. Cross-platform way to execute:
#
# git branch --merged | grep -v master | xargs git branch -d
#
# Requires gitapi - https://bitbucket.org/haard/gitapi
# License: Public Domain
import gitapi
repo = gitapi.Repo('.')
output = repo.git_command('branch', '--merged').strip()
for branch in output.split('\n'):
branch = branch.strip()
if branch.strip(' *') != 'master':
print(repo.git_command('branch', '-d', branch).strip())
https://gist.github.com/techtonik/b3f0d4b9a56dbacb3afc
其他回答
要删除已合并到主分支的本地分支,我使用以下别名(git-config-e--global):
cleanup = "!git branch --merged master | grep -v '^*\\|master' | xargs -n 1 git branch -D"
我使用gitbranch-D来避免错误:分支“somebranch”未完全合并。消息,而我的当前签出与主分支不同。
注意:如果您的工作流可能将master和dev作为祖先,则可以添加其他分支以排除它们。通常我从“sprint-start”标签分支出来,master、dev和qa都不是祖先。
首先,列出在远程中合并的本地跟踪分支(考虑使用-r标志列出所有远程跟踪分支)。
git branch --merged
您可能会看到一些不想删除的分支。我们可以添加一些参数来跳过我们不想删除的重要分支,比如master或develop。下面的命令将跳过master分支和其中包含dev的任何内容。
git branch --merged| egrep -v "(^\*|master|main|dev)"
如果要跳过,可以将其添加到egrep命令中,如下所示。不会删除分支skip_branch_name。
git branch --merged| egrep -v "(^\*|master|main|dev|skip_branch_name)"
要删除已合并到当前签出分支的所有本地分支,请执行以下操作:
git branch --merged | egrep -v "(^\*|master|main|dev)" | xargs git branch -d
您可以看到master和dev被排除在外,以防它们是祖先。
您可以通过以下方式删除合并的本地分支:
git branch -d branchname
如果未合并,请使用:
git branch -D branchname
要从远程使用中删除它,请执行以下操作:
git push --delete origin branchname
git push origin :branchname # for really old git
从远程删除分支后,可以通过以下方式进行修剪以消除远程跟踪分支:
git remote prune origin
或者如另一个答案所示,通过以下方式修剪单独的远程跟踪分支:
git branch -dr branchname
gitbranch--merged|grep-Ev'^(.master |\*)'|xargs-n 1 gitbranch-d将删除除当前签出的分支和/或主分支外的所有本地分支。
这是一篇有助于理解这些命令的文章:Steven Harman的Git Clean:Delete Already Merged Branches。
我最喜欢的简单脚本:
git branch --merged | grep -E -v "(master|main|develop|other)" | xargs git branch -d
我的Bash脚本贡献大致基于mmrobin的答案。
它需要一些有用的参数来指定include和exclude,或者只检查/删除本地或远程分支,而不是两者。
#!/bin/bash
# exclude branches regex, configure as "(branch1|branch2|etc)$"
excludes_default="(master|next|ag/doc-updates)$"
excludes="__NOTHING__"
includes=
merged="--merged"
local=1
remote=1
while [ $# -gt 0 ]; do
case "$1" in
-i) shift; includes="$includes $1" ;;
-e) shift; excludes="$1" ;;
--no-local) local=0 ;;
--no-remote) remote=0 ;;
--all) merged= ;;
*) echo "Unknown argument $1"; exit 1 ;;
esac
shift # next option
done
if [ "$includes" == "" ]; then
includes=".*"
else
includes="($(echo $includes | sed -e 's/ /|/g'))"
fi
current_branch=$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
if [ "$current_branch" != "master" ]; then
echo "WARNING: You are on branch $current_branch, NOT master."
fi
echo -e "Fetching branches...\n"
git remote update --prune
remote_branches=$(git branch -r $merged | grep -v "/$current_branch$" | grep -v -E "$excludes" | grep -v -E "$excludes_default" | grep -E "$includes")
local_branches=$(git branch $merged | grep -v "$current_branch$" | grep -v -E "$excludes" | grep -v -E "$excludes_default" | grep -E "$includes")
if [ -z "$remote_branches" ] && [ -z "$local_branches" ]; then
echo "No existing branches have been merged into $current_branch."
else
echo "This will remove the following branches:"
if [ "$remote" == 1 -a -n "$remote_branches" ]; then
echo "$remote_branches"
fi
if [ "$local" == 1 -a -n "$local_branches" ]; then
echo "$local_branches"
fi
read -p "Continue? (y/n): " -n 1 choice
echo
if [ "$choice" == "y" ] || [ "$choice" == "Y" ]; then
if [ "$remote" == 1 ]; then
remotes=$(git remote)
# Remove remote branches
for remote in $remotes
do
branches=$(echo "$remote_branches" | grep "$remote/" | sed "s/$remote\/\(.*\)/:\1 /g" | tr -d '\n')
git push $remote $branches
done
fi
if [ "$local" == 1 ]; then
# Remove local branches
locals=$(echo "$local_branches" | sed 's/origin\///g' | tr -d '\n')
if [ -z "$locals" ]; then
echo "No branches removed."
else
git branch -d $(echo "$locals" | tr -d '\n')
fi
fi
fi
fi
推荐文章
- RPC失败;卷度传输已关闭,剩余未完成的读取数据
- 我应该在.gitignore文件中添加Django迁移文件吗?
- 错误:您对以下文件的本地更改将被签出覆盖
- Git rebase—即使所有合并冲突都已解决,仍然会继续报错
- 在Git中,我如何知道我的当前版本是什么?
- 跟踪所有远程git分支作为本地分支
- 为什么要把Gradle Wrapper提交给VCS?
- 自定义SSH端口上的Git
- git如何显示不存在于.gitignore中的未跟踪文件
- Git错误:遇到7个文件应该是指针,但不是
- GitHub克隆与OAuth访问令牌
- 移动(或“撤销”)最后一个git提交到非暂存区域
- 我可以在GitHub上对要点进行拉请求吗?
- Hg:如何做一个像git的rebase
- 如何丢弃远程更改并将文件标记为“已解决”?