Git delete a branch remotely
Git delete a branch from the local and remote repository
While working with Git repositories, very often you will create and need to delete
a lot of branches. Some branches will be merged into the master branch and some are
no longer necessary. In either case, at some point they need to be deleted. We want
to keep the repository clean and neat after all.
Deleting a local branch in a Git repository is quite straight forward. You can use
either of these commands to do it:
1
2
3
4
5
git branch -d <branch_name>
git branch -D <branch_name>
To delete a branch in the remote repository, you can do something very similar:
1
2
3
4
5
6
7
8
9
10
11
git push -d <remote> <branch_name>
# <remote> is the remote name, very often 'origin'
git push -d origin <branch_name>
The difference between -d
and -D
Both -d
and -D
are aliases:
-d
is an alias for--delete
. This deletes a branch only if the branch
has already been fully merged in its upstream branch. This is very useful when
using git workflows,
to prevent unintentional mistakes of deleting a branch that is intended to be merged.
Once merged, however, the branch can be safely deleted.
-D
is shortcut for--delete
--force
. This deletes the branch regardless
of the merge state of the branch. This is useful for deleting branches that are
not expected to be merged.
## Deleting remote branch on Github repository
Very often you need to remove a branch on a remote repository on Github.
The regular process of creating a Pull Request,
once the branch has been merged, offers an action to delete the branch via the web
interface:
In the branch that should be deleted was not merged via Pull Request, then you can
remove it from the command line by pushing to the remote repository:
1
2
3
git push -d origin <branch_name>