0

The first thing reset will do is move what HEAD points to. This isn’t the same as changing HEAD itself (which is what checkout does); reset moves the branch that HEAD is pointing to.

https://git-scm.com/book/en/v2/Git-Tools-Reset-Demystified

What is the difference between moving HEAD and changing HEAD?

Bob
  • 4,576
  • 7
  • 39
  • 107

3 Answers3

0

Let's say we're on main.

  • git checkout mybranch points HEAD at mybranch, and copies the contents of mybranch into the index and the working tree. We are now "on" mybranch.

  • git reset a0b0c0 points main at a0b0c0. It also keeps HEAD pointed at main so that we are still "on" main. It might or might not also copy the contents of a0b0c0 into the index and/or the working tree, depending whether this reset is soft, mixed, or hard.

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

Moving HEAD would mean changing the position in the tree of the branch HEAD points to.
Changing HEAD basically means changing the branch you're on.

The second important difference is how checkout updates HEAD. Whereas reset will move the branch that HEAD points to, checkout will move HEAD itself to point to another branch.

For instance, say we have master and develop branches which point at different commits, and we’re currently on develop (so HEAD points to it). If we run git reset master, develop itself will now point to the same commit that master does. If we instead run git checkout master, develop does not move, HEAD itself does. HEAD will now point to master.

edin-m
  • 3,021
  • 3
  • 17
  • 27
0

The quote describes the case where HEAD points to a branch. Suppose master points at CommitA, and dev points at CommitB.

Before,

HEAD -> master -> CommitA

after git reset dev

HEAD -> master -> CommitB

after git reset CommitB

HEAD -> master -> CommitB

after git checkout dev

HEAD -> dev -> CommitB

after git checkout CommitB

HEAD -> CommitB

git reset changes the commit HEAD recursively points at, from CommitA to CommitB. git checkout changes what HEAD directly points at, from master to dev and from master to CommitB.

HEAD could be detached in the first place,

HEAD -> CommitA

In this case, after git reset dev

HEAD -> CommitB

after git reset CommitB

HEAD -> CommitB

after git checkout dev

HEAD -> dev -> CommitB

after git checkout CommitB

HEAD -> CommitB
ElpieKay
  • 27,194
  • 6
  • 32
  • 53
  • Doesn't `HEAD` always point to a branch? Your first sentence makes it sound like there could be a case where `HEAD` does not point to a branch; I'm still getting used to the exact meanings Git uses. – Bob May 11 '21 at 13:59
  • @Bob No. It could be a detached HEAD. https://stackoverflow.com/a/51264681/6330106. A detached HEAD can be considered as an anonymous branch. – ElpieKay May 12 '21 at 03:13