I'm really going crazy over this. Quite frankly I'm not sure why I'm having trouble understanding this as it should be simple, right? First of all I've done tons of searching on this. I've read a bunch of stuff on stackoverflow, I've read blog articles found through lots of Google searching. However some details are still unclear, despite however duplicated and simple this question may seem, so my apologies in advance to the people that are hovering over the downvote/close buttons. 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):
- The first should remove both the tracking reference and the remote branch
- The second should remove the remote branch but NOT the tracking reference (if a local branch exists), although the reason why you wouldn't remove the tracking reference seems pointless and confusing to me as a sort-of-beginner.
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:
- There are two ways to delete a remote branch. What is a really good, simple way to remember which one to use under which circumstance?
- Assuming it matters at all, how does the existence of a corresponding local branch (for a remote branch) affect the decision of how a remote branch is deleted?
- How does the existence of a tracking reference/relationship between a local and remote branch affect how you choose to delete a branch, and which of the methods of deleting a branch also cleans up tracking references?
Thanks everyone! Please, save my hair!
EDIT (summary of answers)
Based on the answers below, I was able to get clarification on the one piece that was confusing everything about this. The documentation for the -r
option for git branch
states:
List or delete (if used with -d) the remote-tracking branches.
The confusion for me was with what is considered "Remote". Remote to me, initially, meant "origin"... what was on "github" in my case. However, in your LOCAL clone, github has its own concept of what a remote is, and that's its own copy of the remote branches. So basically it seems that deleting a remote branch involves two steps: Deleting the origin's branch (this is the git push origin :topic1
) and then deleting the remote tracking branch (the git branch -rb origin/topic1
).
That helps my understanding, although because the word "remote" has two meanings here, it makes it VERY confusing to communicate this to people, so I hope this amendment to my question helps people in the future. Thanks to everyone who answered and who will continue to contribute to this question in the future!