Git: Delete a Branch

Deleting your local branch

Once you are done with your new feature / bug fix, it's time to clean you git tree by removing the branches you are not using anymore. It's quite simple. The following command removes a local branch.


git branch -d BranchName

Deleting a remote branch

Now, maybe you don't want anyone to access the branch you just removed localy. The way to do this is to also delete it from the remote server. This one is a little tricky. Don't forget the : before the branch name. The following example suppose that your branch is located on origin. If you don't know what I'm talking about it's probably on origin so keep this intact.


git push origin :BranchName

Prune your local repository

Like you, other people from your team will create and delete branches as the project grows. They may have closed a branches that you previously checked out on your local compute. It's a good habit to remove (prune) those branches from your local computer when they aren't on the remote server.

This gives your the chance to see if any branches have been removed from the server.


git remote prune origin --dry-run

Once you are ready to prune your tree, simply remove the --dry-run flag.


git remote prune origin

Automatically remove the reference to deleted remote branches

The following command removes the reference not the local branch from your computer. This will happen when you do a git fetch or a git pull.


git config --global fetch.prune true

See Also

Links

Advertisement