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

git checkout命令是用来切换分支或者撤销修改的命令。根据提供的引用,我们可以了解到git checkout命令有以下几种用法: 1. 切换本地分支:可以使用git checkout branchname命令来切换到指定的本地分支。 2. 切换远程分支:需要先将远程分支与本地分支关联,然后使用git checkout branchname命令来切换到指定的远程分支。 3. 创建并切换到新分支:可以使用git checkout -b branchname命令来创建并切换到一个新的本地分支。 4. 放弃修改:可以使用git checkout .命令来放弃所有工作区的修改,使用git checkout -- filename命令来放弃对指定文件的修改,使用git checkout -f命令来放弃工作区和暂存区的所有修改。 综上所述,git checkout命令可以用来切换分支、创建新分支和撤销修改。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [git checkout 命令图文详解](https://blog.csdn.net/wangdawei_/article/details/124567178)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [git checkout 命令详解](https://blog.csdn.net/linyichao123/article/details/128359279)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值