3

This is probably a simple (but irritating) question. How do you remove all changes remove changes since last commit? I botched a patch, and I want to go back to how things were at my last commit.

I have tried:

git reset --hard master

and

git checkout master

but neither rid my folders of the files hat I have added / changed (they are still listed as untracked files.)

FrsLry
  • 348
  • 4
  • 19
Hoytman
  • 1,722
  • 2
  • 17
  • 29
  • Possible duplicate: [Reset all changes after last commit in git](http://stackoverflow.com/q/4630312/1199226) – itsjeyd Apr 23 '14 at 22:03

3 Answers3

5

Untracked files are just that, untracked. Git doesn't know or care about them. git reset --hard will only revert tracked files to the state they were in at the most recent (or named) commit.

You can use git clean to remove untracked files and directories from your working tree. Be careful you don't accidentally delete any hard work!

The "silver bullet" that will restore a repository back to the state it was at as of the most recent commit, as if you had just cloned it (this will even remove all user files that are "ignored" by the repository!) would be this: git reset --hard && git clean -fdx. (If you omit the x flag then it will only remove untracked files that are not ignored, which might include build products, for example.)

cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • is there a required operator? git actually refused to do it (and told me so in no uncertain terms) it says it needs -i -n or -f – Hoytman Apr 23 '14 at 22:08
  • @Hoytman I'd suggest reading the manpage. By default, it will refuse to operate unless you specify the `-f` argument. You can instead specify the `-n` argument to see what it *would* do if you supplied `-f`, without actually doing it. – cdhowie Apr 23 '14 at 22:09
0

I wish I could just comment on this, but check this out: How to undo last commit(s) in Git?

and here: http://git-scm.com/docs/git-reset

Community
  • 1
  • 1
Brian Robbins
  • 290
  • 3
  • 17
0

A little late to this one, but seeing that on 2017/09/26 this was still indexed as third using the google search engine here is my response. I found what worked for me via this stack exchange discussion: git undo all uncommitted or unsaved changes

Basically if you have not used git add yet then use:

git checkout .

To unstage files staged with git add use:

git reset
droid-zilla
  • 536
  • 7
  • 8