1

I have only master branch on my remote origin.

Then I did:

git fetch origin refs/heads/master:refs/remotes/origin/master2

as a result I got:

* [new branch]      master     -> origin/master2

which seems all right.

It is shown as remote tracking branch with the master:

bash$ git branch -r
origin/HEAD -> origin/master
origin/master
origin/master2

but master2 is shown as :

bash$ git remote show origin
  Remote branches:
master                      tracked
refs/remotes/origin/master2 stale (use 'git remote prune' to remove)

I. My first question is why master2 is shown as stale? I was able to fetch to it (and create it as my local remote tracking one) and I would expect that it will be mapped to remote origin/master?

II. Second question is why I have to do:

bash$ git branch -r -d origin/master2

to delete it and got error when trying to do it by giving full refspec:

bash$ git branch -r -d refs/remotes/origin/master2
error: remote-tracking branch 'refs/remotes/origin/master2' not found.

I've checked man of git-branch and found there nothing special about branch name:

<branchname>
       The name of the branch to create or delete. The new branch name
       must pass all checks defined by git-check-ref-format(1). Some of
       these checks may restrict the characters allowed in a branch name.
user1337
  • 175
  • 7

1 Answers1

0

See my previous answer "What is a “stale” git branch?": you are creating a local master branch tracking a remote branch named master2.
But master2 does not exist in the origin repo.
Hence the stale attribute.

The fetch does not fetch any commit from the remote non-existing branch master2, but does create a local master branch tracking origin/master2, which means the next git push would push (and create) origin/master2.

As I mention here, a simple git fetch origin -p --progress would remove origin/master2.

As mentioned by the OP in the comments, the chapter "Git Internals - The Refspec" does include:

If you want to do a one-time only fetch, you can specify the specific refspec on the command line, too.
To pull the master branch on the remote down to origin/mymaster locally, you can run:

$ git fetch origin master:refs/remotes/origin/mymaster
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for quick reply. As I understand after first push master2 will be created on remote, after that my local refs/remotes/origin/master2 will loose 'stale' status but both origin/master2 and remotes/origin/master2 will be empty ones? – user1337 Dec 26 '17 at 20:57
  • @user1337 yes, after a first push, `master2` will lose the 'stale' status. But it won't be empty: it will have at least one commit from `master`. – VonC Dec 26 '17 at 20:58
  • I found it in: https://git-scm.com/book/en/v2/Git-Internals-The-Refspec where you can find: If you want to do a one-time only fetch, you can specify the specific refspec on the command line, too. To pull the master branch on the remote down to origin/mymaster locally, you can run: $ git fetch origin master:refs/remotes/origin/mymaster – user1337 Dec 26 '17 at 21:19
  • @user1337 Good catch: I have included your comment in the answer for more visibility. – VonC Dec 26 '17 at 21:24