美文网首页
[Git]-005 撤销修改

[Git]-005 撤销修改

作者: 一点想法 | 来源:发表于2019-08-22 17:03 被阅读0次

    1.撤销工作区的修改

    操作步骤:
    1.新建demo4.txt,添加任意内容到文件中后,git add到缓存区

    192:learn qingjiao$ cat demo4.txt
    good good study
    day day up
    
    192:learn qingjiao$ git add demo4.txt
    
    192:learn qingjiao$ git status demo4.txt
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        new file:   demo4.txt
    
    

    1.1 文件修改后还未再次提交到暂存区,git checkout -- filename对工作区的修改进行撤销

    2.对demo4.txt文件进行修改,误删除了一行内容

    192:learn qingjiao$ cat demo4.txt  #删除后文件内容
    good good study
    
    192:learn qingjiao$ git status demo4.txt #查看此时工作区状态
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        new file:   demo4.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:   demo4.txt
    

    3.使用git checkout -- filename将工作区修改撤销

    192:learn qingjiao$ git checkout -- demo4.txt  #将删除操作撤销
    
    192:learn qingjiao$ git status demo4.txt #查看此时工作区状态,只有增加这个change了
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        new file:   demo4.txt
    

    2.撤销暂存区的修改

    操作步骤:
    1.对demo4.txt中新增一行'not happy today'
    2.并将修改添加到暂存区
    3.查看工作区状态

    192:learn qingjiao$ 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:   demo4.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    192:learn qingjiao$ git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        modified:   demo4.txt
    

    2.1 git reset HEAD filename撤销暂存区的修改

    1.执行 git reset HEAD demo4.txt
    2.查看工作区和暂存区文件内容差异

    192:learn qingjiao$ git reset HEAD demo4.txt
    Unstaged changes after reset:
    M   demo4.txt
    
    192:learn qingjiao$ git diff demo4.txt
    diff --git a/demo4.txt b/demo4.txt
    index 14bfb6c..6e8d0cb 100644
    --- a/demo4.txt
    +++ b/demo4.txt
    @@ -1,2 +1,3 @@
     good good study
     day day up
    +not happy today  #工作区比暂存区多出一行,我们已经将暂存区的修改撤销了
    

    说明:如果想要此时想要将工作区的修改进一步撤销,使用git checkout -- filename即可撤销工作区修改

    192:learn qingjiao$ git checkout -- demo4.txt
    192:learn qingjiao$ git diff demo4.txt
    192:learn qingjiao$ cat demo4.txt
    good good study
    day day up    #此时工作区和暂存区的文件内容无差异
    

    相关文章

      网友评论

          本文标题:[Git]-005 撤销修改

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