1

Suppose one is on branch "master" and that HEAD is at the tip of the branch (say, on commit C). Suppose one then executes git reset --hard HEAD^3 back to commit A. Then HEAD now refers to A.

Question: Does "master" also refer to A, or is it still pointing at C? Put differently: in this context, does HEAD always refer to what the branch "master" does (i.e., assuming that our repository has only one branch named "master")?

George
  • 6,927
  • 4
  • 34
  • 67
  • possible duplicate of [Difference between HEAD and master](http://stackoverflow.com/questions/4386959/difference-between-head-and-master) – vitorbal Aug 20 '15 at 12:00
  • Put simply, the answer is no. Take a look at this question, I think the accepted answer explains the difference between "master" and "HEAD" pretty well: http://stackoverflow.com/questions/4386959/difference-between-head-and-master – vitorbal Aug 20 '15 at 12:01
  • What commits are between `A` and `C`? `git reset HEAD~3` puts you back *three* commits behind, so you probably want four commits in your example: `A -- B -- C -- D`. – jub0bs Aug 20 '15 at 12:21

2 Answers2

2

No, HEAD will not always refer to the same commit as "master". In case you checkout a commit that has been become dangling HEAD will refer to that commit and "master" will still refere to the tip of that branch.

In this case will git reset --hard HEAD~3change HEAD and master to refer to the same commit.

joran
  • 2,815
  • 16
  • 18
0

Let's check it, current branch is:

$ git branch
* master

current state of the repo:

$ git log --oneline
e585b43 C
4bbf8be B
6ae7d39 A
fb4949b Initial commit

Let's rebase it:

$ git reset --hard HEAD~3
HEAD is now at fb4949b Initial commit

Let's check where HEAD points:

$ git log HEAD  --oneline
fb4949b Initial commit

Let's check where master points:

$ git log master --oneline
fb4949b Initial commit

As you can see HEAD and master point at the same commit

neshkeev
  • 6,280
  • 3
  • 26
  • 47
  • So master doesn't persist in referring to the tip of the branch, in this case commit C? Doesn't that conflict with joran's answer? – George Aug 20 '15 at 12:15
  • *`HEAD` and `master` point at the same commit.* Again, that's imprecise. `HEAD` points at `master`, which itself points at commit `fb4949b`, but `HEAD` does not point directly at commit `fb4949b`, in this case. – jub0bs Aug 20 '15 at 12:37