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):
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:
Thanks everyone! Please, save my hair!
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!
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
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