Git Undoing Undoing changes


Example

Undo changes to a file or directory in the working copy.

git checkout -- file.txt

Used over all file paths, recursively from the current directory, it will undo all changes in the working copy.

git checkout -- .

To only undo parts of the changes use --patch. You will be asked, for each change, if it should be undone or not.

git checkout --patch -- dir

To undo changes added to the index.

git reset --hard

Without the --hard flag this will do a soft reset.

With local commits that you have yet to push to a remote you can also do a soft reset. You can thus rework the files and then the commits.

git reset HEAD~2

The above example would unwind your last two commits and return the files to your working copy. You could then make further changes and new commits.

Beware: All of these operations, apart from soft resets, will permanently delete your changes. For a safer option, use git stash -p or git stash, respectively. You can later undo with stash pop or delete forever with stash drop.