How to revert uncommitted changes including files and folders?

前端 未结 14 1648
小鲜肉
小鲜肉 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:23

    You can just use following git command which can revert back all the uncommitted changes made in your repository:

    git checkout .
    

    Example:

    ABC@ABC-PC MINGW64 /c/xampp/htdocs/pod_admin (master)
    $ git status
    On branch master
    Your branch is up-to-date with 'origin/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:   application/controllers/Drivers.php
            modified:   application/views/drivers/add.php
            modified:   application/views/drivers/load_driver_info.php
            modified:   uploads/drivers/drivers.xlsx
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    ABC@ABC-PC MINGW64 /c/xampp/htdocs/pod_admin (master)
    $ git checkout .
    
    ABC@ABC-PC MINGW64 /c/xampp/htdocs/pod_admin (master)
    $ git status
    On branch master
    Your branch is up-to-date with 'origin/master'.
    
    nothing to commit, working tree clean
    
    0 讨论(0)
  • 2020-11-28 17:24

    I think you can use the following command: git reset --hard

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

    You can run these two commands:

    # Revert changes to modified files.
    git reset --hard
    
    # Remove all untracked files and directories.
    # '-f' is force, '-d' is remove directories.
    git clean -fd
    
    0 讨论(0)
  • 2020-11-28 17:29

    Git 2.23 introduced git restore command to restore working tree files.

    https://git-scm.com/docs/git-restore

    To restore all files in the current directory

    git restore .

    If you want to restore all C source files to match the version in the index, you can do

    git restore '*.c'

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

    Use:

    git reset HEAD filepath
    

    For example:

    git reset HEAD om211/src/META-INF/persistence.xml
    
    0 讨论(0)
  • 2020-11-28 17:30

    One non-trivial way is to run these two commands:

    1. git stash This will move your changes to the stash, bringing you back to the state of HEAD
    2. git stash drop This will delete the latest stash created in the last command.
    0 讨论(0)
提交回复
热议问题