How do I revert all local changes in Git managed project to previous state?
If you want to revert changes made to your working copy, do this:
git checkout .
If you want to revert changes made to the index (i.e., that you have added), do this. Warning this will reset all of your unpushed commits to master!:
git reset
If you want to revert a change that you have committed, do this:
git revert <commit 1> <commit 2>
If you want to remove untracked files (e.g., new files, generated files):
git clean -f
Or untracked directories (e.g., new or automatically generated directories):
git clean -fd
Note: You may also want to run
git clean -fd
as
git reset --hard
will not remove untracked files, where as git-clean will remove any files from the tracked root directory that are not under git tracking. WARNING - BE CAREFUL WITH THIS! It is helpful to run a dry-run with git-clean first, to see what it will delete.
This is also especially useful when you get the error message
~"performing this command will cause an un-tracked file to be overwritten"
Which can occur when doing several things, one being updating a working copy when you and your friend have both added a new file of the same name, but he's committed it into source control first, and you don't care about deleting your untracked copy.
In this situation, doing a dry run will also help show you a list of files that would be overwritten.
Re-clone
GIT=$(git rev-parse --show-toplevel)cd $GIT/..rm -rf $GITgit clone ...
- ✅ Deletes local, non-pushed commits
- ✅ Reverts changes you made to tracked files
- ✅ Restores tracked files you deleted
- ✅ Deletes files/dirs listed in
.gitignore
(like build files) - ✅ Deletes files/dirs that are not tracked and not in
.gitignore
- 😀 You won't forget this approach
- 😔 Wastes bandwidth
Following are other commands I forget daily.
Clean and reset
git clean --force -d -xgit reset --hard
- ❌ Deletes local, non-pushed commits
- ✅ Reverts changes you made to tracked files
- ✅ Restores tracked files you deleted
- ✅ Deletes files/dirs listed in
.gitignore
(like build files) - ✅ Deletes files/dirs that are not tracked and not in
.gitignore
Clean
git clean --force -d -x
- ❌ Deletes local, non-pushed commits
- ❌ Reverts changes you made to tracked files
- ❌ Restores tracked files you deleted
- ✅ Deletes files/dirs listed in
.gitignore
(like build files) - ✅ Deletes files/dirs that are not tracked and not in
.gitignore
Reset
git reset --hard
- ❌ Deletes local, non-pushed commits
- ✅ Reverts changes you made to tracked files
- ✅ Restores tracked files you deleted
- ❌ Deletes files/dirs listed in
.gitignore
(like build files) - ❌ Deletes files/dirs that are not tracked and not in
.gitignore
Notes
Test case for confirming all the above (use bash or sh):
mkdir projectcd projectgit initecho '*.built' > .gitignoreecho 'CODE' > a.sourceCodemkdir becho 'CODE' > b/b.sourceCodecp -r b cgit add .git commit -m 'Initial checkin'echo 'NEW FEATURE' >> a.sourceCodecp a.sourceCode a.builtrm -rf cecho 'CODE' > 'd.sourceCode'
See also
git revert
to make new commits that undo prior commitsgit checkout
to go back in time to prior commits (may require running above commands first)git stash
same asgit reset
above, but you can undo it