Hello Devs!
We’ve all been in a hurry—whether fixing a bug or adding a new feature—and boom, you hit commit a little too soon.
Maybe you realize right after that there’s a typo or missed some important file to include/exclude in.
Don’t worry; we’ve all been there.
That’s why we put together this step-by-step guide on how to undo the last commit (before pushing) and how to revert the latest commit.
With these simple steps you can keep your commit history clean and avoid those “whoops” moments in front of the team.
Steps to follow:
To remove the latest commit in Git without losing any changes, we can use soft reset.
This command moves the branch pointer back by one commit while keeping your changes staged, ready to modify or commit again.
git reset --soft HEAD~1
Note: HEAD~1 moves the branch back by one commit.
To verify your files are still staged use below command:
git status
Output: You should see your files listed under “Changes to be committed,” indicating they’re staged and ready.
If you’d like to update the commit message, run the below command:
git commit -m "Your new commit message here"
Once changes are done and commit messages have been updated, you can push your latest changes using the below command:
git push origin dev --force-with-lease
Note: --force-with-lease ensures your push doesn't accidentally overwrite any changes.
Finally, confirm that your latest commit is successfully updated by using the below command:
git log --oneline
You’ll see your new commit message at the top, confirming that the latest commit is now updated.
That’s It! You’ve successfully reverted and updated your latest commit.
Remember to double-check your changes with 'git status' and use '--force-with-lease' to prevent accidentally overwriting others' work.
This process helps you to maintain clean commit history, even after those “whoops” moments.
Happy coding, and keep your commits clean! 🚀
The 'git reset --hard' command permanently deletes all uncommitted changes in your working directory.
Once executed, there's no way to recover these changes unless they were previously committed.
Key risks:
- Permanently removes all staged and unstaged changes
- Resets your code to the specified commit state
- No undo option for lost changes
Safety tip: Always run 'git status' to review changes first, and consider using 'git stash' as a safer alternative before hard reset.
The main difference is how they handle working directory and staging area:
git reset --soft HEAD~
- Moves the HEAD and branch back by one commit
- Keeps your changes in the staging area
- Preserves all your modified files
- Safe option as no changes are lost
git reset --hard HEAD~
- Moves the HEAD and branch back to one commit
- Completely removes staged and working directory changes
- Resets everything to the previous commit's state
- All uncommitted changes are permanently lost
'--soft' is safer for adjusting commits, while '--hard' is used for complete resets.
When undoing a commit, users often encounter these common pitfalls:
- Data Loss: Using 'git reset --hard' can permanently delete changes.
- Overwrites: Force pushing (git push --force) may overwrite others' work.
- No Backup Branches: Failing to create safety branches before undoing.
Pro Tip: Always create a backup branch or use 'git stash' before undoing commits to prevent data loss.
You can undo multiple commits in Git by specifying the number of commits to reset. Run the below command :
git reset --soft HEAD~n
Where n is the number of commits you want to undo. This keeps changes in the staging area.
To remove both commits and changes, run below command:
git reset --hard HEAD~n
Pro Tip: Always double-check your commit history with 'git log before using '--hard' to avoid losing important changes.
If you want to completely erase the last commit, including all changes, use 'git reset --hard HEAD~1'. This will remove the commit and reset your files to the previous state.
Pro Tip: Use git log to review the commit history and confirm you're undoing the right one. It's a good way to avoid accidentally losing important work.
Still have questions?
Send an email to archana@leadwalnut.com, OR
Book a FREE consultation with an expert developer here.