How to remove a branch in git

Posted in Tutorials

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

In this tutorial, we will learn how to remove a branch in git.  The nice thing about git is that branching in easy and inexpensive.  You can branch to perform experiements and if experiements doesn’t pan out, you can abandon and remove the branch.

In this example, suppose we have a working nodejs application in app.js and git status is clean.  We are on the master branch.  We now create a local branch called “play”…

git branch play

This created the branch “play” as confirmed when you do …

git branch
* master

The asterisk shows that we are still on the master branch.  We switch to the play branch by …

git checkout play

And the console returns the message “Switch to branch play”.

A shorthand to perform both the “git branch” and “git checkout” commands at the same time is …

git checkout -b play

Now we can play with our code, making a change to app.js.  And perhaps doing …

git add .
git commit -m “playing”

Let say that we want to abandon this change and go back to master.

git checkout master

Now to delete the branch “play”…

git branch -d “play”

We get the following error because we had made changes to the branch that we did not merge to master…

error: The branch ‘play’ is not fully merged.

Nothing happened.    If you are sure you want to still the branch, force it with …

git branch -D “play”

Now local branch “play” has been deleted.  If this branch was pushed to remote and you want to delete the remote branch, see this tutorial.