×

Search anything:

Git delete remote branch [2 methods]

git

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

Several collaborators work on a Git repository and often, changes are merged which should not be merged. Sometimes, development branches are created which when merged with master branch later, the development branches may be deleted to keep the repository clean.

In this article, we have explored 2 different ways to delete a remote branch in Git repository.

Table of contents:

  1. Git delete remote branch directly
  2. Git delete branch locally and update remote

In short, the command to delete a remote branch is as follows:

git push origin -d <branch-name>

Remote branch refers to the branch that exists in the remote repository so your cloned repository will have the same branch. On deleting a remote branch, the corresponding local branch is also deleted.

Git delete remote branch

Git delete remote branch directly

The command to delete a remote branch is:

git push origin -d <branch-name>

This command is a shorthand notation of two commands combined together which are:

git branch -d <branch name>
git push origin

Note that this will give an error if you have uncommitted changes in the concerned branch locally. This is the easiest way to delete a branch:

  • Clone the remote repository
  • Delete the remote branch directly

Git delete branch locally and update remote

To understand how Git branches are deleted locally, go through this article which explains it step by step.

The second approach to delete a branch is to delete it locally first and then, update the remove repository that the branch is deleted. The commands are as follows:

git branch -d <branch name>
OR
git branch --delete <branch name>

OR use the following commands to delete the local branch if you have uncommited changes in the branch:

git branch -D <branch name>
OR
git branch --delete --force <branch name>

Once deleted, update the remote repository as follows:

git push origin

This command will delete the corresponding remote branch in the remote repository. This approach involves multiple steps but is preferred if you want to make changes in your local repository first and then, apply to the remote repository. This is the standard approach.

With this article at OpenGenus, you must have the complete idea of how to delete a remote branch in a git repository.

Git delete remote branch [2 methods]
Share this