Discard all uncommitted changes, modified files, added files and non-added
Solution 1
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
Solution 2
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.
Solution 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.
Related videos on Youtube
user2012677
Updated on June 18, 2022Comments
-
user2012677 almost 2 years
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?
-
phd about 5 yearsPossible duplicate of git: undo all working dir changes including new files
-
phd about 5 years
-
-
user2012677 about 5 yearswhat is the difference between
git reset --hard HEAD
andgit reset --hard
-
Romain Valeri about 5 yearsNone.
HEAD
is implied when you pass no ref explicitly. -
Romain Valeri about 5 years@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.