Git Reset and Revert Commands Explained
Posted on June 7, 2024 • 2 min read • 242 wordsUnderstand the differences between git reset and git revert commands and how to use them to modify commit history and undo changes.
Resetting and reverting are powerful Git commands for modifying commit history and undoing changes. Here’s how to use them:
git reset --soft HEAD~1Moves the HEAD pointer to the previous commit, keeping changes in the staging area.
git reset HEAD~1Moves the HEAD pointer to the previous commit, keeping changes in the working directory but not staged.
git reset --hard HEAD~1Moves the HEAD pointer to the previous commit, discarding all changes in the working directory.
git revert commit-hashCreates a new commit that undoes the changes from a specific commit.
git reset HEAD filenameRemoves a file from the staging area.
git reset --soft HEAD~1Undoes the last commit but keeps changes in the staging area.
git reset --hard HEAD~1Undoes the last commit and discards all changes.
git commit --amend -m "New commit message"Modifies the most recent commit.
git checkout -- filenameRestores a deleted file from the last commit.
git reset --hard commit-hashResets the repository to a specific commit, discarding all changes.
Understanding how to use git reset and git revert effectively can help you manage your commit history and undo changes safely. Practice these commands to become proficient in their use.