Leedehai
Leedehai

Reputation: 3940

git: is HEAD "the current branch" or "the tip of the current branch"?

In the Git glossary documentation (here):

  1. HEAD is defined to be "The current branch". Later in the same paragraph, it went further by saying "HEAD is a reference to one of the heads in your repository".

  2. However, the paragraph above it defines what a head is: "a named reference to the commit at the tip of a branch."

This answer on StackOverflow cited, "as stated in the O'Reilly Git book, 2nd edtion, p.69, HEAD always refers to the most recent commit on the current branch", which means "HEAD is the tip of the current branch".

I'm confused. Is the all-capital-letter HEAD (1) the current branch, or (2) the tip of the current branch?

NOTE: this question didn't resolve my question.

Upvotes: 0

Views: 600

Answers (3)

Matt Messersmith
Matt Messersmith

Reputation: 13747

HEAD is simply where you're currently pointing. This can be either a branch (which is a pointer to a stack of commits) or a commit itself. In the typical usecase, it's going to point to a branch. However, it can also point to a commit (this snippet assumes you're at the root of some git repo):

(base) Matthews-MacBook-Pro:abc matt$ git checkout -b test
Switched to a new branch 'test'
(base) Matthews-MacBook-Pro:abc matt$ cat .git/HEAD
ref: refs/heads/test
(base) Matthews-MacBook-Pro:abc matt$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
(base) Matthews-MacBook-Pro:abc matt$ cat .git/HEAD
ref: refs/heads/master
(base) Matthews-MacBook-Pro:abc matt$ git log -2
commit 5d4fe79e315c302722cfdfef3dd049f720db5acc (HEAD -> master, origin/master, origin/HEAD, test)
Author: Matt Messersmith <[email protected]>
Date:   Tue Sep 25 20:05:38 2018 -0400

    Problem 155 sol.

commit 73cdc8f6a679664e3b92a826377b280aadf31de1
Author: Matt Messersmith <[email protected]>
Date:   Tue Sep 25 19:47:50 2018 -0400

    An easy warmup.
(base) Matthews-MacBook-Pro:abc matt$ git checkout 73cdc8f6a679664e3b92a826377b280aadf31de1
Note: checking out '73cdc8f6a679664e3b92a826377b280aadf31de1'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 73cdc8f An easy warmup.
(base) Matthews-MacBook-Pro:leetcode matt$ cat .git/HEAD
73cdc8f6a679664e3b92a826377b280aadf31de1

The difference between a "branch" and "tip of a branch" doesn't really make much sense. It's sort of like asking the difference between a pointer and the tip of the pointer. Branches just point to things (stacks of commits), and HEAD acts in a similar fashion (can point to a branch or a commit). I suppose it comes down to semantics and linguistics.

HTH!

Upvotes: 3

Quentin
Quentin

Reputation: 63114

Both. We're talking about:

  • A branch, with its intuitive meaning from a human point of view: a chain of commits which sprouts from either the "trunk" (typically master) or the ground (the initial commit);

  • A branch, as seen by Git, which keeps track of the most recent commit only. To Git, a branch is only a named pointer that refers to the tip of a commit chain.

Once you know that both definitions coïncide in practice, there's no actual ambiguïty.

Upvotes: 3

eftshift0
eftshift0

Reputation: 30156

Independently of what the documentation says (which might have to get some correction, dunno), HEAD is always where you are standing, which might very well be the tip of a branch... but it might not. Say, you checkout master~2 and there's no branch pointing to master~2. Then HEAD is on master~2, and you are working on detached HEAD state.

Upvotes: 3

Related Questions