2

I'd like to know if we use git checkout <commit hash> and the commit is in multiple branch, how do we know which one get checkout?

I didn't know how to do it visually so I tried to do a little drawing, sorry about that.
I'm not allowed to post it directly so here's a link.
If we have those three branches, then we git checkout <Commit Z>, will we get Branch A or Branch B? How does it work? Thank you!

https://i.stack.imgur.com/Y3wJr.png

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61

3 Answers3

4

git checkout <commit> sets HEAD at this commit, regardless of any branches which might or might not point on said commit.

The working tree is updated accordingly, and you get what's called a detached HEAD. Further commits made from this point will indeed take HEAD as their parent, but no branch points to it until you create one (or make an existent one point here).

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • Will this _detached HEAD_ contains commits A,B,C,D? The link you provided seem to explain what happens if we commit AFTER a detached HEAD, but what about the commits before? – AnonymousLama Jan 23 '20 at 12:56
  • @AnonymousLama This `checkout ` doesn't modify your commit tree, everything reachable from a commit stays reachable. Your schema is not the clearest ever, but if you checkout commit `Z` (or `branch B` for that matter), no it won't contain `A,B,C,D` . – Romain Valeri Jan 23 '20 at 13:01
  • That means it gets checkout where it was first created, and not any other reference to this commit in another branch? – AnonymousLama Jan 23 '20 at 13:08
  • It means that if you checkout `Z` for example, then commit twice, these two new commits are on no branch at all. If you then checkout a branch, these commits become candidates for garbage collection, unless you reattach a branch to them (the reflog keeps a reference on it so it won't be deleted soon, it's usually a matter of weeks). – Romain Valeri Jan 23 '20 at 13:09
4

A branch name is just a pretty name for a commit hash (branch HEAD to be exact).

Checking out a commit hash will put you into "detached head" state.

You can read about it here, or make an on-line search about detached head state and what it means

A commit (hash) can belong to many branches, and many branch HEADs can point to the same commit hash.

Checking out a commit (regardless if it is the HEAD of any branch) will put you to the detached HEAD state.

ArielGro
  • 795
  • 1
  • 11
  • 24
  • If I understand correctly, that means that a `git checkout ` can be translated to `git checkout `? – AnonymousLama Jan 23 '20 at 13:02
  • 2
    @AnonymousLama No. Checking out a branch or checking out the commit at its tip leads to the same working tree, but with a different `HEAD`. If you commit in the latter case, the new commit won't be on the branch. – Romain Valeri Jan 23 '20 at 13:05
2

Branch is reference to commit, which in user eyes is head of branch. If you checkout commit literally (by SHA) you go to head detached state.

Leszek Mazur
  • 2,443
  • 1
  • 14
  • 28