How do I delete a Git branch locally and remotely?

To delete a Git branch both locally and remotely, you can follow these steps. I'll provide you with an example for each step:

  1. Delete Locally:

    Use the following commands to delete a branch locally:

    sh
# Switch to a different branch (if you're on the branch you want to delete) git checkout <another-branch> # Delete the branch locally git branch -d <branch-name>

If the branch contains unmerged changes, use -D instead of -d to force deletion:

sh
git branch -D <branch-name>

Example:

sh
  • git branch -d feature/new-feature
  • Delete Remotely:

    After deleting the branch locally, you need to delete it on the remote repository as well. Use the following command:

    sh
  • git push origin --delete <branch-name>

    Example:

    sh
    1. git push origin --delete feature/new-feature

    By following these steps, you'll delete the branch both locally and remotely. It's important to note that deleting a branch is a permanent action, so make sure you have the necessary backups or have pushed any important changes to another branch before proceeding.

    Comments