Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

git - 如何删除所有已合并的Git分支?(How can I delete all Git branches which have been merged?)

I have many Git branches.

(我有很多Git分支。)

How do I delete branches which have already been merged?

(如何删除已经合并的分支?)

Is there an easy way to delete them all instead of deleting them one by one?

(有没有一种简单的方法可以将它们全部删除,而不是一个一个地删除它们?)

  ask by Nyambaa translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

UPDATE:

(更新:)

You can add other branches to exclude like master and dev if your workflow has those as a possible ancestor.

(如果您的工作流程可能将其作为祖先,则可以添加其他分支以排除像master和dev这样的分支。)

Usually I branch off of a "sprint-start" tag and master, dev and qa are not ancestors.

(通常,我从“ sprint-start”标签分支出来,而master,dev和qa不是祖先。)

First, list all branches that were merged in remote.

(首先,列出所有在远程中合并的分支。)

git branch --merged

You might see few branches you don't want to remove.

(您可能会看到几个不想删除的分支。)

we can add few arguments to skip important branches that we don't want to delete like master or a develop.

(我们可以添加一些参数来跳过不想删除的重要分支,例如master或developer。)

The following command will skip master branch and anything that has dev in it.

(以下命令将跳过master分支以及其中包含dev的所有内容。)

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

If you want to skip, you can add it to the egrep command like the following.

(如果要跳过,可以将其添加到egrep命令中,如下所示。)

The branch skip_branch_name will not be deleted.

(分支skip_branch_name不会被删除。)

git branch --merged| egrep -v "(^*|master|dev|skip_branch_name)"

To delete all local branches that are already merged into the currently checked out branch:

(删除所有已经合并到当前签出分支中的本地分支:)

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

You can see that master and dev are excluded in case they are an ancestor.

(您可以看到,如果master和dev是祖先,则将它们排除在外。)


You can delete a merged local branch with:

(您可以使用以下方法删除合并的本地分支:)

git branch -d branchname

If it's not merged, use:

(如果未合并,请使用:)

git branch -D branchname

To delete it from the remote in old versions of Git use:

(要在旧版Git中从远程删除它,请使用:)

git push origin :branchname

In more recent versions of Git use:

(在最新版本的Git中使用:)

git push --delete origin branchname

Once you delete the branch from the remote, you can prune to get rid of remote tracking branches with:

(从远程删除分支后,可以通过以下方法修剪掉远程跟踪分支:)

git remote prune origin

or prune individual remote tracking branches, as the other answer suggests, with:

(或如其他答案所示,修剪各个远程跟踪分支,方法是:)

git branch -dr branchname

Hope this helps.

(希望这可以帮助。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...