Aim : To delete a remote branch named 'branchname'
Steps I used to do was: [first approach]
Today I tried to delete using the same above steps, but encountered an issue as mentioned below:
$ git branch -d branchname
warning: deleting branch 'branchname' that has been merged to
'refs/remotes/origin/branchname', but not yet merged to HEAD.
Deleted branch branchname (was f394ddc).
prash ~/folder/project1 (branch1)
$ git push origin branchname
Enter passphrase for key '/c/Users/prash/.ssh/id_rsa':
error: src refspec branchname does not match any.
error: failed to push some refs to 'ssh://[email protected]/projname.git'
Finally I was able to delete through a different approach, [second approach]
$ git push origin --delete branchname
Question,
Do you know why I could not delete the remote branch as per first approach ? Also, why that warning?
edit:
I tried to replicate the first issue by creating and deleting a remote branch, and the only change I made this time is adding a colon before the branchname I missed out early, and it got deleted. Not sure if that was the reason earlier.
In your first example, the reason why the push failed is because you did
git push origin branchname
instead of
git push origin :branchname
The colon is significant; it's the difference between pushing a branch and deleting a branch. The one-character difference here is why git push origin --delete branchname
is a much safer command to use, in general.
Since you had deleted branchname
, when you tried to push it, git
said "I don't know what branchname
is because I don't have a branch with that name" and failed to push anything.
You might be thinking that deleting the branch locally just marked that branch as "deleted", and so when you push it, origin
's version of branchname
also gets marked as "deleted", but that's not how git
deletes branches. When you delete a branch locally, the branch is gone forever (though the commits that belonged to that branch remain, and so you can recover it by looking through git reflog
).
As for the initial warning about HEAD
, git
is trying to ensure that you don't lose any data.
Every branch can have an "upstream" branch associated with it, so that you can use git push
and git pull
without having to specify an explicit remote and an explicit branch. When you delete a branch, git
checks with the upstream branch to see if your local branch has commits that do not exist in the remote upstream branch. You will not lose any data by deleting a branch that is identical to a remote, because you can just check it out again. You can lose data if you delete a branch that contains commits that haven't been pushed upstream, so git
tries to help you out by warning you if you delete a branch with unpushed commits.
It's just a warning though, because there are plenty of situations where you don't care about some commits. They could have been failed experiment, non-rebased versions of merged branches, or some other legitimate situation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With