Git Delete Branch – How to Remove a Local or Remote Branch

Git is a widely used version control system and a crucial tool for web developers.

Branches are a key feature of Git, offering powerful capabilities for managing code.

In this article, you’ll learn the essentials of how to delete both local and remote branches in Git.

What are Branches in Git?

A branch in Git is a reference to a specific commit.

Git branches represent snapshots of a project and its changes at a particular point in time.

In a large project, there is usually a central repository containing all the code, commonly referred to as the “main” or “master” branch.

Branching enables you to create separate, independent versions of the main project. You might create a branch to make changes, add a new feature, or write tests while fixing a bug. This allows you to work without impacting the main code.

In summary, branches allow you to modify the codebase without impacting the core code until you’re ready to implement those changes.

This helps maintain a clean and organized codebase.

Why Remove Branches in Git?

You created a branch to manage the code for a change you intended to make in your project.

Next, you merged that change or new feature into the main version of the project.

Since the branch is no longer needed, it’s considered best practice to delete it to avoid cluttering your codebase.

How to Delete a Local Branch in Git

Local branches are created on your machine and do not impact any remote branches.

The command to remove a local branch in Git is:

git branch -d  local_branch_name
  • The git branch command is used to delete a branch locally.
  • The -d flag is an option for the command, which stands for --delete. It indicates that you want to remove the specified branch. Finally, local_branch_name is the name of the branch you wish to delete.

Let’s explore this in more detail with an example.

To display all the local branches, use the following command:

git branch

I have two branches: master and test2. I am currently on the test2 branch, as indicated by the (*):

Screenshot-2021-08-25-at-4.13.14-PM

I want to delete the test2 branch, but it’s not possible to delete the branch you’re currently on and viewing.

If you attempt to do so, you’ll encounter an error that looks something like this:

Screenshot-2021-08-25-at-4.17.50-PM

Before deleting a local branch, make sure to switch to another branch that you don’t want to delete using the git checkout command:

git checkout branch_name

#where branch_name is the name of the branch you want to move to
#in my case the other branch I have is master, so I'd do:
#git checkout master

Here’s the output:

Screenshot-2021-08-25-at-4.20.40-PM

Now, I can delete the branch:

Screenshot-2021-08-25-at-5.10.13-PM

The command we just used to delete a local branch doesn’t work in all situations.

If the branch contains unmerged changes or unpushed commits, the -d flag will prevent the local branch from being deleted.

This is because the commits are not referenced by any other branches, and Git is protecting you from accidentally losing any commit data.

If you attempt this, Git will display an error:

Screenshot-2021-08-25-at-5.23.46-PM

As the error indicates, you’ll need to use the -D flag instead.:

git branch -D local_branch_name

The -D flag, with a capital D (an alias for --delete --force), forcefully deletes the local branch, regardless of its merge status.

However, note that you should use this command with caution, as it does not prompt you to confirm your actions.

Only use it when you are completely sure you want to delete a local branch.

If you haven’t merged it into another local branch or pushed it to a remote branch, you risk losing any changes you’ve made.

Screenshot-2021-08-25-at-5.33.41-PM

How to Delete a Remote Branch in Git

Remote branches are distinct from local branches.

They are repositories hosted on a remote server, accessible from there, in contrast to local branches, which reside on your local system.

The command to delete a remote branch is:

git push remote_name -d remote_branch_name
  • Instead of using the git branch command for local branches, you can delete a remote branch with the git push command.
  • Next, specify the name of the remote, typically origin.
  • The -d flag is used for deletion and is an alias for --delete.
  • remote_branch_name refers to the remote branch you want to delete.

Now, let’s look at an example of how to delete a remote branch.

To list all remote branches, use this command:

git branch -a

The -a flag (an alias for --all) displays all branches, both local and remote.

Screenshot-2021-08-25-at-7.35.31-PM

I have two local branches, master and test, and two remote branches, origin/master and origin/test.

The -r flag (an alias for --remotes) displays only the remote branches.

Screenshot-2021-08-25-at-7.37.12-PM

To delete the remote origin/test branch, I use the following command:

git push origin -d test

Output:

Screenshot-2021-08-25-at-7.39.34-PM

This command deleted the test branch from the remote repository named origin.

The origin/test remote branch no longer exists:

Screenshot-2021-08-25-at-7.42.01-PM

Conclusion

Now you know how to delete both local and remote branches in Git.

If you want to learn more about Git, check out the following courses on freeCodeCamp’s YouTube channel:

  • To get started with Git, explore key Git commands, and understand the typical Git workflow, watch “Git and GitHub for Beginners – Crash Course.”
  • For a detailed understanding of branches and how they function, check out “Git Branches Tutorial.”

Thank you for reading, and happy learning!

Leave a Comment