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.
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.
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.
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.
"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.
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}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With