I'm not really sure how I got my repository in this situation, and it's not even a problem anymore, but I'd like to learn about what happened so I don't feel so lost next time.
When doing a pull, I was warned I was in detached HEAD state. git status
showed the following (screenshot):
HEAD detached at 44422b7
So I ran git log
to find out how far back from main I was, and it showed (screenshot):
commit 44422b74b6826291479ee7a17fe18bb4acca6355 (HEAD, main)
It didn't make much sense to me, as I seemed to be in the same commit as the main branch but, eager to solve the problem, I ran git checkout main
, and then the next git log
showed, instead (screenshot),
commit 44422b74b6826291479ee7a17fe18bb4acca6355 (HEAD -> main)
and all started working as I initially expected.
So I'm left trying to understand the difference between what git log
showed on the first and second time I ran it (the one with the arrow vs. the one with just a comma), and maybe understand how I got to that situation in the first place.
Thanks for any help I can get.
CodePudding user response:
Branches are labels that point at a commit.
HEAD always points at the currently checked out commit.
commit 44422b74b6826291479ee7a17fe18bb4acca6355 (HEAD, main)
commit 44422b74b6826291479ee7a17fe18bb4acca6355 (HEAD -> main)
Both say you are at commit 44422b74b6826291479ee7a17fe18bb4acca6355.
Both say HEAD and main point at that commit.
But HEAD -> main
says main is the currently checked out branch.
When you run git commit
a new commit is made and the currently checked out branch, and HEAD, are moved to it.
It's possible to have no branch checked out, that is a detached HEAD state. In this case when you git commit
only HEAD moves. No branch is tracking your commit. If you git checkout
or git switch
away, there will be (almost) nothing referring to those detached commits.
There's plenty of ways to get into a detached HEAD state, the most basic is to checkout a commit ID. git checkout 4ba97c6ffc71f88a4de4ed88b188dbec2e5ff325
.
You can get out of a detached HEAD state by checking out a branch, like you did. Or you can make a new branch where you're at.
CodePudding user response:
These are really two separate questions:
-
trying to understand the difference between what git log showed on the first and second time I ran it (the one with the arrow vs. the one with just a comma),
-
understand how I got to that situation in the first place.
The answer to question 2 is in Why did my Git repo enter a detached HEAD state?
The answer to question 1 is simpler: the first one shows the detached-HEAD state and the second, with the arrow in it, shows the attached-HEAD state. But we can expound on this a bit: in particular, the whole concept of "detached HEAD" in the first place is a bit peculiar. Before we can describe it, though, we have to know what an attached HEAD is! In one way it's obvious—it's the opposite of a detached HEAD—but that's just circular reasoning. We need something more basic, and this lies in how Git finds commits.
First, remember that every Git commit has a unique hash ID:
commit 44422b74b6826291479ee7a17fe18bb4acca6355
That big ugly string of letters-and-digits is a number, expressed in hexadecimal. (In decimal it's 389687007142770230305517186390235637298437448533, which isn't really any better.