This tutorial explains how to delete both local and remote branches in GitHub.
First, run the command git branch -a
or git branch --all
to list both local and remote branches.
In the example below, the repository is on the featuredev
branch:
B:\ch12>git branch -a
* featuredev
main
remotes/origin/HEAD -> origin/main
remotes/origin/featuredev
remotes/origin/main
From the above command, we have the following list of branches:
Local Branches: featuredev
, main
Remote Branches: remotes/origin/featuredev
, remotes/origin/main
Deleting a Local Branch in Git
To delete a local branch in Git, follow these steps
First, checkout the local branch you want to delete
B:\ch12>git checkout featuredev
Already on 'featuredev'
Your branch is up to date with 'origin/featuredev'.
Now, you can delete the local branch in multiple ways.
Using the
--delete --force
optiongit branch --delete --force localbranch
Using the -D option, where
-D
is an alias for--delete --force
git branch -D localbranch
Using
--delete
git branch --delete localbranch
Deleting a Remote Branch in Git
First, list out all remote branches using the git branch -r
or git branch --remotes
command
B:\ch12>git branch -r
origin/HEAD -> origin/main
origin/featuredev
origin/main
With recent Git versions, you can delete using the --delete
option with git push.
It deletes the remote branch and pushes the changes.
git push origin --delete branchname
or
git push origin -d branchname
Here, -d
is an alias for the --delete
option.
With older versions of Git, you can use.
git push origin:branch
For example:
B:\ch12>git push origin --delete featuredev
To git.com:user/ch12.git
- [deleted] featuredev
This removes the remote branch and its references from the local repository. However, if other developers are using this remote repository, the references are still pointing to the remote repository, and a deleted branch is shown to them.
To delete from other developer systems, use the following command:
git remote prune featuredev