How To Delete A Git Branch Locally And Remotely

Sarath Pillai's picture
Deleting a git branch from local as well as remote

The one single tool that dominates the version controlling space is none other than GiT. There are many reasons for its popularity and adoption. It is distributed in nature, it is decentralized, you can do almost every operation locally (meaning connectivity is only required while pushing the changes).

Another killer feature git offers is “branches”. Although many other version controlling tools offer branching feature, none of them implemented branching in the manner git implemented it (it is extremely light weight). Unlike other version controlling tools, switching between branches is lightning fast in Git.

 

This article will dig through the delete operation of a branch in git. Different people have different opinion about deleting a branch. Being said that, it is a common practice among users of git, to delete branches that have been merged to main line development (master branch in most cases..Well it depends on your git repository structure.)

 

Deleting a branch is a good idea because…

  1. It reduces clutter in your repository

  2. Once your changes from a branch is merged to the main line of development, it actually is quite useless, unless you are actively using it for further development.

 

Being said that..Keep in mind that deleting a branch won't improve performance or disk utilization (this is because git branches are nothing but pointers to a specific commit id - the last commit id of the source branch from where you created the new branch)

 

Let us create a repository with few test files, and also create and delete few branches.

 

Clone the Repository and create few branches

The below will clone an existing repository named "test-repo". Well this a repository i created for pure illustration purposes. It has just one single branch (ie: the default branch master), and 2 commits in that branch.

 

$git clone https://gitlab.example.com/sarath/test-repo.git
Cloning into 'test-repo'...
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), done.

 

Once we clone the repository, let us get inside the test-repo directory, and see the commits and current branch we are in.

 

$git log --oneline
0807c9a (HEAD -> master, origin/master, origin/HEAD) Adding test files a b c
ebd0b69 Initial commit

The HEAD is pointing to master, meaning we are currently inside master branch. And the above command also shows us that there are two commits in this master branch, with their respective commit messages.

 

Let us create one branch now inside this repository. This can be achieved by running the below commands.

 

$ git checkout -b testbranch1
Switched to a new branch 'testbranch1'

 

The above shown command will create a branch named testbranch1, and will take you inside that new branch. Any work done from this point onwards will be part of this new branch. Actually creating a branch and getting inside that branch is two seperate commands.

The command "git branch testbranch1" will create the branch

The command "git checkout testbranch1" will take you inside that branch

The command "git checkout -b testbranch1" is a shortcut to create a branch and get inside that new branch - Basically one single command instead of the above two.

 

We can confirm whether we are indeed inside our new branch by running any of the below commands. The HEAD should now be pointed to testbranch1 rather than master as we saw earlier.

 

$ git log --oneline
0807c9a (HEAD -> testbranch1, origin/master, origin/HEAD, master) Adding test files a b c
ebd0b69 Initial commit
$ git branch
  master
* testbranch1

 

 

Now let us do some changes and make a commit.

 

$ touch d e f
$ git add .
$ git commit -a -m "Adding test files d e f"
[testbranch1 1a0751b] Adding test files d e f
 3 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 d
 create mode 100644 e
 create mode 100644 f
$ git push origin testbranch1
Counting objects: 2, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 297 bytes | 297.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To https://gitlab.example.com/sarath/test-repo.git
 * [new branch]      testbranch1 -> testbranch1

 

As shown above, we created 3 new test files, and added it and committed them and then later on pushed it. The push operation will create this new branch with its relevant history in the remote repository as well.

 

Once you have your changes from testbranch1 merged to master branch, you should be able to delete the testbranch1 from local as well as remote as it is pretty useless if you are not going to work on it afterwards.

Let us go ahead and merge our testbranch1 to master. This can be done by first switching to master branch, and then executing the merge command. See below.

 

$git checkout master
$git merge testbranch1

 

Well if you are using something like github or gitlab, directly merging without pull request and review is not permitted. I am not using any pull request and review process here, as this is purely for illustration.

 

Now our master branch should have all the changes from testbranch1. We can now delete our testbranch1 from local as well as remote.

 

How to Delete a GiT branch Locally?

You can delete a git branch locally by executing git branch command with -d option. Remember, this will only delete the branch from local repository. See below.

 

$ git branch -d testbranch1
Deleted branch testbranch1 (was 1a0751b).

 

 

There is an interesting aspect to the above shown command. It will only delete the branch, if the contents of that branch (testbranch1) is merged to the original branch from where it was created (ie: master branch in our case)

If the changes are not merged to the original branch from where it was created, it will throw an error that looks like below.

 

error: The branch 'testbranch1' is not fully merged.
If you are sure you want to delete it, run 'git branch -D testbranch1'

As mentioned in the output above, you can still use -D option to forcefully delete a branch that has changes that are yet to be merged.

 

How to Delete a GiT Remote Branch?

As mentioned earlier, the above shown method will only delete the branch from local repository. To delete a branch from remote repository using command line, you need to execute a git push command as shown below.

 

$ git push origin :testbranch1
To https://gitlab.example.com/sarath/test-repo.git
 - [deleted]         testbranch1

 

Alternatively, you can also fire the below command, which does the same thing.

 

$ git push origin -d testbranch1
To https://gitlab.example.com/sarath/test-repo.git
 - [deleted]         testbranch1

 

 

One important thing to keep in mind is this. If there are servers or other machines that is tracking this repository (from which we deleted a branch), either they have to clone the repository again, or do a prune operation with git fetch command.

Rate this article: 
Average: 3 (882 votes)

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.