Git

How to Delete a Git Branch Locally?

Git branches are the basic unit of the Git repository that allows programmers to try different code versions, test modules, and even work concurrently in different contexts. To keep your development environment clean, you must remove the branches you do not need. Additionally, it helps to minimize the complexity of the project.

In this blog, we will demonstrate the method to delete the Git branch locally.

How to Delete Git Branch Locally?

To delete the Git branch locally, users can utilize the “git –delete” or “git -d” commands. For the specified purpose, check out the below-provided steps.

Step 1: Open Git Bash Terminal
First, type “Git Bash” in the Start menu and launch the Git Bash terminal:

Step 2: Move to Git Local Repository
By utilizing the “cd” command, move to the required Git local repository:

$ cd "C:\Git"

Step 3: List Down Git Local Branches
To view all local branches, check out the provided command:

$ git branch

Step 4: Create New Branch
Create a new Git branch through the “$ git branch” command also provide the branch name:

$ git branch Second-branch

Again, list down the local branches to verify whether the new branch is created or not:

$ git branch

Here, you can see that the “Second-branch” is created successfully:

Step 5: Delete Branch
To delete the Git branch again, utilize the “git branch” command with the “–delete” option:

$ git branch --delete Second-branch

Step 6: Verify Branch Deletion
Next, verify whether the branch is deleted or not by listing down all local branches:

$ git branch

The below output indicates that the “Second-branch” is successfully deleted:

Alternatively, the user can utilize the “-d” option instead of the “–delete” option:

$ git branch -d first

We have taught you how to delete a Git branch locally.

Conclusion

To delete the Git branches locally, first launch the Git Bash terminal and move to the Git repository. List down all local branches using the “$ git branch” command. Then, delete the unused or extra branch using the “$ git branch –delete” command in the terminal and again list down branches to check if the selected branch is deleted or not. This post has elaborated on the method for deleting a Git branch locally.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.