Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do we understand git checkout [dot]

As my understanding, git checkout is about moving the head.

For example, git checkout <commit id> is to move the head to the <commit id>, git checkout <branch_name> is to move the head to the <branch_name>.

However, git checkout . is to discard the unstaged changes. it doesn't seem that there is any business about head.

So I can not understand why git uses the same key word checkout to do two totally no-relative things. Or git checkout . still works based on head?

like image 400
Yves Avatar asked Sep 19 '18 08:09

Yves


People also ask

What is start point in git checkout?

Checkout a New Branch or Reset a Branch to a Start PointIf the BRANCH-NAME branch doesn't exist, Git will create it and start it at START-POINT . If the BRANCH-NAME branch already exists, then Git resets the branch to START-POINT . This is equivalent to running git branch with -f .

How do you checkout in git?

You can not only create a new branch but also switch it simultaneously by a single command. The git checkout -b option is a convenience flag that performs run git branch <new-branch>operation before running git checkout <new-branch>. Syntax: $ git checkout -b <branchname>

What does GitHub checkout mean?

Checkout is the command used to switch between the different branches of a GitHub repository. When a branch is checked out, all files in the working directory are updated to match the versions stored in that branch.

Why is git checkout called Checkout?

DESCRIPTION Updates files in the working tree to match the version in the index or the specified tree. If no paths are given, git checkout will also update HEAD to set the specified branch as the current branch. checkout (co): Check out a working copy from a repository.


1 Answers

As you've noticed, the checkout command is overloaded to mean two different things. I think git help checkout makes that fairly clear though:

git-checkout - Switch branches or restore working tree files

There are several forms of the command, the one you're asking about is:

git checkout [<tree-ish>] [--] <pathspec>...

In your case the <tree-ish> argument is omitted, and the -- argument to separate options and filenames is ommitted, and <pathspec> is . (i.e. the current directory). That form of the command is documented as:

Overwrite paths in the working tree by replacing with the contents in the index or in the <tree-ish> (most often a commit).

So since you didn't specify a <tree-ish> argument, the contents of the files in . are replaced with the contents from the index. That means discarding any changes in . that have not been added to the index yet.

You can think of it as "checkout these files from the repository", which can either mean from a commit, or from the index (which might contain changes that have been staged but not yet committed).

like image 129
Jonathan Wakely Avatar answered Oct 16 '22 17:10

Jonathan Wakely