In development with Git, you create a branch when you’re woking on a feature for a specific purpose.
As the development progresses, the number of the branches keep increasing and you end up feeling like deleting some branch that you already finished with.
This article is about how well you manage local branch, remote branch and remote-tracking branch.
Git command to delete a branch
First, please note that there are three kinds of branch:
- Local branch
- Remote branch
- Remote-tracking branch
Also be sure that local branches and Remote-tracking branches are ones on your local environment, whereas remote branches are on a remote server. if you are not familiar with this concept, please revisit the Git document site first. That is fundamental idea in git development.
1Deleting a local branch
To delete a local branch, just use “-d” option to the git branch command. This is very easy.
// Git command to delete a local branch git branch -d <branch name> git branch ---delete <branch name>
As is often the case, when the branch you are removing from your repository is not fully merged yet, you get the error message.
$ git branch -d cool-feature warning: deleting branch 'cool-feature' that has been merged to 'refs/remotes/origin/cool-feature', but not yet merged to HEAD. Deleted branch cool-feature (was 8e23461).
Hmm, this is uncool… Now, if you know that the branch is really unneeded anymore, and you are forcing to delete the branch, use the “-f” option altogether. This is the shorthand for “--force”. See the three commands below with same effect:
// Git command to force deleting a local branch git branch -df <branch name> git branch --delete --force <branch name> git branch -D <branch name>
Maybe the last one is the most handy since it’s shortest. This is just the shortest-had command to force deleting the local branch.
2Deleting a remote branch
If you are deleting a remote, the command will be a little bit different than the above. You need use “git push” command, not “git branch” command. The two commands below look different but work exactly the same way.
// (1) Git command to delete a remote branch git push ---delete origin <branch name> // (2) Git command to delete a remote branch git push origin :<branch name>
The former with (1) must be pretty understandable for anyone.
As for the latter with (2), it looks a bit tricky. You notice “:” in front of the parameter of a branch name. This means that “You are pushing BLANK(nothing) to the remote’s corresponding branch”, which is considered removing the remote branch with the specified name. That’s pretty cool, isn’t it?
Again, when you are changing something on a remote server, mostly you need to use “git push”. This is the command for doing something remotely on the server.
3Deleting a remote-tracking branch
When you have deleted a remote branch, don’t forget about removing the corresponding remote-tracking branch left on your local environment. This is how you do for it; use “-r” option, the shorthand of “--remote”, to operate on a remote-tracking branch.
// Git command to delete a remote-tracking branch git branch -dr <branch name> git branch ---delete --remote <branch name>
easy way to keep your environment clean
If you are working on a team, you get a lot of feature branches on your remote repository since your team members keep pushing their own feature branches.
and even if someone has deleted an obsolete branch on the server, the remote-tracking branch for it remains on your local environment. This commands is handy in terms of making your repository keep track of the existence of those branch on the server.
This removes remote-tracking branches from your local env if they are no longer on the server.
// Remove the remote-tracking branches on your local if it doesn't exist git fetch --prune origin