There will come a time when you optimize your workflow to ensure PR requests are as small as possible.
Each request can be minimal, so you end up with tons of new local branches that have probably already been merged.
Below is an example of my local daily.dev repo with all the branches I have locally.
Time to make some changes and clean up our mess.
Removing local git branches
We could go to the editor and click remove on the local branches.
However, we wouldn't be developers if we didn't use the terminal correctly.
To delete one local branch, we can use the following command.
git branch -d BRANCH_NAME
However, this will only work for merged branches. If you still want to proceed, you can use the capital D like this:
git branch -D BRANCH_NAME
Deleting all local branches
However, when we have many local branches, we might want to delete all of them at once.
For that, it's important to note that the delete call can handle multiple files.
First, we have to find all the branches. I used the following command.
git branch | grep -v \*
However, this also includes our master/main branch.
git branch | grep -v "master\|main"
And if you only want to remove merged branches, you can use the following addition.
git branch --merged | grep -v "master\|main"
To execute deletion, we can pass another argument for the delete command.
git branch --merged | grep -v "master\|main" | xargs git branch -D
Conclusion
We can have many local branches that we might want to clean up in one go.
To delete a single branch, use the following command.
git branch -d BRANCH_NAME
# use -D for unmerged branches
If you want to delete all merged local branches except master/main, use the following command.
git branch --merged | grep -v "master\|main" | xargs git branch -D
Note: The above only deletes merged branches
If you want to delete all local branches except master/main, use the following.
git branch | grep -v "master\|main" | xargs git branch -D
Note: The above also deletes unmerged branches
Thank you for reading, and let's connect!
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter
Top comments (22)
Very handy one-liners.
There should be a git command especially for this cases. Something like:
And the main branch is ignored by default, to not do something dangerous. Maybe also a hint if you delete a branch when you are checkout into it at the moment ...
Or some similar feature, with a good described documentation in one place π
Git is a goldmine for content creators.
So many common tasks don't have an intuitive solution, and the official documentation has a chicken and egg problem where you can search only what you already know how to do.
So lots of Google traffic.
There should really be a really good cleanup way actually.
Heck, even simply an automated way, merged branch -> get info back to delete it when it's not active anymore.
But at the very least indeed a option to make it easier and safe for people to do this.
When this starts happening at scale, this is probably an indication that we need to think at a higher level of abstraction than 'branches' and 'pull requests', or at least we need a tool that simplifies part of this workflow. Lately I am trying out Git Patch Stack for this. It creates a branch and a PR automatically from a single commit. At this level of abstraction it calls the combination of the branch and PR a 'patch'. You can create and layer multiple patches in a stack, the intention is that each patch gets sent as a PR to be reviewed. As patches are reviewed and merged, the patches above those ones are automatically rebased on top of the new branch head.
It takes a little setup thoughβthe tool by itself doesn't know how to create PRs, it relies on shell scripts called 'hooks' to do that. I am trying it out now but haven't yet made up my mind about whether it's the ultimate solution to the problem. I just think a solution above the level of branches is needed.
Thanks for that vision Yawar.
At what large scale are you working at, just interested to hear :)
Maybe this command is helpful to someone ;)
The command deletes all local branches without an origin attached
git fetch -p && for branch in $(git branch -vv | grep ': gone]' | awk '{print $1}'); do git branch -D $branch; done
Thanks for sharing Oli
Hi Chris, thanks for the post. Could you create series regarding git and git bash. I am struggling with git commands. Mostly when i've to work on already present repositeries. Could you make series how to use git and it's commands affectvily both for new repos and existed repos. Thanks in advance.
Hey,
I actually already did a whole series on Git, this one was kind of the aftermath.
You can find the full series here:
daily-dev-tips.com/tags/git/
Love these. I would suggest a mention of how to create an alias for these commands to make using them more intuitive(a challenge with git other comments point out). That is how I've set up similar commands for my shell.
example:
alias deleteLocalBranches="git branch | grep -v "main" | xargs git branch -D"
Yeah, wrote the alias on my todo list to refactor.
If you do have some time before me, feel free to modify it in my repo already.
github.com/rebelchris/daily-dev-ti...
Thanks β€οΈ! That's a useful command! I used to do git prune than going to delete the highlights branches in red by vscode π€!
With Oh My Zsh you can use the alias "gbda" :)
(git branch --no-color --merged | command grep -vE "^(+||\s($(git_main_branch)|development|develop|devel|dev)\s*$)" | command xargs -n 1 git branch -d)
Oh dang! TIL π
Clone the repo again ;)
I used to do this π π€
Also good
Nice! Love it π
Actually if you want to can add it to my repo?
Would be great for other people too.
github.com/rebelchris/daily-dev-ti...
I literally went blank about how to do it earlier today (and then got distracted so never googled it). Thank you!
Glad it helped :)