-->

How to revert uncommitted changes including files

2019-01-07 01:08发布

问题:

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?

回答1:

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


回答2:

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 --


回答3:

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


回答4:

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.


回答5:

git clean -fd

didn't help, new files remained. What I did is totally deleting all the working tree and then

git reset --hard

See "How do I clear my local working directory in git?" for advice to add the -x option to clean:

git clean -fdx

Note -x flag will remove all files ignored by Git so be careful (see discussion in the answer I refer to).



回答6:

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



回答7:

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.



回答8:

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


回答9:

A safe and long way:

  1. git branch todelete
  2. git checkout todelete
  3. git add .
  4. git commit -m "I did a bad thing, sorry"
  5. git checkout develop
  6. git branch -D todelete


回答10:

I usually use this way that works well:

mv fold/file /tmp
git checkout fold/file


回答11:

Use:

git reset HEAD filepath

For example:

git reset HEAD om211/src/META-INF/persistence.xml