reverting changes in git

created onJune 22, 2022
last modified onJune 28, 2022

unstaged changes

Changes that have been staged yet with , can be removed from the index with . To undo unstaged changes for a specific file, run:

git restore <file>

Or, for the whole working dir:

git restore .

prior to git version 2.3

Restore a specific file:

git checkout -- <file>

Restore the whole working directory

git checkout -- .

staged but not commited changes

Changes that have been staged (with ), can unstaged with . To unstage a file that has been staged, run:

git reset <file>

To unstage all changes that have been staged, run:

git reset

prior to git version 1.8.2

Prior to git version 1.8.2 you had to add the pointer to to unstage a file that has been staged (with ):

git reset HEAD <file>

and for all changes that have been staged:

git reset HEAD

The problem with was that if you worked with a new repo and did not have made any commit to your repo, would fail because without any commit there would be no HEAD pointer. Thus, the behaviour of was changed to allow unstaging changes in a repo without any prior commit.

unstaged and staged changes

An alternative is using , for single files, run:

git checkout <file>

to restore the whole directory, use

git checkout .

or

git checkout-index -a -f

reference

git version 1.8.2 release notes