1

There is a branch on a remote git repository where ultimately I want to fetch locally, then merge the work in - nothing unusual there.
I suspect there may be a merge conflict - so rather than issue git pull origin/<branch_name>, I want to take a look at the code on that remote branch before I try merge it into My local branch.

So what I tried was:

  • git fetch origin I believe that means I now have a copy of the read only remote tracking branch, then I did

  • git checkout origin/<branch_name>

To try checkout and look at the contents of the remote branch.
It has worked, but I'm getting some messages like

  • HEAD is in a detached state
  • Previous HEAD position was c293198

and My vim fugitive is reporting the current branch as a hash fragment like a697b40.
Which makes me suspect I'm doing it wrong.
Is there a more "proper" way to inspect the contents of a remote branch before attempting a merge?

the_velour_fog
  • 2,094
  • 4
  • 17
  • 29
  • If your only worry currently is that git shows the HEAD at some commit sha instead of a name, do `git checkout -b name origin/` – hjpotter92 Dec 02 '15 at 06:43
  • @hjpotter92 That would actually create a new local branch. The OP doesn't want that. They just want to inspect it. Checking out the tracking branch directly is the way to go. – Joseph Silber Dec 02 '15 at 06:46
  • I just wondered if there was a more "procelain" command - that I was missing. having git report as a the HEAD as a commit sha is in fact probably helpful to alert to the fact im viewing a read only branch – the_velour_fog Dec 02 '15 at 06:47

2 Answers2

3

Actually, all you need to do is:

git checkout branch_name

If there is an origin/branch_name, the local branch will automatically:

  • checkout origin/branch_name
  • track it (set origin/branch_name as an upstream branch of the new local branch)

See git checkout man page:

If <branch> is not found but there does exist a tracking branch in exactly one remote (call it <remote>) with a matching name, treat as equivalent to:

$ git checkout -b <branch> --track <remote>/<branch>

Then you can work in that local branch and/or merge it to your work branch.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • thanks I just did that by accident - with another branch from the same remote - and concluded that git just created the local branch from the info it had in the remote tracking branch. but when I explitcitly want to see what the state of the remote tracking branch - independent of my own work, isnt `git checkout remotename/branchname` correct? – the_velour_fog Dec 02 '15 at 07:27
  • @user4668401 if you don't want to make any commit in it, yes, a detached HEAD checkout can work. Incidentally, remember that you can have multiple checkouts per repo nowadays: http://stackoverflow.com/a/30185564/6309 – VonC Dec 02 '15 at 07:29
2

That is actually correct. Since you can't move the tracking branch, you are in detached HEAD.

All detached HEAD means is that making a new commit, then moving away from it (by checking out something else) will leave you with no reference to that new commit.

For example, checking out a tag will also tell you that you're in detached HEAD. Since tag refs don't move on their own, any new commit you add will not have a true ref pointing to it.

So to answer your question: Is there a more "proper" way to inspect the contents of a remote branch before attempting a merge? No. What you're doing is right.

Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • ah ok, that makes sense - so basically I should look at the non-normal status reporting as an alert that the repo is in an unusual - read only - state. Also as an aside - it seems to me a more descriptive name for detached HEAD would be : *un-attachable HEAD* ? – the_velour_fog Dec 02 '15 at 06:55
  • The HEAD is *not* un-attachable. At any point in time, you can decide to create a new branch at your current position, which will attach the HEAD to it (meaning it would move together with HEAD). Detached head is the correct term, even though it sounds scarily zombie-like (your head is detached. You can still look around...) – Joseph Silber Dec 02 '15 at 16:28