Undoing Things

Data Recovery

Git silently records what your HEAD is every time you change it. Each time you commit or change branches, the reflog is updated. You can see where you’ve been at any time by running the command

$ git reflog

To see the same information in a much more useful way, we can run the command

$ git log -g

If the commit you are looking for is not in the reflog then you should run the command that shows you all objects that are not pointed to by another object

$ git fsck --full

To recover it, create a new branch at that commit. The following command will create the recover-branch that is where your master branch used to be

$ git branch recover-branch _COMMIT_ID_

To delete the uncommitted state and return the working tree to the last committed state

$ git reset --hard HEAD

To delete the latest commit, and return to the previous one (one before HEAD)

$ git reset --hard HEAD~1

To return a single file to its last committed state

$ git checkout -- _FILENAME_

or

$ git checkout HEAD _FILENAME_

Stop a file being tracked (but do not delete it from the working directory, add to .gitignore etc after this)

$ git rm --cached _FILE/FOLDER_NAME_

Restore a file to a previous commit

$ git checkout _COMMIT_ID_ _FILE_TO_RESTORE_