Undo Changes in Git: git checkout, git revert, & git reset
Master Git version control with essential undo commands
Git Command Usage Scenarios
Git Checkout Process
Navigate to repository
Open terminal and navigate to your Git repo folder
Check file status
Run git status to see which files have been modified
Revert specific file
Execute git checkout filename.html to restore the file to previous commit state
Use git checkout for uncommitted changes when you want to discard local modifications and return a file to its last committed state. This is perfect for experimental changes you decide not to keep.
Finding Commit Hashes
| Feature | GitHub/Bitbucket Web | Terminal Command |
|---|---|---|
| Access Method | Web browser interface | Command line |
| Command Required | None | git log --oneline |
| Information Shown | Full commit details | Condensed hash and message |
If you forget the --no-edit flag, you'll enter VIM editor. Exit by pressing colon (:), then 'q' for quit, then hit Return/Enter. This prevents getting stuck in the editor.
Git Reset Options
Soft Reset
Undoes commit but keeps changes staged. Files remain ready for a new commit with modifications intact.
Multiple Commits
Add numbers to undo several commits at once. For example, HEAD~2 undoes the last two commits while preserving changes.
git reset --soft HEAD~ is the same as git reset --soft HEAD^
Git Reset vs Git Reset Hard
Always ensure you have a clean working tree before performing reset operations. Run git status to verify no uncommitted changes exist that could be lost during the reset process.
Git Reset Process Flow
Check Status
Verify clean working tree with git status
Find Hash
Locate target commit hash via web interface or git log
Execute Reset
Run git reset or git reset --hard with commit hash
Course Categories Available
Key Takeaways