git: push deleted branch to remote

spiderman

Aim : To delete a remote branch named 'branchname'

Steps I used to do was: [first approach]

  1. git branch -d branchname
  2. git push origin :branchname

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.

tbekolay

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Git branch shows deleted remote branch

Git remote branch deleted, but still it appears in 'branch -a'

Git Push to a remote branch with directory

How to recover deleted remote branch in Git

git delete local branch if remote is deleted as alias

How to identify a deleted remote branch in Git?

git recover branch deleted on remote and local folder deleted

Git Push From One Remote Branch To Another Remote Branch

git, How to push local branch into the specific remote

git push complete and separate branch to remote repository

Git push diverges the remote and local branch

Git Push given Local and Remote Branch Name

git push a detached head to a dev branch of remote

Git push from fork to specific remote branch?

Git push branch from one remote to another?

Heroku push updates app, but not remote git branch

push new git branch to remote repo on eclipse

Git push master branch to secondary remote

How to push to a remote git branch on Gitlab?

Git: Push code to the remote master branch

Git push not showing new branch on remote

git push origin in another remote branch by mistake

In PyCharm terminal "git branch -a" shows remote git (Github) repositories that are deleted

How do I push a local Git branch to master branch in the remote?

From a local branch, git push to a different remote branch

Git remote branch deleted by mistake:any loss of information?

Deleted remote git branch still showing up on a different clone

Are local git branches ever deleted automatically when their remote branch is removed?

git - deleted remote branch head causes pull error