美文网首页
git revert用法

git revert用法

作者: xdlkc | 来源:发表于2017-06-12 00:39 被阅读601次
  • 上次学习了git reset,是用来回退某个版本的,在回退的时候commit提交日志也会回退,这次的git revert正好相反,以一次新的提交来作为回退的方式,与指定回退的版本相互抵消来做到撤销。

  • 先看一下用法:

    git revert commitId
    

    用来指定撤销哪次的commit,commitId就是日志中commit后面的40位十六进制id

  • 做一下比较

    reset:

    [lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git log -2
    commit 850770dd060dc59ee78ab5223ff501563aa67736
    Author: xxx <xxx.com>
    Date:   Wed Jun 7 10:08:14 2017 +0800
    
        del b
    
    commit e5ef718d32687acd3ad541f5eae454c513390acb
    Author: xxx <xxx.com>
    Date:   Wed Jun 7 10:07:41 2017 +0800
    
        b
    [lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git reset --soft  850770dd060dc59ee78ab5223ff501563aa67736
    
    [lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git log -1
    commit e5ef718d32687acd3ad541f5eae454c513390acb
    Author: xxx <xxx.com>
    Date:   Wed Jun 7 10:07:41 2017 +0800
    
        b    
    

    可以看到reset直接把回退版本之前的版本全都撤销掉了;

    revert:

    [lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git log -1
    commit 79689a057631404be86eaee7e2f12c98910ffda3
    Author: xxx <xxx.com>
    Date:   Wed Jun 7 10:14:50 2017 +0800
    
        ss
    [lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git revert 79689a057631404be86eaee7e2f12c98910ffda3
    [master ccf39cc] Revert "ss"
     2 files changed, 1 insertion(+)
     create mode 100644 a.txt
     create mode 100644 b.txt
    
     [lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git log -2
     commit ccf39cc1b98b2376ba1468698f1a2976294d9fca
     Author: xxx <xxx.com>
     Date:   Wed Jun 7 10:15:03 2017 +0800
    
         Revert "ss"
    
         This reverts commit 79689a057631404be86eaee7e2f12c98910ffda3.
    
     commit 79689a057631404be86eaee7e2f12c98910ffda3
     Author: xxx <xxx.com>
     Date:   Wed Jun 7 10:14:50 2017 +0800
    
         ss     
    

    可以看到,revert把撤销的这次作为了一次新的提交,而且要撤销的那次还保留着;
    而且在我看来,reset是回退到某个版本之前,revert是单纯的撤销某次提交,一个偏阶段,一个偏单一.

  • 后记

    在我删除本地文件后提交远程仓库时出现了如下错误:

    [lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git push origin master
    To ssh://git.sankuai.com/~zhangjunbo/test.git
     ! [rejected]        master -> master (non-fast-forward)
    error: failed to push some refs to 'ssh://git@git.xxx.com/~xxx/test.git'
    hint: Updates were rejected because the tip of your current branch is behind
    hint: its remote counterpart. Integrate the remote changes (e.g.
    hint: 'git pull ...') before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.  
    

    读一下发现是远程的代码跟我提交的不一样,拒绝覆盖,当然解决的方法就是利用

    git rm <file>
    

    这条命令来删掉本地没有的文件,然后再次提交即可.

    [lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git rm b.txt
    rm 'b.txt'
    [lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git st
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
      deleted:    b.txt
    
    [lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git ci -m "del b"
    [master 850770d] del b
     1 file changed, 0 insertions(+), 0 deletions(-)
     delete mode 100644 b.txt
    [lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git st
    On branch master
    nothing to commit, working tree clean
    

    当然了下次在本地删除文件时还是直接使用git rm 比较好,这样避免了这种的错误.

相关文章

网友评论

      本文标题:git revert用法

      本文链接:https://www.haomeiwen.com/subject/mqbkqxtx.html