The reason why I'm posting here today is because the behavior I see on the command line differs from what I actually read online. So it confuses me.
The two commands under question here are:
git branch -r -d origin/topic1
and
git push origin :topic1
My understanding so far (I may be completely wrong though, my brain is fried):
Here is a recent scenario I have run into. I just cloned a repository with two remote branches:
origin/master
origin/develop
The only local branch I have is:
master
I want to delete the remote branch origin/develop
, so watch this:
Robert@COMP /c/Code/project (master)
$ git branch -rd origin/develop
Deleted remote branch origin/develop (was 9ff16e8).
Robert@COMP /c/Code/project (master)
$ git fetch
From github.com:username/project
* [new branch] develop -> origin/develop
As you can see, I tried to delete the remote branch, and immediately fetch the latest changes from origin, but for some reason it recreated the branch. I have no idea why it would do this, I'm very confused. I don't have a local corresponding branch for origin/develop
, so I don't know why it is doing this.
I'd like to know why this happened, but also (to address the more general title of this question, and to perhaps help everyone else scavenging stack overflow for answers to these confusing ambiguities), I'd like more general answers to some questions:
The first should remove both the tracking reference and the remote branch
No: it is a local operation, so it can only delete the remote tracking branch (which is within your repo)
The remote tracking branch is here to remember the last SHA1 fetched from the branch on the remote repo.
If you want to delete that branch (on the remote repo), you need to tell that remote repo (hence the git push :topic1
)
A local branch (one within your repo) can be:
As mentioned in "How can I delete all git branches which are already merged?", once you have delete several branches of a remote repo, you can prune all the remote tracking branches of your local repo with a:
git remote prune origin