Various ways to undo, backpedal and regret what you just did.
For what follows, each file has three states:
Within a fresh editing session, until you stage some changes to a file, it will have the same state in the index as in HEAD.
If you have no interest in any of the local changes (staged or unstaged):
$ git reset --hard
They're gone.
Say you've made some local changes, but have staged nothing so far. From man git-checkout
:
git checkout [<tree-ish>] [--] <pathspec>... Overwrite paths in the working tree by replacing with the contents in the index or in the <tree-ish> (most often a commit). When a <tree-ish> is given, the paths that match the <pathspec> are updated both in the index and in the working tree.
To undo some local (unstaged) edits to README
, overwrite your working tree copy with the copy from the index:
$ git checkout -- README
To undo all unstaged changes throughout the working tree:
$ git checkout -- .
If you have already staged some changes, then made additional changes and want to undo only those additional, unstaged changes, see above – it works the same way.
$ git status On branch master Your branch is up to date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: README Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: README $
Discard unstaged changes:
$ git checkout -- README
End result – your unstaged changes are lost, while the staged changes in the index are right where you left them.
$ git status On branch master Your branch is up to date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: README
git checkout [<tree-ish>] [--] <pathspec>... Overwrite paths in the working tree by replacing with the contents in the index or in the <tree-ish> (most often a commit). When a <tree-ish> is given, the paths that match the <pathspec> are updated both in the index and in the working tree.
To overwrite with the most recently committed version of the file:
$ git checkout HEAD -- README
Recall how to do this for everything in the working tree:
$ git reset --hard
From man git-reset
:
git reset [<mode>] [<commit>] ... --mixed Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action.
Example:
$ git reset -- README
Recall:
git checkout [<tree-ish>] [--] <pathspec>... Overwrite paths in the working tree by replacing with the contents in the index or in the <tree-ish> (most often a commit). When a <tree-ish> is given, the paths that match the <pathspec> are updated both in the index and in the working tree.
To restore file from HEAD:
$ git checkout HEAD -- <file>
To restore file from index:
$ git checkout -- <file>