Enroll in Selenium Training

The last tutorial briefed us about the creation of branches and how to switch between them swiftly. In computer technology, if you can create something, the technology also provides a way to remove/delete that. It stands as the crux of this tutorial. We are going to learn how to delete a branch in Git along with:

  • Why delete branches?
  • Git Delete Branch
    • How to delete a Local Branch?
    • How to delete a Remote Branch?

Why Delete Branch?

I am sure this thought must have come to your mind while starting this tutorial. "Why should you delete a branch in Git?" Or maybe "When should you delete a branch in Git?"  It's quite natural to get these questions in mind. Additionally, one should know the logic before actually proceeding on the "How" part.

Consider branches as a thought, as an idea. Assume that you are working on some project, and you get an idea about a new feature. You created a new branch and started working on it. After implementing your idea, testing it, and merging it with the master, you finish the work on that branch. It is a straightforward process. You can go ahead and delete the newly created feature branch from the repository. Keeping the branches stuck to your account serves no real purpose in your project journey. If nothing, they increase the burden of complex branch chains that will develop as you move deep into the project.

One might think that the branches would help a user keep track of the history and would take you back to a feature whose development happened long ago if any historical context is required. In real life, that is true. You never know when you might need to go back to a feature developed long ago. But, one can also handle that problem with the help of "tags" in Git. We have a completed chapter dedicated to tags that you can explore. In brief, a tag requires to label a commit so that you can go back to it, explore it, and remember why that commit happened.

The following sequence of images shows the same scenario. A user has two branches on which they are working simultaneously.

Create Two feature branches from Master

Once the user finishes everything, they merge Branch 1 to the master branch.

merging one feature branch to master branch

The user can now delete the branch and continue with the branches that are currently in development.

Delete branch

We can now proceed to delete the branches from our local system. It is to note that the deletion of branches does not lead to any code loss (except if you have not merged them).

Delete Branch In Git

In this section, we will discuss the deletion of branches from our local systems and push them to the remote repository. In addition to it, we will also highlight the situation about deleting the branch from our remote repository.

How To Delete A Local Branch?

Before we head to remove a local branch, first, list out all the branches in the local repository with the command:

git branch

Display all the local branches

Now we can proceed ahead. To delete a branch on your local system, follow these simple steps:

Type in the following command:

git branch -d <branch_name>

command to delete branch in git

Note: The "d" flag used here specifies that we intend to delete a branch.

Notice that we are currently on the "prod" branch and trying to delete the same branch through the command. Execute the command to see if it works.

branch delete error

Git gives us an error that the branch cannot delete. This error arose because we are trying to delete a branch on which we are working. Git suggests us "check out" from this branch and try deleting again. You can try the same as a practice. It will work smoothly.

Alternatively, the user can also use git branch -D <branch_name> to force delete the branch without checking the merged status of the branch. Doing so, Git won't give any warnings even if the branch does not merge in.

Execute the command to delete the branch named prod.

deleted branch successfully

Recheck the available branches locally by typing the following command:

git branch

List local branches

Alright! We have successfully deleted the "prod" branch. But this is done only on the local machine. You can also try and delete a branch on the remote repository through the local machine.

How To Delete A Remote Branch?

Before we head to remove a remote branch, we should view all the branches in the remote repository. Type in the following command and execute:

git branch -a

Display all the remote branches

As seen, there are many remote branches associated with this repository, among which the last one is the prod branch. We will try to delete the same branch in our next steps.

You can go ahead with any branch of your choice; in this part, we will be moving forward with deleting the "prod" branch. For this, execute the following command:

git push <remote_repo_name> --delete <branch_name>

delete remote branch

Execute the command to delete the branch named prod from the remote repository.

remote branch deleted success

As soon as the branch deletes, we receive the success message from Git:

[deleted]        <branch_name>

You can also confirm the same by listing all the remote branches again.

List all the remote branches

Git confirms that the deletion of the branch. So now, we have learned how to delete a branch locally and remotely through Git. It brings us to the end of this tutorial. In the next tutorial, we will talk about merging and creating pull requests. You can also explore the GitHub section of this course to know more about deletion, creation, and pull requests with the help of GitHub remotely. Till then, keep practicing.

Practice Excercise:

  • Create a branch locally by the name "Bug-Patch".
  • Switch to the branch and make some changes by creating a new file etc.
  • Commit these changes and look at the Git Logfile of the "Bug-Patch" branch.
  • Push this branch to the remote repository.
  • View all the remote and local branches to confirm.
  • Delete this branch locally from Git.
  • Delete this branch from a remote repository.

Common Problems On Deletion In Branches

Does deleting the branch also deletes the associated commits?

No, branches are the mere references to the commit. Deleting a branch does not have any effect on the commit, and they will still be available in the repository.

Can the master branch be deleted?

The master branch is just a type of branch in the repository, which is also the default branch by default. But, as a rule in Git, default branches cannot be deleted. So, to delete the master branch first, the user has to change the default branch. (Refer How to change the default branch in Git).

Can we recover the deleted branches? Yes, you can recover the deleted branches via git reflog command. Reflog is short for Reference Logs, which records the points when the updating of the branches and other references happened in the repository. One can recover the branches by going back to a particular log.

Git Create Branch
Git Create Branch
Previous Article
Merge Branch In Git
Merge Branch In Git
Next Article

Similar Articles