How to revert uncommitted changes including files

2019-01-07 01:10发布

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?

11条回答
Melony?
2楼-- · 2019-01-07 01:31
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).

查看更多
霸刀☆藐视天下
3楼-- · 2019-01-07 01:31

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
查看更多
Ridiculous、
4楼-- · 2019-01-07 01:33

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
查看更多
家丑人穷心不美
5楼-- · 2019-01-07 01:39

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.
查看更多
对你真心纯属浪费
6楼-- · 2019-01-07 01:42

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
查看更多
一夜七次
7楼-- · 2019-01-07 01:43

I usually use this way that works well:

mv fold/file /tmp
git checkout fold/file
查看更多
登录 后发表回答