git还原至某个commit版本

git还原至某个commit版本

现有一个git仓库的log信息如下所示:

➜  jGitTest git:(master) git log

commit d2c9551129a3ebf2b5be86af4e6dc4dfac32c206
Author: pengganyu <peng_gy@163.com>
Date:   Thu Jan 28 15:04:06 2016 +0800

    commit help

commit 325235b0a90338ec2a1325fa5ac783f1dd939677
Author: pengganyu <peng_gy@163.com>
Date:   Thu Jan 28 14:57:22 2016 +0800

    dfald

commit 12c63e071b5a7713bb08e94cbbdb147c5c7d1544
Author: pengganyu <peng_gy@163.com>
Date:   Thu Jan 28 14:46:01 2016 +0800

    fdkfa;

commit e493aa97538828fc453207a70e8e91b5e595224a
Author: pengganyu <peng_gy@163.com>
Date:   Thu Jan 28 14:36:33 2016 +0800

    commit

需求

最近提交的是
     d2c9551129a3ebf2b5be86af4e6dc4dfac32c206
要想恢复至
     e493aa97538828fc453207a70e8e91b5e595224a

方案1

jGitTest git:(master) git reset e493aa97538828fc453207a70e8e91b5e595224ajGitTest git:(master)git commit -am "reset to e493aa97538828fc453207a70e8e91b5e595224a" 
➜  jGitTest git:(master) git push -f origin master 
  • 若git push不成功,需要在repository当中设置去掉保护;
  • 此方法的弊端,把原有的commit 版本e493aa97538828fc453207a70e8e91b5e595224a之后的commit都给删掉了
commit 14bc0b0e978097c1b0e62191c58dfbab34bbb516
Author: pengganyu <peng_gy@163.com>
Date:   Thu Feb 25 12:58:03 2016 +0800

    reset to e493aa97538828fc453207a70e8e91b5e595224a

commit e493aa97538828fc453207a70e8e91b5e595224a
Author: pengganyu <peng_gy@163.com>
Date:   Thu Jan 28 14:36:33 2016 +0800

    commit

方案2

➜  jGitTest git:(master) git revert e493aa97538828fc453207a70e8e91b5e595224a  -m "back to e493aa97538828fc453207a70e8e91b5e595224a"
➜  jGitTest git:(master) ✗ git commit -am "reset to e493aa97538828fc453207a70e8e91b5e595224a" 
➜  jGitTest git:(master) git push origin master
➜  jGitTest git:(master) git log 

commit 2b4b708b2ac3d4ff2b6d446add3a0edbfb63fb41
Author: pengganyu <peng_gy@163.com>
Date:   Thu Feb 25 13:06:02 2016 +0800

    reback to e493aa97538828fc453207a70e8e91b5e595224a

commit d2c9551129a3ebf2b5be86af4e6dc4dfac32c206
Author: pengganyu <peng_gy@163.com>
Date:   Thu Jan 28 15:04:06 2016 +0800

    commit help

commit 325235b0a90338ec2a1325fa5ac783f1dd939677
Author: pengganyu <peng_gy@163.com>
Date:   Thu Jan 28 14:57:22 2016 +0800

    dfald

commit 12c63e071b5a7713bb08e94cbbdb147c5c7d1544
Author: pengganyu <peng_gy@163.com>
Date:   Thu Jan 28 14:46:01 2016 +0800

    fdkfa;

commit e493aa97538828fc453207a70e8e91b5e595224a
Author: pengganyu <peng_gy@163.com>
Date:   Thu Jan 28 14:36:33 2016 +0800

    commit
  • 新增一个commit版本,同时将代码恢复至e493aa97538828fc453207a70e8e91b5e595224a commit版本,之前的commit信息都存在,不会被删除
posted @ 2016-02-25 13:22  peng_gy  阅读(1265)  评论(0编辑  收藏  举报