
tl;dr
Need to delete a branch in GitHub? It’s a simple operation and keeping your repo clean & tidy is incredibly important in any project. Learn the various Git delete branch commands (git delete branch locally, git delete branch remote, etc.) using this quick reference sheet. Bonus: also learn how to undo/recover a deleted branch.
# Delete a Git branch locally
git branch -d yourbranch
# Force a Git branch to be deleted locally
git branch -D yourbranch
# Delete a Git branch remotely
git push origin –delete yourbranch
# Delete merged branched
git branch –merged master | grep -v "\*master" | xargs -n 1 git branch -d
# Delete all Git branches (except master)
git branch | grep -v "master" | sed ‘s/^[ *]*//’ | sed ‘s/^/git branch -d /’ | bash
# Recover/undo a Git branch deletion
git fsck –full –no-reflogs | grep commit
git branch branchid
Git Delete Branch Commands
It’s important to regularly delete outdated branches. Why? Because it keeps things organized and clean allowing other developers that may be working in the repo the ability to easily see what’s current.
Why not? If they’re not being used, they serve no purpose. They’ll also clutter up you repo and make it difficult to work with the list of branches.
As soon as it’s no longer needed. For instance, after a pull request has been merged or an experiment is no longer needed.
Use the “git branch -d” or “git branch -D” commands. -D meaning you’re forcing it to be deleted.
“git push origin –delete” or previous to v1.7.0, “git push origin :branchname”.
Use “git branch –merged master | grep -v “\*master” | xargs -n 1 git branch -d”.
Find the commit ID, then check it out. Check out the details below on how to accomplish this.
An example scenario.
Let’s take this scenario for example on when to delete a branch.
Someone reports a bug that we decide to hotfix. So we create a branch for that hotfix off of “master”, name it “hotfix”, and we push it up so that all of the developers can collaborate on fixing it.
The bug gets fixed and merged into “master”. So now what do we do with it? Should it just sit out there as a branch forever until the end of time or should we now delete it, since it has served its purpose? It seems unclean to just leave branches lying around everywhere, as the list of branches will likely become very long, most of which aren’t even necessary anymore.
Branches can be safely removed without risk of losing any changes. Consider a scenario in which a branch patch-1 is about to be merged with the master branch through a pull request. Before the merge, master and patch-1 both point to separate commits in git’s commit history. After the merge (assuming a new merge commit is added), both master and patch-1 point to a new merge commit. At this point, the pull request is complete, and future commits should only be made on master, not patch-1.
When should you keep an outdated branch?
Never. The only reason for keeping branches around once they have served their purpose and been merged back into the main trunk of the repository is to provide some historic context. Fortunately, git provides another feature for this precise purchase: tags.
Any time you find that you want a bookmark or reference to a particular commit, such as to mark the commit that was used for a deployment, you can add a tag for this purpose. You could even create a simple command to combine deleting unused branches and adding tags with the same name as the branch if you were so inclined (and really did want to track every branch).
To add a tag to the current branch and commit, just execute:
git tag tagname
This will only add the tag locally. To sync your change remotely, use:
git push --tags
Let’s dig into the code.
Learn how to delete a branch locally, remotely, delete merged branches, and even recover or undo a deletion with the commands below. Have an idea of how to do it better? Comment below with your suggestion.
Locally Delete a Git Branch
$ git branch -d yourbranch
Deleted branch yourbranch (was 2a4ef7)
Depending if you already have unmerged commits in the branch you’re trying to delete, you may see this error:
error: The branch ‘my-branch’ is not fully merged.
If you are sure you want to delete it, run ‘git branch -D my-branch’.
This protects you from losing your reference to those commits, which means you would effectively lose access to that entire line of development. If you really want to delete the branch (e.g., it’s a failed experiment), you can use the capital -D flag:
$ git branch -D yourbranch
Deleted branch yourbranch (was 2a4ef7)
This is the command to use if you want to permanently throw away all of the commits associated with a particular line of development.
Delete a Remote Git Branch
As of Git v1.7.0, you can delete a remote branch using:
git push origin –delete yourbranch
Previous to Git v1.7.0, you can use:
git push origin :yourbranch
Delete a Merged Git Branch
Through time as you create and merge branches, you’ll want to delete the ones no longer needed. In order to delete local branches which have already been merged, run:
git branch –merged master | grep -v "\*master" | xargs -n 1 git branch -d
You can omit the master branch argument to remove local branches which have already been merged into the current HEAD:
git branch –merged | grep -v "\*" | xargs -n 1 git branch -d
For more information, see Git Clean: Delete Already-Merged Branches.
Blow Away all Git Branches
If you’re like me, you leave branches laying around, even after they’ve been merged into master
. The sad part is GitHub even provides a button to do the cleanup, but I can’t be bothered. So when you’re ready to do some real clean up on a repository, run this command to delete every branch in your local except for the master
branch.
git branch | grep -v "master" | sed ‘s/^[ *]*//’ | sed ‘s/^/git branch -d /’ | bash
Undo Git Branch Delete
What if you accidentally deleted a branch or just need to recover one that’s been deleted? Not a problem. Use the following to find the HEAD commit of the deleted branch to get it back:
$ git fsck –full –no-reflogs | grep commit
dangling commit 609bb956bc9ef1b43c0w6af60fa009e37be0566d
dangling commit 608bb956bc9ef1b43c0g4af60fa0e9e34be0566d
dangling commit 708bb956bc9ef1b43c0wdaf60fa000e99be0566d
dangling commit 402bb956bc9ef1b43c0d4af60fa001e87be0566d
Then do a git show
to find the commit you want to recover:
git show 609bb956bc9ef1b43c0w6af60fa009e37be0566d
Once you found the commit you want, do a git branch
to recover the branch:
git branch 609bb956bc9ef1b43c0w6af60fa009e37be0566d
Simple stuff!
The downside to deleting branches.
One downside is that you will break any hyperlinks to the branch’s location (in GitHub, etc.). Personally I very rarely have a permanent link to non-primary branches, and if I did want to link to some work on a given branch, I would most likely do so after it had been made into a pull request (in which case I would link to the PR). That said, this is a potential downside to deleting branches.
In Conclusion
Don’t let your Git branches get out of control. Keep them organized and clean by using the delete commands found above. Its considered best practice and helps developers easily understand what branches are up-to-date and in use.
Check out these other useful Git articles on more ways to use Git effectively:
- Learn what & how to do a git rebase.
- Define the files git should ignore from adding to the rep.
1 comment on “Git Delete Branch Commands”.
# Dec 5, 2017
Hi,
What if the branch is deleted from the remote (using GITHUB web interface) and when we create a new clone, we don’t have any information in the reflog, as the reflog tracks the Head movement only on the local repository.
Thanks,
Peeyush
All comments posted on 'Git Delete Branch Commands' are held for moderation and only published when on topic and not rude. Get a gold star if you actually read & follow these rules.
You may write comments in Markdown. This is the best way to post any code, inline like `<div>this</div>` or multiline blocks within triple backtick fences (```) with double new lines before and after.
Want to tell me something privately, like pointing out a typo or stuff like that? Contact Me.