Delete Branches in Git using commands

In git workflow, the ideal strategy to work is to fork a new branch, make changes and finally merge in the main development branches. Over the time, this results in creation of large number of branches which are not required and becomes stale. Although a branch is just a pointer to an commit and does not require more than 40 bytes of disk space, it can be painful to search a long list of branches and deciding what you want to work on. Also since we humans are not good with creating unique names for branches, they can also result in confusion.

Below are some steps to clean branches from git repository to remove the clutter.

Delete a Local branch

To delete the local branch in Git using command, we can use one of the followings:

git branch -d branch_name
git branch -D branch_name

As you can see above, we have 2 different argument, one with small case ‘d’ and one with capital case ‘D’.

The -d option stands for –delete, which would delete the local branch. The -D option stands for –delete –force, which deletes the branch even if there are uncommitted changes.

Delete a remote branch

To delete a remote branch, we can use the following command:

git push {remote_name} –delete {branch_name}

Above command can also be used if we want to delete git tags.

What about the Connection

Do note that remote branch and local branches are totally separate objects in Git. They are sync’d to each other using a git connection which is again a separate object. Deleting local branch will also remove the connection information from the local machine. Also, deleting local branch does not mean that remote branch will get deleted on its own or vice-versa.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s