3

In the git documentation for the checkout command the following text is found:

...If no paths are given, git checkout will also update HEAD to set the specified branch as the current branch....

Can anyone give a simpler explanation of what this means? I'm sorry if it seems simple, and reading through that page, I can't seem to come up with what it means exactly. Sorry if this seems basic..

In particular I am confused on how checkout is updating HEAD. I usually envision checkout affecting the working directory -- is this an ability unique to git in that you are updating your local copy of the repository for the purposes of working with it later?

Zoe
  • 27,060
  • 21
  • 118
  • 148
cgp
  • 41,026
  • 12
  • 101
  • 131

5 Answers5

3

Version A: (specifying only the branch)

git checkout <branch>

Gets all files for that <branch> and places HEAD (a pointer to "where am I now") at the branch specified.

Version B: (specifying a path only)

git checkout <file>

Gets the latest version of <file> and leaves HEAD alone.

cgp
  • 41,026
  • 12
  • 101
  • 131
vonbrand
  • 11,412
  • 8
  • 32
  • 52
  • It has been a long time since this question and I understand GIT much, much better. I prefer this answer, because it is written clearly and concisely (though I did update it, mostly for formatting) I believe at the time I had a misunderstanding that HEAD was the revision -- I didn't understand that HEAD was a pointer. I was thinking of SVN where saying HEAD usually meant I ended up at a particular revision in my workspace. – cgp Jan 17 '13 at 02:29
  • would you say it's similar to an `svn revert` ? – Adriano Sep 16 '13 at 16:19
3

HEAD is a pointer to the commit your working copy was checked out from. so if you checkout a branch (or commit or tag) then HEAD is set to that commit.

this information is stored in the textfile .git/HEAD, you can simply look at its content:

$ cat .git/HEAD
# refs: refs/heads/master
knittl
  • 246,190
  • 53
  • 318
  • 364
3

It means that git checkout branchname will

  1. Check out the files from the tip of that branch into your working directory, and
  2. Set your HEAD to the tip of that branch, so that you are now "on" it.

Example:

 jb@apto % git branch         
 * develop
   master
   next
 jb@apto % git checkout master
 Switched to branch 'master'
 jb@apto % git branch
   develop
 * master
   next
Jakob Borg
  • 23,685
  • 6
  • 47
  • 47
1

If you are in a DETACHED HEAD mode (see this question), you could easily reset HEAD to the master branch:

checkout detached

Here a 'git checkout' would reset HEAD to the tip of master branch.
(More in "A Visual Git Reference")
It will also update both the index and the working directory.
So it is not just about resetting the pointer 'HEAD'.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

With Git 2.23+ (August 2019), use git switch instead of the confusing git checkout, coupled with git restore (for files only)

git switch master

That would switch HEAD back to master.

You can still reference a commit directly (detached HEAD) with git switch --detach <acommit>.
No more checkout (which deals both with branches and files).

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250