How to delete remote branches in Git

How to delete remote branches in Git

ยท

3 min read

Hello folks ๐Ÿ‘‹ While working with Git, it is possible that you come across a situation where you want to delete a remote branch or just forgot command for it. There is command in git that remove branches from our local machine or remove remote branches. Before jumping into how to delete remote branch, letโ€™s revisit how you would go about deleting a branch in the local repository with Git.

Deleting local branches

  1. First, we print out all the branches (local as well as remote), using the git branch command with -a (all) flag or to check local branches on machine use git branch.
  2. To delete the local branch, just run the git branch command again, this time with the -d (delete) flag, followed by the name of the branch you want to delete (test branch in this case).
# print local branch
git branch
* master
  develop
  latest-bugs
# print all branch (local & remote)
git branch -a
* master
  develop
  latest-bugs
  remotes/origin/HEAD -> origin/develop
  remotes/origin/develop

git branch -d latest-bugs
# Deleted branch latest-bugs

Note: You can also use the -D flag which is synonymous with --delete --force instead of -d. This will delete the branch regardless of its merge status.

Deleting remote branches

To delete a remote branch, you canโ€™t use the git branch command. Instead, use the git push command with --delete flag, followed by the name of the branch you want to delete. You also need to specify the remote name (origin in this case) after git push.

git branch -a
* master
  develop
  latest-bugs
  remotes/origin/HEAD -> origin/develop
  remotes/origin/develop

git push origin --delete develop
To https://github.com/sample-example.git
 - [deleted]         develop

That's it these simple command are useful for deleting both remote and local branches. if you like this article please let me know in comment section or follow me on Twitter ๐Ÿฅ