User Tools

Site Tools


git_undoing_things

This is an old revision of the document!


Overview

Various ways to undo, backpedal and regret what you just did.

The state of files

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.

Undoing uncommitted changes

Throwing away all staged and unstaged changes

If you have no interest in any of the local changes (staged or unstaged):

$ git reset --hard

They're gone.

Undoing unstaged changes (part one)

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

Undoing unstaged changes (part deux)

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

Undoing all staged and unstaged changes

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

Unstaging staged content

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
git_undoing_things.1551183854.txt.gz · Last modified: 2019/02/26 12:24 by rpjday