二、GIT基础-撤消修改

作者: 温东 | 来源:发表于2017-12-28 15:00 被阅读0次

    4、撤消修改

    我们编辑Hello.txt文件,添加一行新的内容后保存,这时我们发现最后一行的内容添加有误,我们需要删除它。这时我们可以选择手工删除刚才添加的最后一行,把文件恢复,我们也可以使用git status先查看状态:

    Hello world
    first modify
    second modify
    three modify
    four modify
    [root@node1 git-test]# echo "five modify" >>Hello.txt 
    [root@node1 git-test]# cat Hello.txt 
    Hello world
    first modify
    second modify
    three modify
    four modify
    five modify
    [root@node1 git-test]# git status
    On branch master
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
        modified:   Hello.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")
    你可能会发现,Git告诉你,git checkout –file可以删除工作区的修改:
    [root@node1 git-test]# git checkout -- Hello.txt 
    [root@node1 git-test]# git status
    On branch master
    nothing to commit, working tree clean
    You have new mail in /var/spool/mail/root
    [root@node1 git-test]# cat Hello.txt 
    Hello world
    first modify
    second modify
    three modify
    four modify
    我们再看另一种情况,
    [root@node1 git-test]# echo "five modify" >>Hello.txt 
    [root@node1 git-test]# git add Hello.txt 
    [root@node1 git-test]# git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        modified:   Hello.txt
    
    [root@node1 git-test]# echo "six modify" >>Hello.txt 
    [root@node1 git-test]# git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        modified:   Hello.txt
    
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
        modified:   Hello.txt
    
    [root@node1 git-test]# git checkout -- Hello.txt 
    [root@node1 git-test]# git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        modified:   Hello.txt
    
    [root@node1 git-test]# cat Hello.txt 
    Hello world
    first modify
    second modify
    three modify
    four modify
    five modify
    

    最后我们总结一下:
    一种是readme.txt自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;
    一种是readme.txt已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。
    总之,就是让这个文件回到最近一次git commit或git add时的状态。最后要注意git checkout –file命令中的--,这个很重要,如果没有—这个命令就是另外的作用了。我们在后面具体会何用。
    现在我们把所有的修改都已经提交到了暂存区,还没有commit,这个时候我们发现刚才的修改有错误,那么我们可以使用git reset HEAD file把暂存区的修改撤销掉,重新放回工作区:

    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        modified:   Hello.txt
    
    You have new mail in /var/spool/mail/root
    [root@node1 git-test]# git reset HEAD Hello.txt 
    Unstaged changes after reset:
    M   Hello.txt
    [root@node1 git-test]# git status
    On branch master
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
        modified:   Hello.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")
    [root@node1 git-test]#
    

    我们将所有的修改都commit,这个时候如果发现错误,想回退到刚才提交之前的版本该怎么处理?
    首先我们通过git log命令查看我们的提交历史:

    6cfd50cb9f0edec3fc59dd59845b4c6e3b5ec22a git reset HEAD test
    8d9eb9afd8c38821cc84ffe47fe2261386f48a4f aaa
    6597abf6bb3617c2cbaad12cf1749d9771464ecd git mv test
    2b1583efcc6e18d0f80946f88d416b140b01367a delete Hello.txt
    9ccca93a00bd91a4883595b2f44a416ffb5ede9e mv test
    3a2db18768b228b3af6fa878e36544d15e35cfa4 rm test finish
    a80c6a946c0968edcbe3b9826629112b147a84e2 rm test
    4794019de33bc2bfcea823ded27f80e98230e114 jump git add submit
    b79795b531900217191da0db8b36ee220c074ad4 tree submit
    a306d944c27e569f8003a82f7e251159cb61db33 second submit
    5e874cc11b31065c65d4fd4ed7e6a275dca524d5 first submit
    我们使用git reset –hard b7979回到第三次提交的版本:
    [root@node1 git-test]# git reset --hard b79795
    [root@node1 git-test]# git log --pretty=oneline
    b79795b531900217191da0db8b36ee220c074ad4 tree submit
    a306d944c27e569f8003a82f7e251159cb61db33 second submit
    5e874cc11b31065c65d4fd4ed7e6a275dca524d5 first submit
    [root@node1 git-test]# cat README 
    My first git test project
    first modify
    second modify
    three modify
    

    我们可以使用git reset –hard commit-id返回指定的版本,但是返回后git log里面就只有当前提交之前的提交记录。
    Git的版本回退速度非常快,因为Git在内部有个指向当前版本的HEAD指针,当你回退版本的时候,Git仅仅是把HEAD从指向当前的版本改为指向你要回退的版本,然后顺便把工作区的文件更新。
    小结:
    场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- file。
    场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset HEAD file,就回到了场景1,第二步按场景1操作。
    场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,使用git reset --hard commit-id回到指定的版本,不过前提是没有推送到远程库。

    相关文章

      网友评论

        本文标题:二、GIT基础-撤消修改

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