git revert

git revert

现在有四次提交,

commit df2aab98015cbd253ba41d59874c2822536dc460
Author: Yale Li <hellolyx1618@gmail.com>
Date:   Tue Aug 25 11:33:00 2015 +0800

    four commit

commit 014305c26d6e8f321c87905de746e68106764ba4
Author: Yale Li <hellolyx1618@gmail.com>
Date:   Tue Aug 25 11:32:21 2015 +0800

    third commit

commit 81a493e42cd43fb69daca3c04d961bf9e6485dc6
Author: Yale Li <hellolyx1618@gmail.com>
Date:   Tue Aug 25 11:31:41 2015 +0800

    second commit

commit 5886426c4c12f8546496afe45edd06669c4a7863
Author: Yale Li <hellolyx1618@gmail.com>
Date:   Tue Aug 25 10:31:07 2015 +0800

    first commit

git revert用于反转提交,执行evert命令时要求工作树必须是干净的。

git revert用一个新提交来消除一个历史提交所做的任何修改。

revert 之后你的本地代码会回滚到指定的历史版本,这时你再 git push 既可以把线上的代码更新(这里不会像reset造成冲突的问题)。


我们现在修改第二次提交历史,找到第二次commit 的标记代码81a493e42cd43fb69daca3c04d961bf9e6485dc6。

效果如下,

➜  sample git:(master) git revert 81a493e42cd43fb69daca3c04d961bf9e6485dc6
error: could not revert 81a493e... second commit
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
➜  sample git:(master) ✗ git status
On branch master
You are currently reverting commit 81a493e.
  (fix conflicts and run "git revert --continue")
  (use "git revert --abort" to cancel the revert operation)

Unmerged paths:
  (use "git reset HEAD <file>..." to unstage)
  (use "git add <file>..." to mark resolution)

	both modified:   hello.txt

no changes added to commit (use "git add" and/or "git commit -a")

嗯,revert失败了,是因为revert过程中和现有本地仓库的代码出现了冲突,需要解决这次冲突然后再做一次提交,也就是通过一次新的提交来解决有问题的代码。

运行git revert --abort来删除这次revert。

➜  sample git:(master) ✗ git revert --abort
➜  sample git:(master) git status
On branch master
nothing to commit, working directory clean

重新选择一次提交来revert。选择第三次提交,修改第三次提交的内容为 recover third commit。。

➜  sample git:(master) git revert 014305c26d6e8f321c87905de746e68106764ba4
error: could not revert 014305c... third commit
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

然后看一下冲突的文件,

first commit
second commit
<<<<<<< HEAD
third commit
four commit

=======
>>>>>>> parent of 014305c... third commit

解决办法就是把冲突的部分解决掉,然后做一次提交,如下解决冲突后的文件内容,

first commit
second commit
recover third commit
four commit

提交,查看提交后的log信息

➜  sample git:(master) ✗ git status
On branch master
You are currently reverting commit 014305c.
  (fix conflicts and run "git revert --continue")
  (use "git revert --abort" to cancel the revert operation)

Unmerged paths:
  (use "git reset HEAD <file>..." to unstage)
  (use "git add <file>..." to mark resolution)

	both modified:   hello.txt

no changes added to commit (use "git add" and/or "git commit -a")
➜  sample git:(master) ✗ git commit -a -m 'recover third commit'
[master 4023a1d] recover third commit
 1 file changed, 1 insertion(+), 2 deletions(-)
➜  sample git:(master) git status
On branch master
nothing to commit, working directory clean
➜  sample git:(master) git log

查看log就可以看到刚才提交的信息,同时文件的内容也时我们最终想要的。

=========END=========

转载于:https://my.oschina.net/xinxingegeya/blog/496841

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值