Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does `git checkout ...` do?

Tags:

git

I occasionally accidentally write git checkout ..., which puts me into a detached head state. I was wondering why. Here's the "dot story":

> git checkout . # checks out current directory > git checkout .. # Checks out parent directory, if in repository. > git checkout ... # Puts into detached head state? > git checkout .... error: pathspec '....' did not match any file(s) known to git. 
like image 872
Chris Jefferson Avatar asked Sep 07 '17 16:09

Chris Jefferson


People also ask

What does git checkout file do?

The git checkout command is used to update the state of the repository to a specific point in the projects history. When passed with a branch name, it lets you switch between branches. Internally, all the above command does is move HEAD to a different branch and update the working directory to match.

What is git checkout vs pull?

With checkout you switch to a specific revision. You want to do this, if you just started using this. Now if you are already following a remote branch, you might only need to update your local branch. That's what pull does for you.

Does git checkout create a new branch?

The easiest way to create a Git branch is to use the “git checkout” command with the “-b” option for a new branch. Next, you just have to specify the name for the branch you want to create. To achieve that, you will run the “git checkout” command with the “-b” option and add “feature” as the branch name.

What does check out a repository mean?

"To check out" means that you take any given commit from the repository and re-create the state of the associated file and directory tree in the working directory.


Video Answer


1 Answers

This is a degenerate form of this syntax, described in the gitrevisions(7) man page:

   <rev1>...<rev2>        Include commits that are reachable from either <rev1> or <rev2> but        exclude those that are reachable from both. When either <rev1> or        <rev2> is omitted, it defaults to HEAD. 

Note that last bit, "When either <rev1> or <rev2> is omitted, it defaults to HEAD". That means that writing ... is equivalent to HEAD...HEAD. When used in git checkout this ends up evaluating to the commit id of HEAD. That is, you're just doing:

git checkout HEAD^{commit} 
like image 177
larsks Avatar answered Oct 01 '22 00:10

larsks