This is an old revision of the document!


Various ways to undo, backpedal and regret.

$ git reset --hard

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:

$ git checkout -- .

If you have already staged some changes, made additional changes and want to undo just those local (unstaged) changes, see above.

$ 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:

$ 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.

So overwrite with the most recently committed version of the file:

$ git checkout HEAD -- README
  • git_undoing_things.1551175097.txt.gz
  • Last modified: 2019/02/26 09:58
  • by rpjday