Deleting remote branches in git: understanding it for once and for all
Asked Answered
M

1

5

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?
Mika answered 1/2, 2014 at 13:59 Comment(1)
Git documentation has a very bad habit of using the same word with different meanings. It's supposed to be "obvious from context", but it is not always so, and for those new to git (or who use it lightly) it may never become obvious.Noble
M
3

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:

  • a simple branch
  • a remote tracking branch (one created by a fetch)
  • a local tracking branch (because it has an upstream branch associated to it)

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
Microreader answered 1/2, 2014 at 14:12 Comment(4)
So am I correct in that git push origin :topic1 will not remove the tracking reference? So what's the "complete" command to run to clean up the tracking reference as well? Would be nice to have one clean command for both.Mika
@RobertDailey yes, the remote tracking branch remains, as I mentioned here: https://mcmap.net/q/1174473/-deleting-a-remote-branchMicroreader
@RobertDailey So you need 2 commands, but as I mention in the edited answer, git remote prune can help.Microreader
Thanks VonC, I amended my question to clarify that the term 'remote' seems overloaded, and that is the reason for my confusion, so hopefully people will read that and it will help them in the future. Your answer is excellent, thank you very much!Mika

© 2022 - 2024 — McMap. All rights reserved.