This is an old revision of the document!


Various ways to undo, backpedal and regret.

For what follows, each file has three states:

  • HEAD: its state as of the most recent commit on this branch
  • staged: the changes added to the index
  • local changes: the combination of both staged and unstaged changes

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.

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.

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

$ git checkout HEAD -- README

Recall how to do this for everything:

$ git reset --hard
  • git_undoing_things.1551182687.txt.gz
  • Last modified: 2019/02/26 12:04
  • by rpjday