5

What is the best way to discard uncommitted changes with Git.

I read this, but want to fully understand what each step is doing.

git undo all uncommitted or unsaved changes

git reset

"This will unstage all files you might have staged with git add", what does unstage mean?

Does this mean remove any added files?

git checkout .

"revert all local uncommitted changes (should be executed in repo root)"

What is considered the root? If I am on Branch A and I want to disregard all uncommitted changes on Branch A, is Branch A considered the root?

git clean -fdx

"WARNING: -x will also remove all ignored files, including ones specified by .gitignore! You may want to use -n for preview of files to be deleted."

Is this deleting my files? including ignored files? What if ignored files were not changed?

I simple want go back to my last commit and not worry about any changes after it. What is the simplest and safest way to do that?

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
user2012677
  • 5,465
  • 6
  • 51
  • 113
  • 1
    Possible duplicate of [git: undo all working dir changes including new files](https://stackoverflow.com/questions/1090309/git-undo-all-working-dir-changes-including-new-files) – phd Mar 17 '19 at 20:12
  • 1
    https://stackoverflow.com/search?q=%5Bgit%5D+undo+all+changes – phd Mar 17 '19 at 20:12

3 Answers3

7

what does unstage mean?

"unstage" means that the changes are removed from the index. The index is where you stage changes before committing them. Unstaging does not delete any files or reverse any changes from your working directory.

What is considered the root?

Here, "root" refers to the base directory of your project. It has nothing to do with branches or commits.

I simple want go back to my last commit and not worry about any changes after it. What is the simplest and safest way to do that?

The simplest way to throw away all of your local changes is with

git reset --hard HEAD
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
5

The one people usually do when they think f%ck it, I don't want to see what I have in front of me, I want to take everything on my working tree as it was on the last revision and forget about everything else is:

git reset --hard

Use with caution: It will take your working tree to be the way it is on the revision you are on, all changes in working tree and in index are discarded so you won't see them again.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
3

You can do (from your repo root)

git checkout HEAD -- .

or alternatively (as already suggested by eftshift0 and Code-Apprentice in their answers)

git reset --hard HEAD

Both commands will restore your files in the state they were at last commit.

Warning though : this operation is not undoable. If you have the slightest doubt about future use of these failed changes, it's a better idea to stash them :

git stash

(and have the possibility to inspect them or reuse at a later point)


And about the other commands you considered, git reset (HEAD is implied here) would "only" get all changes out of the index, not out of the working tree. So your unwanted changes would still be there in your files, just unstaged for the next commit.

And beware of git clean -fdx, it's a dangerous command if badly used, since it will delete every untracked file. It has its use, but in the case you describe (getting your project in the state it was at last commit), it's not what you need.

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • @rkta That's right. But I was refering to the uncommited changes, and though you can indeed come back to the previous state in the reflog, you won't get your uncommited changes back. – Romain Valeri Mar 18 '19 at 20:09