Clean up your local branches after merge and delete in GitHub

If you are a Github user, then you might familiar with Pull Requests. Github has an options to delete a branch after merging of pull request.

After a Pull Request has been merged, you’ll see a button to delete the lingering branch:

Above action will delete the branch only in the remote. Then there is a question: how do I clean up my local branches? I found an answer as follows

Lets say my test branch name feature-collaboration

1. List branches in local machine

The command git branch -a shows the test branch feature-collaboration is present on local and also present on remote

git branch -a

2. Prune/Cleanup the local references to remote branch

The command git remote prune origin --dry-run lists branches that can be deleted/pruned on your local. An option --dry-run is needed.

git remote prune origin --dry-run

Now go ahead and actually prune/cleanup the local references by running the command git remote prune origin. Note that you don't need an option --dry-run.

git remote prune origin

Again, run the command git branch -a will show the local status of branches.

git branch -a

Now you can notice that remotes/origin/feature-collaboration has been removed, but not the local branch feature-collaboration.

3. Delete local branch

If wanted, we can clean-up the local branch feature-collaboration as well

git branch -d

That's it. Have a nice day.

Vote on HN

comments powered byDisqus