How to revert uncommitted changes including files and folders?

前端 未结 14 1649
小鲜肉
小鲜肉 2020-11-28 17:07

Is there a git command to revert all uncommitted changes in a working tree and index and to also remove newly created files and folders?

相关标签:
14条回答
  • 2020-11-28 17:32

    Please note that there might still be files that won't seem to disappear - they might be unedited, but git might have marked them as being edited because of CRLF / LF changes. See if you've made some changes in .gitattributes recently.

    In my case I've added CRLF settings into the .gitattributes file and all the files remained in the "modified files" list because of this. Changing the .gitattributes settings made them disappear.

    0 讨论(0)
  • 2020-11-28 17:32

    From Git help:

     Changes to be committed:
          (use "git restore --staged <file>..." to unstage)
    
        Changes not staged for commit:
          (use "git add <file>..." to update what will be committed)
          (use "git restore <file>..." to discard changes in working directory)
    
    0 讨论(0)
  • 2020-11-28 17:35

    If you have an uncommitted change (its only in your working copy) that you wish to revert to the copy in your latest commit, do the following:

    git checkout filename
    
    0 讨论(0)
  • 2020-11-28 17:40

    I usually use this way that works well:

    mv fold/file /tmp
    git checkout fold/file
    
    0 讨论(0)
  • 2020-11-28 17:41

    Use "git checkout -- ..." to discard changes in working directory

    git checkout -- app/views/posts/index.html.erb
    

    or

    git checkout -- *
    

    removes all changes made to unstaged files in git status eg

    modified:    app/controllers/posts.rb
    modified:    app/views/posts/index.html.erb
    
    0 讨论(0)
  • 2020-11-28 17:43

    If you want to revert the changes only in current working directory, use

    git checkout -- .
    

    And before that, you can list the files that will be reverted without actually making any action, just to check what will happen, with:

    git checkout --
    
    0 讨论(0)
提交回复
热议问题