checkout

git-checkout - Switch branches or restore working tree files

# 用index或者指定commit中的版本,来更新工作区中的文件
# 切换分支
Updates files in the working tree to match the version in the index or the specified tree.
If no pathspec was given,git checkout will also update HEAD to set the specified branch as the current branch.

When you checkout a branch,
it changes HEAD to point to the new branch ref,
populates your index with the snapshot of that commit,
then copies the contents of the index into your working directory.
改变HEAD指向,用那个commit来重新填充index,用index里的内容填充工作区

# This moves HEAD to point to the testing branch.
git checkout testing

With Paths

# -- 是为了避免文件名和已有分支重名时,歧义

# Changes not staged for commit,工作区进行了修改,但还没有add
# 此时丢弃工作区的修改,可以使用这个命令
# Any local changes you made to that file are gone.
# Git just replaced that file with the last staged or committed version.
# 用index中的版本来更新hello.c文件,即取消工作区的修改
git checkout -- hello.c

# 如果指定了commit,则用commit中的版本更新index和working tree.

checkout和reset

Running git checkout [branch] is pretty similar to running git reset --hard [branch] in that it updates all three trees
for you to look like [branch], but there are two important differences.

First, unlike reset --hard, checkout is working-directory safe;
it will check to make sure it’s not blowing away files that have changes to them.
Actually, it’s a bit smarter than that — it tries to do a trivial merge in the working directory,so all of the files
you haven’t changed will be updated.
reset --hard, on the other hand, will simply replace everything across the board without checking.

The second important difference is how checkout updates HEAD.
Whereas reset will move the branch that HEAD points to,checkout will move HEAD itself to point to another branch.

在这里插入图片描述
git checkout详解
Reset Demystified

猜你喜欢

转载自blog.csdn.net/qq_53318060/article/details/131278486