How to Undo the Last Commit: Step-by-Step Guide

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.

How to Undo the Last Commit

Steps to follow:

1. Undo the Latest Commit

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.

2. Verify Staged Changes

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.

3. Create a New Commit with Updated Message

If you’d like to update the commit message, run the below command:

git commit -m "Your new commit message here"

4. Push latest Changes

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.

5. Confirm the Updated Commit

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.

Conclusion

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! 🚀

Related FAQs

1. Why should you be cautious when using the 'git reset --hard' command?

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.

2. What are the key differences between 'git reset --soft HEAD~' and 'git reset --hard HEAD~'?

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.

3. What are some common pitfalls to be aware of when undoing a Git commit?

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.

4. Can you undo more than one commit in Git, and if so, how?

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.

5. How do you completely undo the latest commit in Git?

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.