How to delete Local and Remote Git Branch


In this article, you will learn the basics about how to remove local and remote branches in Git. To delete both a local and a remote Git branch, you can follow these steps:

1. Deleting the Local Branch

You can delete a local Git branch using the git branch command. Make sure you are on a different branch before attempting to delete the branch you want to remove.

git branch -d branch_name

If the branch contains changes that haven’t been merged, Git will refuse to delete it with the -d option. In such cases, use the -D option to force the deletion:

git branch -D branch_name

Replace branch_name with the name of the branch you want to delete.

2. Deleting the Remote Branch

To delete a remote branch, you need to use the git push command with the --delete flag. This removes the remote branch from the remote repository.

git push origin --delete branch_name

Replace origin with the name of your remote repository (it’s typically origin by default), and branch_name with the name of the branch you want to delete on the remote.

Here’s a summary of the commands:

  • Delete the local branch: git branch -d branch_name (or -D if necessary)
  • Delete the remote branch: git push origin --delete branch_name

Make sure you are certain about deleting branches, especially remote ones, as it’s not easy to recover them once they are deleted.

References

  1. Git Squash: Squashing Commits for a Cleaner History
  2. Git Pull vs Fetch
  3. Git Merge vs. Rebase
  4. Branch – GitDoc

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.