1

If I understand right, when you have a detached head(HEAD->commit), then the git CHECKOUT branch solves the issue(HEAD->branch[->someCommit])

But what does resetting to a branch do? It was supposed to set the pointer of an object(often a branch) to which HEAD points to another branch. But since we dont have this middle-man (branch) - what does it do then? And why?

Novellizator
  • 13,633
  • 9
  • 43
  • 65

1 Answers1

1

It simply moves HEAD: as I mentioned in "Practical uses of git reset --soft?":

git reset is all about moving HEAD.

If you move HEAD to another commit, then HEAD remains detached.

After a discussion about the difference between git reset and git checkout vs the detached or attached nature of the symbolic reference HEAD, here what we found together:

  • if HEAD points on branch, git reset would move branch as well to <something>: resetting it doesn't make it un-detached, it changes branch HEAD
  • if HEAD points on a commit (was already detached), then it would remain detached: As the OP puts it:

so when detached and I call git reset <branch> it finds the commit behind the branch and change the commit in .git/HEAD for the commit that <branch> refers to.

Let's consider a HEAD which is attached to branch1 (cat .git/HEAD would return branch1):

  • git checkout branch2 will change HEAD to branch2 and leave branch1 untouched
  • git reset branch2 will reset branch1 HEAD to branch2: cat .git/refs/heads/branch1 would contains the same SHA1 as branch2.

That's the difference:

  • reset moves HEAD (as I told at the beginning: it is all about moving HEAD)
  • checkout switches branches (or detaches HEAD)

Regarding the attached/detached nature of HEAD:

  • reset doesn't change the nature of HEAD (if it was attached, it remains attached)
  • checkout can change the nature of HEAD (if it was attached and you checkout a commit instead of a branch, HEAD becomes detached)
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • is not checkout also about moving HEAD? To my mind checkout moves the HEAD whereas reset moves the thing HEAD points to... – Novellizator Mar 28 '15 at 17:48
  • As I mentioned in "What's the difference between “`git reset`” and “`git checkout`”?" (http://stackoverflow.com/a/3639387/6309), `git checkout` will modify the working tree (and not the index). `git reset` can move HEAD without modifying the working tree (and it can reset the index as well, if not `--soft`). – VonC Mar 28 '15 at 17:50
  • ok. so in detached head, and when we neglect the working tree and the index, then reset branch and checkout branch do essentially the same?(=both move the head to the branch) – Novellizator Mar 28 '15 at 18:24
  • @Novellizator they do the same for HEAD, if you reset or if your checkout a branch. But the fact those commands are applied to a *detached* HEAD is not relevant. They would still do the same if the HEAD was *not* detached. – VonC Mar 28 '15 at 18:28
  • well, now I'm getting _really_ confused. In your own post - http://stackoverflow.com/questions/3639342/whats-the-difference-between-git-reset-and-git-checkout/3639387#3639387 you showed a picture and obviously the HEAD was NOT after moved after the reset(.git/HEAD contained the _same_ thing), only the develop was moved. So I conclude that git reset (--soft, for simplification) does _not_ do anything to the HEAD. What am I missing? – Novellizator Mar 28 '15 at 18:32
  • @Novellizator what you are missing is that HEAD is a symbolic ref: see http://stackoverflow.com/q/964876/6309. If it references a branch, then it moves the branch. If it references a commit, it changes commit. In all cases it moves. – VonC Mar 28 '15 at 18:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/74003/discussion-between-novellizator-and-vonc). – Novellizator Mar 28 '15 at 19:47
  • One very small addition, to give another perspective: both checkout and reset call internally update-ref, but git checkout calls it with --no-deref. See more here: https://github.com/git/git/blob/master/builtin/checkout.c#L649 – Novellizator Mar 28 '15 at 21:08