Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when we git checkout a commit

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.sstatic.net/Y3wJr.png

like image 413
LMDev Avatar asked Feb 27 '26 08:02

LMDev


1 Answers

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).

like image 98
Romain Valeri Avatar answered Mar 02 '26 05:03

Romain Valeri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!