I have many Git branches. 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?
相关问题
- How to add working directory to deployment in GitH
- Why does recursive submodule update from github fa
- Extended message for commit via Visual Studio Code
- Emacs shell: save commit message
- Can I organize Git submodules in a flat hierarchy?
相关文章
- 请教Git如何克隆本地库?
- java开发bug问题:GitHub授权登录无法获取授权账号信息?
- What is the tortoisehg gui equivalent of doing “hg
- Is there a Github markdown language identifier for
- How to use Mercurial from Visual Studio 2010?
- “no implicit conversion of Integer into String” er
- GitHub:Enterprise post-receive hook
- Git Clone Fails: Server Certificate Verification F
For those of you that are on Windows and prefer PowerShell scripts, here is one that deletes local merged branches:
Below query works for me
and this will filter any given branch in the grep pipe.
Works well over http clone, but not so well for the ssh connection.
Based on some of these answers I made my own Bash script to do it too!
It uses
git branch --merged
andgit branch -d
to delete the branches that have been merged and prompts you for each of the branches before deleting.If you'd like to delete all local branches that are already merged in to the branch that you are currently on, then I've come up with a safe command to do so, based on earlier answers:
This command will not affect your current branch or your master branch. It will also tell you what it's doing before it does it, using the -t flag of xargs.
To delete local branches that have been merged to master branch I'm using the following alias (
git config -e --global
):I'm using
git branch -D
to avoiderror: The branch 'some-branch' is not fully merged.
messages while my current checkout is different from master branch.I've used Adam's answer for years now. That said, that there are some cases where it wasn't behaving as I expected:
1 & 2 were straightforward to address, with just a change to the regex. 3 depends on the context of what you want (i.e. only delete branches that haven't been merged into master or against your current branch). 4 has the potential to be disastrous (although recoverable with
git reflog
), if you unintentionally ran this in detached HEAD state.Finally, I wanted this to all be in a one-liner that didn't require a separate (Bash|Ruby|Python) script.
TL;DR
Create a git alias "sweep" that accepts an optional
-f
flag:and invoke it with:
or:
The long, detailed answer
It was easiest for me to create an example git repo with some branches and commits to test the correct behavior:
Create a new git repo with a single commit
Create some new branches
Desired behavior: select all merged branches except: master, develop or current
The original regex misses the branches "masterful" and "notmaster" :
With the updated regex (which now excludes "develop" rather than "dev"):
Switch to branch foo, make a new commit, then checkout a new branch, foobar, based on foo:
My current branch is foobar, and if I re-run the above command to list the branches I want to delete, the branch "foo" is included even though it hasn't been merged into master:
However, if I run the same command on master, the branch "foo" is not included:
And this is simply because
git branch --merged
defaults to the HEAD of the current branch if not otherwise specified. At least for my workflow, I don't want to delete local branches unless they've been merged to master, so I prefer the following variant:Detached HEAD state
Relying on the default behavior of
git branch --merged
has even more significant consequences in detached HEAD state:This would have deleted the branch I was just on, "foobar" along with "foo", which is almost certainly not the desired outcome. With our revised command, however:
One line, including the actual delete
All wrapped up into a git alias "sweep":
The alias accepts an optional
-f
flag. The default behavior is to only delete branches that have been merged into master, but the-f
flag will delete branches that have been merged into the current branch.