乐闻世界logo
搜索文章和话题

What is head in git?

4个答案

1
2
3
4

In Git, HEAD is a reference that points to the latest commit of the current branch. In simple terms, it represents the result of your last commit or the current working snapshot. In Git, HEAD is typically used to represent the tip of the current branch.

For example, if you are working on the master branch and make some commits, HEAD will point to the latest commit of the master branch. If you switch to another branch, such as feature, HEAD will update to point to the latest commit of the feature branch.

In Git, HEAD can be in an attached state or a detached state. When it is in an attached state, it points to the latest commit of the current branch. When in a detached state, it directly points to a commit rather than the tip of a branch. This situation typically occurs when you check out a specific commit instead of a branch.

Additionally, HEAD can be used with other references, such as HEAD~1 to reference the parent commit of the current commit, HEAD~2 to reference the parent of the parent commit, and so on.

A practical example is that if I want to view the previous commit in the history of the current branch, I can use the command git show HEAD~1. If I want to reset the current branch to the state of this previous commit, I can use the command git reset --hard HEAD~1. This will make the HEAD of the current branch point to that commit and update the contents of the working directory to match that commit.

2024年6月29日 12:07 回复

In Git, HEAD is a reference pointing to the latest commit checked out in the current working directory. In other words, HEAD points to the latest local commit on the current branch and updates with new commits.

When you switch branches, HEAD moves to the latest commit of the new branch. In most cases, HEAD points to a branch name, such as master or main, referred to as an 'attached HEAD'. This indicates that you are in a normal development workflow, with HEAD attached to a specific branch.

However, you can check out a specific commit without attaching to any branch, known as a 'detached HEAD'. In this mode, you can view historical code or make experimental commits. However, note that commits created while in a detached HEAD state may be lost when switching back to a normal branch unless you explicitly create a new branch to preserve these changes.

You can check where HEAD points using git log, or view the current commits of all branches with git show-ref --heads. In Git's implementation, HEAD is typically a file containing the SHA-1 hash of the current commit, located in the .git/ directory.

2024年6月29日 12:07 回复

HEAD is simply a special pointer that points to your current local branch.

From Pro Git book, Chapter 3.1 Git Branching - Branches in a Nutshell in the Creating a New Branch section:

What happens if you create a new branch? This creates a new pointer that you can move. Suppose you create a new branch named testing. You can use the git branch command to do this:

shell
$ git branch testing

This creates a new pointer at the same commit as your current position.

Image description placeholder

How does Git know which branch you are currently on? It maintains a special pointer called HEAD. Note that this is quite different from the HEAD concept in other VCS (e.g., Subversion or CVS) that you might be familiar with. In Git, this is a pointer to your current local branch. In this case, you are still on master. The git branch command only creates a new branch—it does not switch to it.

Image description placeholder

2024年6月29日 12:07 回复

There may be a subtle but important misunderstanding in these answers. I think I should add my answer to clarify it.

What is HEAD?

HEAD is you

HEAD is a symbolic reference pointing to any location in your commit history. No matter where you go or what you do, it follows you like a shadow. If you make a commit, HEAD moves. If you check out something, HEAD moves. No matter what you do, if you move to a new place in the commit history, HEAD follows you. To resolve a common misunderstanding: you cannot detach yourself from HEAD. This is not a detached HEAD state. If you find yourself thinking: 'Oh no, I'm in a detached HEAD state! I lost my HEAD!' Remember, this is your HEAD. HEAD is you. You are not detached from HEAD; you and HEAD have detached from other things.

What does HEAD point to?

HEAD can point to a commit, yes, but usually not. Let me say it again. Usually HEAD does not point to a commit. It points to a branch reference. It attaches to the branch; when you perform certain operations (e.g., commit or reset), the attached branch moves with HEAD. You can see what it points to by looking under the hood.

bash
cat .git/HEAD

Usually you get something like:

shell
ref: refs/heads/master

Sometimes you get:

shell
a3c485d9688e3c6bc14b06ca1529f0e78edd3f86

HEAD is what happens when it directly points to a commit. This is called a detached HEAD, because HEAD points to something other than a branch reference. If you make a commit in this state, master is no longer attached to HEAD and will not move with you. Where the commit is doesn't matter. You can be on the same commit as the master branch, but if HEAD points to that commit instead of the branch, it is detached, and new commits will not be associated with the branch reference.

If you try this exercise, you can visualize it graphically. Run these commands from your Git repository. You'll get slightly different output, but the key parts will be there. When you need to directly inspect a commit, use any abbreviated hash from the first output (here, a3c485d).

bash
git checkout master git log --pretty=format:"%h: %d" -1 # a3c485d: (HEAD -> master) git checkout a3c485d -q # (-q is for dramatic effect) git log --pretty=format:"%h: %d" -1 # a3c485d: (HEAD, master)

Okay, so the output has a slight difference. Directly checking a commit (instead of a branch) gives us a comma instead of an arrow. Do you think we're in a detached HEAD state? HEAD still points to the specific revision associated with the branch name. We're still on the master branch, aren't we?

Now try:

bash
git status # HEAD detached at a3c485d

No. We're in a 'detached HEAD' state.

(HEAD -> branch) You can see that it's the same as (HEAD, branch) when using git log -1.

In summary

HEAD is you. It points to whatever you're looking at, no matter where you are. Usually, it's not a commit, but a branch. If HEAD does point to a commit (or tag), even if it points to the same commit (or tag) as the branch, you (and HEAD) are detached from that branch. Since you're not attached to the branch, when you make a new commit, the branch won't follow you. HEAD, however, will.

2024年6月29日 12:07 回复

你的答案