Loading...

Git delete local branch – How to delete a local branch with git?

Git delete local branch – How to delete a local branch with git?

While working with multiple projects we often end up having a lot of branches in our repository. In this article, we will learn how to delete a local branch with git.

Git Branch

Let’s first revise the concept of the git branch.

In Git, a branch is a lightweight movable pointer to one of the commits. The default branch name in Git is master. As you start making commits, you’re given a master branch that points to the last commit you made. Every time you commit, the master branch pointer moves forward automatically.

Creating a new branch means you’re creating a new pointer to the same commit you’re currently on. You can think of it as a “fork” of the current branch.

Here is a simple visualization of the concept of the git branch:

Two branches main and feature

Till C2 commit there was just one branch. After that, we branched off from C2 to create feature-1 branch. Now we have two branches. We can make changes to both branches independently. This allows us to work on multiple features at the same time. We can merge the changes from feature-1 branch to master branch when we are done with the feature. Git branching also makes it easy to collaborate with other developers.

List all local branches

To list all local branches, run the following command:

git branch
Code language: Bash (bash)

This will display all the branches that you have locally. It will also highlight the branch you are currently on.

In my case, it shows that there are two branches, feature-2 and main. Since I am currently on feature-2 branch, it is highlighted with green.

Creating a new branch

To delete a local branch with git, first, we need to know how to create a new branch. To create a new branch, we can use the git branch command.

git branch <branch-name>
Code language: Bash (bash)

For example, to create a new branch named feature-1, we can use the following command:

git branch feature-1
Code language: Bash (bash)

Now, if we run git branch command, we will see that we have a new branch named feature-1

You can see initially I had only one branch named main. After creating a new branch named feature-1, I have two branches now.

Deleting a local branch

To delete a local branch, we can use the git branch -d command.

git branch -d <branch-name>
Code language: Bash (bash)

For example, deleting the feature-1 branch, we can use the following command:

git branch -d feature-1
Code language: Bash (bash)

Now, if we run git branch command, we will see that the feature-1 branch is deleted.

You can see that initially, we had two branches, master and feature-1. After deleting the feature-1 branch, we are left with just one branch, master.

Use the -d flag to delete a branch only if it has already been merged in its upstream branch, or in HEAD if no upstream was set with –track or –set-upstream-to. If you want to delete a branch regardless of its merged status, use the -D flag. It basically forces deletes the branch. But be careful while using this flag. You might lose some work if you are not careful.

git branch -D <branch-name>
Code language: Bash (bash)

For example, deleting the feature-1 branch forcefully, we can use the following command:

git branch -D feature-1
Code language: Bash (bash)

Important Note

You cannot delete a branch that you are currently on. You need to switch to another branch before deleting the branch.

Illustration of man cutting with saw the tree branch on which he is sitting.

Conclusion

In this article, we learned how to delete a local branch with git normally and forcefully. We also learned how to create a new branch and list all local branches.

Sharing is caring

Did you like what Arnab Sen wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far