小能豆

How do I delete a Git branch locally and remotely?

git

Failed Attempts to Delete a Remote Branch:

$ git branch -d remotes/origin/bugfix
error: branch ‘remotes/origin/bugfix’ not found.

$ git branch -d origin/bugfix
error: branch ‘origin/bugfix’ not found.

$ git branch -rd origin/bugfix
Deleted remote branch origin/bugfix (was 2a14ef7).

$ git push
Everything up-to-date

$ git pull
From github.com:gituser/gitproject

  • [new branch] bugfix -> origin/bugfix
    Already up-to-date.
    How do I properly delete the remotes/origin/bugfix branch both locally and remotely?

阅读 302

收藏
2024-01-01

共1个答案

小能豆

To delete both the local and remote branch properly, you can follow these steps:

Locally:

  1. Delete the local branch:

git branch -d bugfix

or, if the branch has not been merged yet:

git branch -D bugfix

Remotely:

  1. Delete the remote branch:

git push origin --delete bugfix

or the shorter form:

git push origin :bugfix

Now, the branch should be deleted both locally and remotely.

Your failed attempts were close, but there are some key points to remember:

  • When deleting a remote branch, use git push origin --delete <branch-name> or git push origin :<branch-name>. The :branch-name syntax tells Git to delete the branch on the remote.
  • When deleting a local branch, use git branch -d <branch-name> to delete a merged branch, or git branch -D <branch-name> to force-delete a branch regardless of its merge status.

In your case, it seems you were trying to delete a remote branch, and using the correct command should solve the issue. Make sure to replace <branch-name> with the actual name of the branch you want to delete.

2024-01-01