This tutorial explains how to delete a local and remote branch in Github.
First, Run the command git branch -a
or git branch --all
to know the list of local and remote branches.
The below repository is on featuredev
B:\ch12>git branch -a
* featuredev
main
remotes/origin/HEAD -> origin/main
remotes/origin/featuredev
remotes/origin/main
From the above command, It has the following list of branches
Local Branches: featuredev
,main
Remote Branches: remotes/origin/featuredev
, remotes/origin/main
How to delete a local branch in git
To delete a local branch in Git, Please follow the below steps
First, Checkout the local branch that 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.
First way using --delete --force
option
git branch --delete --force localbranch
The second way using the -D
option, -D is an alias for --delete --force
.
git branch -D localbranch
Third way using --delete
git branch --delete localbranch
how to delete 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
-d
is an alias for the --delete
option.
with old versions of git
git push origin:branch
B:\ch12>git push origin --delete featuredev
To git.com:user/ch12.git
- [deleted] featuredev
It removes the remote repository and references from a local repository.
If any other developers are using this remote repository, The references are still pointing to the remote repository, and deleted branch is shown to them.
To delete from other developer systems, use the below command..
git remote prune featuredev