git reset用法 一 重置引用(版本切换)

reset命令让我们可以在历史版本中切换。每次commit都会有一个commit id。

比如看看我的仓库的日志:

[plain]  view plain copy print ?
  1. chenshu@sloop2:~/work/189/appengine$ git log  
  2. commit f9fae00840b97ca9ab4e782c9581fb6cb35b0ff4  
  3. Author: shuchen <csfreebird@esri.com>  
  4. Date:   Tue Sep 18 13:15:11 2012 +0800  
  5.   
  6.     update common/tool.sh  
  7.       
  8.     Change-Id: Ib4f0a3f1bc9d9033d72194d6340f87e03c43ec85  
  9.   
  10. commit 7e5aad74c76c2f2d540d7b3f3efaf9948f964de7  
  11. Merge: 3ab3ff0 f03c6c1  
  12. Author: Yongfeng Wu <ywu@esri.com>  
  13. Date:   Tue Sep 18 10:19:03 2012 +0800  
  14.   
  15.     Merge branch 'master' of ssh://10.112.18.189:29418/appengine  
  16.   
  17. commit 3ab3ff03aece1ef9e62af3a613823f1a2bcd5e84  
  18. Author: Yongfeng Wu <ywu@esri.com>  
  19. Date:   Tue Sep 18 10:13:40 2012 +0800  
  20.   
  21.     Design and implement file API for local file system.  

再看看我的HEAD,指向的是refs/heads/master文件,该文件是引用文件,reset命令主要就是修改改引用文件。

内容如下:

[plain]  view plain copy print ?
  1. cat .git/refs/heads/master   
  2. f9fae00840b97ca9ab4e782c9581fb6cb35b0ff4  
也就是当前是master branch,并且当前版本就是最后一次commit id。


要在清楚理解的前提下使用reset命令,一旦你切换到某个历史版本,之后的历史版本信息就没有了。

使用reset之前,首先确认是否配置了

[plain]  view plain copy print ?
  1. core.logallrefupdates=true  
可以通过git config core.logallrefupdates检查。


结合reflog,可以安全的使用reset命令。比如添加一个x文件做为测试。

[plain]  view plain copy print ?
  1. chenshu@sloop2:~/work/myproject$ touch x  
  2. chenshu@sloop2:~/work/myproject$ git add x  
  3. chenshu@sloop2:~/work/myproject$ git commit -m'add test file x'  
  4. [master 1aea8d9] add test file x  
  5.  0 files changed  
  6.  create mode 100644 x  
  7. chenshu@sloop2:~/work/189/myproject$ git reflog  
  8. 1aea8d9 HEAD@{0}: commit: add test file x  
  9. f9fae00 HEAD@{1}: commit: update common/tool.sh  

现在用reset切换到上一个提交

[plain]  view plain copy print ?
  1. $ git reset --hard f9fae00  
  2. HEAD is now at f9fae00 update common/tool.sh  
这是时候看一下目录,x文件已经被删除。

在看一下reflog:

[plain]  view plain copy print ?
  1. $ git reflog  
  2. f9fae00 HEAD@{0}: reset: moving to f9fae00  
  3. 1aea8d9 HEAD@{1}: commit: add test file x  
  4. f9fae00 HEAD@{2}: commit: update common/tool.sh  

但是如果用git log看,你会发现最近一次提交编程了f9fae00,后面的历史记录消失了。

再切回过去的版本呢?

[plain]  view plain copy print ?
  1. $ git reset --hard HEAD@{1}  
  2. HEAD is now at 1aea8d9 add test file x  

x文件回来了。

现在来看一下参数--hard,其实还有其他选项,默认选项是--mixed, 第三个选项是--soft.

--hard是最强的选项,将当前版本设置为某个历史版本后(通过修改引用文件/refs/heads/master来实现),staging和working area的内容也和那个历史版本完全一致。这就意味着你的某些文件可能就失踪了,像x文件一样。

--mixed是默认选项,它修改引用文件,不改变working area的文件,但是staging 内容已经和历史版本一致。

--soft是最弱选项,它只修改引用文件,不改变staging和working area的文件。


所以如果看到这个命令:

git reset --hard HEAD

意思是将staging和working area的文件恢复到HEAD指向的引用文件包含的commit id对应的那个版本。

这个命令git stash内部会调用。


如果要提交到远程gitlab管理的仓库中,需要运行

[plain]  view plain copy print ?
  1. git push -f  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值