How do I revert all local changes in Git managed project to previous state?

ID : 345

viewed : 84

Tags : gitrevertgit-checkoutgit





Top 5 Answer for How do I revert all local changes in Git managed project to previous state?

vote vote

91

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 
vote vote

86

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.

vote vote

70

Re-clone

GIT=$(git rev-parse --show-toplevel) cd $GIT/.. rm -rf $GIT git 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 -x 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

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 project cd project git init echo '*.built' > .gitignore echo 'CODE' > a.sourceCode mkdir b echo 'CODE' > b/b.sourceCode cp -r b c git add . git commit -m 'Initial checkin' echo 'NEW FEATURE' >> a.sourceCode cp a.sourceCode a.built rm -rf c echo 'CODE' > 'd.sourceCode' 

See also

  • git revert to make new commits that undo prior commits
  • git checkout to go back in time to prior commits (may require running above commands first)
  • git stash same as git reset above, but you can undo it
vote vote

63

If you want to revert all changes AND be up-to-date with the current remote master (for example you find that the master HEAD has moved forward since you branched off it and your push is being 'rejected') you can use

git fetch  # will fetch the latest changes on the remote git reset --hard origin/master # will set your local branch to match the representation of the remote just pulled down. 
vote vote

54

After reading a bunch of answers and trying them, I've found various edge cases that mean sometimes they don't fully clean the working copy.

Here's my current bash script for doing it, which works all the time.

#!/bin/sh git reset --hard git clean -f -d git checkout HEAD 

Run from working copy root directory.

Top 3 video Explaining How do I revert all local changes in Git managed project to previous state?







Related QUESTION?