How to undo (almost) anything with Git
- log command, which shows a list of commits. git reflog is similar, but instead shows a list of times when HEAD changed.
- reflog doesn’t last forever. Git will periodically clean up objects which are “unreachable
- If you want to restore the project’s history as it was at that moment in time use git reset –hard . If you want to recreate one or more files in your working directory as they were at that moment in time, without altering history use git checkout – . If you want to replay exactly one of those commits into your repository use git cherry-pick
- Stop tracking a tracked file. Scenario: You accidentally added application.log to the repository, now every time you run the application, Git reports there are unstaged changes in application.log. You put *.log in the .gitignore file, but it’s still there—how do you tell git to to “undo” tracking changes in this file?. Undo with: git rm –cached application.log
- You made some commits, did a git reset –hard to “undo” those changes (see above), and then realized: you want those changes back!. Undo with: git reflog and git reset or git checkout
- You can recover almost anything—anything you’ve committed—via the reflog.