checkout

Git的checkout命令用于切换分支或恢复工作区文件,它可以基于index或特定commit更新工作区。当切换分支时,HEAD、index和工作区都会相应改变。而checkout与reset--hard的主要区别在于,checkout对工作目录更安全,尝试合并未修改的文件,且只移动HEAD,不移动它所指向的分支;reset--hard则会直接替换所有内容,且移动分支指针。
摘要由CSDN通过智能技术生成
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值