What is the HEAD in git?
git head command
<<<<<<< head git conflict
git reset head
git detached head
git checkout detached head
There seems to be a difference between the last commit, the HEAD and the state of the file I can see in my directory.
What is HEAD, what can I do with it and what mistake should I avoid?
HEAD is a reference to the last commit in the currently checked-out branch.
There is a small exception to this, which is the detached HEAD. A detached HEAD is the situation you end up in whenever you check out a commit (or tag) instead of a branch. In this case, you have to imagine this as a temporary branch without a name; so instead of having a named branch reference, we only have HEAD. It will still allow you to make commits (which will update HEAD), so the above short definition is still true if you think of a detached HEAD as a temporary branch without a name.
What is HEAD in git?, HEAD is a reference to the last commit in the currently check-out branch. You can think of the HEAD as the "current branch". When you switch branches with git� git diff HEAD..HEAD~3: More precisely, HEAD is a moving pointer that could refer to the current branch, or it couldn’t but it always refers to the “current commit”. It (current commit) is the commit “git commit” is build on top of, and are often compared against “git diff –cached” and “git status”.
HEAD is a ref (reference) to the currently checked out commit.
In normal states, it's actually a symbolic ref to the branch you have checked out - if you look at the contents of .git/HEAD you'll see something like "ref: refs/heads/master". The branch itself is a reference to the commit at the tip of the branch. Therefore, in the normal state,
HEAD effectively refers to the commit at the tip of the current branch.
It's also possible to have a "detached HEAD". This happens when you check out something besides a (local) branch, like a remote branch, a specific commit, or a tag. The most common place to see this is during an interactive rebase, when you choose to edit a commit. In detached HEAD state, your HEAD is a direct reference to a commit - the contents of .git/HEAD will be a SHA1 hash.
Generally speaking, HEAD is just a convenient name to mean "what you have checked out" and you don't really have to worry much about it. Just be aware of what you have checked out, and remember that you probably don't want to commit if you're not on a branch (detached HEAD state) unless you know what you're doing (e.g. are in an interactive rebase).
What's a "detached HEAD" in Git?, The HEAD pointer in Git determines your current working revision (and thereby the files that are placed in your project's working directory). Normally, when� HEAD is a ref (reference) to the currently checked out commit.. In normal states, it's actually a symbolic ref to the branch you have checked out - if you look at the contents of .git/HEAD you'll see something like "ref: refs/heads/master".
What is Head in Git?, Git maintains a variable for referencing, called HEAD to the latest commit in the recent checkout branch. You can imagine HEAD as the “current committed branch”. HEAD is a reference to the last commit in the currently check-out branch. You can think of the HEAD as the "current branch". When you switch branches with git checkout, the HEAD revision changes to point to the tip of the new branch.
HEAD pointer in Git
Git maintains a reference variable called HEAD. And we call this variable a pointer, because its purpose is to reference, or point to, a specific commit in the repository. As we make new commits the pointer is going to change or move to point to a new commit. HEAD always points to the tip of the current branch in our repository. Now, this has to do with our repository, not our staging index, or our working directory.
Another way to think of it is the last state of our repository or what was last checked out, and because it's where the repository left off or the last state, you can also say that the HEAD points to the parent of the next commit or it's where commit writing is going to take place.
I think a good metaphor to think about this is the playback and record head on a cassette tape recorder. As we start recording audio, the tape moves past the head, and it records onto it. when we press Stop the place where that record head is stopped is the place it'll start recording again when we press Record a second time.Now we can move around, we can move the head to different places, but wherever the head is positioned when we hit Record again that's where it's going to start recording.
The HEAD pointer in Git is very similar, it points at the place where we're going to start recording next. It's the place where we left off in our repository for the things that we've committed.
Git References, The HEAD. The question now is, when you run git branch <branch> , how does Git know the SHA-1 of the last� The HEAD pointer in Git determines your current working revision (and thereby the files that are placed in your project's working directory). Normally, when checking out a proper branch name, Git automatically moves the HEAD pointer along when you create a new commit.
In simple terms, HEAD is a reference to the last commit in the currently check-out branch.
Think of the HEAD as the "current branch". When you switch branches with git checkout, the HEAD revision changes to point to the tip of the new branch.
You can see what HEAD points to by doing:
It is possible for HEAD to refer to a specific revision that is not associated with a branch name. This situation is called a detached HEAD.
Branches in a Nutshell, In this case, you're still on master . The git branch command only created a new branch — it didn't switch to that branch. HEAD pointing to a branch. $ git symbolic-ref HEAD test fatal: Refusing to point HEAD outside of refs/ Tags We just finished discussing Git’s three main object types ( blobs , trees and commits ), but there is a fourth.
Git Tutorial 7: What is HEAD?, Simple git tutorial explaining what is HEAD in git version control system. Git Tutorial:�Duration: 7:13 Posted: Aug 20, 2016 $ git reset --hard HEAD (going back to HEAD) $ git reset --hard HEAD^ (going back to the commit before HEAD) $ git reset --hard HEAD~2 (going back two commits before HEAD) The purpose of the “git reset” command is to move the current HEAD to the commit specified (in this case, the HEAD itself, one commit before HEAD and so on).
Git Head, Git Head. The HEAD points out the last commit in the current checkout branch. It is like a pointer to any reference. The HEAD can be understood as the "current� As you can see, the HEAD points to the target of the git checkout command — to a branch (first 3 images of the quadruplet), or (directly) to a commit (the last image of the quadruplet). The content of the working directory is changed, too, to be in accordance with the appropriate commit (snapshot), i.e. with the commit pointed (directly or
Git HEAD (commit-ish references) – Index, Versions before HEAD we can reference in Git using a number of HEAD references with a caret or “hat” character. Such references are what Git� (See git-pack-refs.) HEAD . The current branch. In more detail: Your working tree is normally derived from the state of the tree referred to by HEAD. HEAD is a reference to one of the heads in your repository, except when using a detached HEAD, in which case it directly references an arbitrary commit. head ref . A synonym for head. hook
- See also stackoverflow.com/questions/964876/head-and-orig-head-in-git/…
- Starting with Git v1.8.4, all the answers below that use
headcan now use
@in place of
HEADinstead. See this answer (last section) to learn why you can do that.
- From git-scm : The HEAD in Git is the pointer to the current branch reference, which is in turn a pointer to the last commit you made or the last commit that was checked out into your working directory. That also means it will be the parent of the next commit you do. It's generally simplest to think of it as HEAD is the snapshot of your last commit.
- Possible duplicate of What is HEAD in Git?
- So why can you have two heads?
- @e-satis: sometimes you'll see branches referred to as heads - they're stored in
refs/heads. Lower-case head is different from
HEAD, though. My answer clarifies this a bit.
- @e-satis: That's not regex. The
^is just git's notation for "the commit before" - that's the commit before the current one. (If the current is a merge, it uses the first parent.)
- @e-satis: See the specifying revisions section of the man page for git-rev-list for more information about all the ways to specify commits - this is just one tiny piece. kernel.org/pub/software/scm/git/docs/…
- No, when rev and HEAD are pointing to the same commit, there is no difference. And you could even write the commit id (the SHA-1 value) instead of rev or HEAD. And don't worry, you don't harass us with the questions :) (me at least :P)
- This is something I don't understand. If you checkout a remote branch, why do you end up with a "detached HEAD". Why don't you automatically jump in the branch in your local repo that correspond to your remote?
- @e-satis: If you want the local branch, check out the local branch. Remember that the two aren't necessarily the same - you have to tell the local one to merge the remote one (or pull). The tracking is just so it knows which one to automatically pull when you ask. The reason it's detached is that the remote branch is intended to be a pointer to the last-seen location of the branch in the remote repo. If you try to commit to it, the remote repo doesn't change, so the remote branch shouldn't either.
- OK, that's what I didn't get : having a local branch named in a way doesn't imply it's the same as the remote one. Really hard to get at the beginning cause I come from a SVN background :-) Thanks man. BTW, how do you move a headless HEAD to a local branch to commit it here ?
- @e-satis: The general answer is
git rebase <branch> HEAD. This will find the last common ancestor of
HEAD, and then take all the commits from there to
HEADand apply them (rebase them) onto
<branch>. It essentially does this by applying them as patches, so if the two branches are really different, there could be conflicts. But if
<branch>is an ancestor of
HEAD(i.e. you were in the right place, just forgot you'd detached
HEAD) the rebase is just a fast-forward merge.
- This is one of the most clear and accurate descriptions of git HEAD that I've seen, after searching for a while.
- coming back to my own answer :)