成为git砖家(9): git checkout <commit> <file> 的含义

1. 目的

git checkout 命令承载了非常多的功能, 想要一次全弄懂,不太现实; 这次白鱼带领大家学习 git checkout <file> 的用法。

老规矩,先查看 git checkout 的文档 :

git help checkout --web

2. 官方文档解释

在这里插入图片描述
官方文档的解释很全面,但是也显得很罗嗦,我们先忽略 -f | --outrs | --theirs 等选项, 只关注 git checkout [<tree-ish>] <pathspec> 这个简单的模式:

  • [<tree-ish>]: 方括号 [] 表示说这是一个可选字段, -ish 意思是“像…一样”, tree-ish 意思是树状的,通常是一个 commit, 但是显然也可以是 HEAD, 或分支名字, 或 tag 名字。
  • <pathspec> 意思是路径

解释:

  1. -- 是可以省略的
  2. 当没有传入 <tree-ish> 时, 也就是 git checkout <pathspec> 这个模式下,是从 index(也就是 stage 区域)作为源,替换掉 working area 的内容。(绿色框)
  3. 当传入 <tree-ish> 时, 也就是 git checkout <commit> <pathspec> 这个模式下,是从 <commit> 取得每个文件的内容, 并且替换掉 index(也就是 stage 区域)和 working area 的内容。例如:
  • git checkout HEAD <file>: 回滚 working area 和 staging area 到当前分支最后一次 commit
  • git checkout b325c <file>: 回滚 working area 和 staging area 到 b325c 这次 commit

在这里插入图片描述

3. Tower 的解释

在这里插入图片描述

Another use case for “checkout” is when you want to restore an old revision of a file:

git checkout 8a7b201 index.html

If you specify “HEAD” as the revision, you will restore the last committed version of the file, effectively undoing any local changes that you current have in that file:

git checkout HEAD index.html

4. References

  • 11
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值