Course
If you’ve ever worked on a group coding project, you know how fast things can go sideways when you try to undo a change. One wrong move, and you could wipe out your own work, or worse: break something your teammate just pushed.
That’s why it’s so important to understand what Git offers to undo mistakes, and when to use each option. Some commands are safe for team environments, while others are better for local cleanup where you're okay rewriting history.
This guide walks you through it all. You’ll learn the different ways to undo commits, how they work, and which one to reach for depending on the situation.
Understanding Git Commit History
Ever hit 'Ctrl+S' to save a doc version and jump back to it later? That’s exactly what a Git commit does, but for your entire codebase.
What is a commit in Git?
A git commit is your project’s snapshot at a specific point in time. It freezes the current state of all your files and assigns that version a unique ID, like taking a timestamped photo of your code. You can always rewind to that point if something breaks later.
How does it work? When you modify a file in your repo, the changes aren’t immediately stored. Instead, they exist locally in your current working directory. To save them, these changes are staged and then committed to the repository. Your commit history is the log of all the commits performed on a repository.
Local vs remote commits
Local commits stay on your machine and aren’t visible to your colleagues or collaborators until you decide to share. That means you have more control over them: you can rewrite history, squash, delete, or reset without affecting anyone else.
Remote commits, on the other hand, are accessible to everyone who works on the repository. That means you shouldn’t rewrite or delete these commits. Doing so breaks things for your teammates if they are using the same remote branch. So you should be cautious and aware of what you’re doing before rewriting shared history.
Undoing The Last Git Commit Locally
In Git, HEAD points to the latest commit in your project’s history. When you use the git reset
command, you’re telling Git to move that pointer to a different commit.
Let’s break down how to use git reset
to undo the last commit in various scenarios.
Using git reset --soft HEAD~1
It moves HEAD back to one commit but leaves your changes in the staging area. Your files are untouched, and everything you committed is still queued up in the stage area. Use this when you want to undo the commit itself, but keep all the changes prepped and staged.
Using git reset --mixed HEAD~1
This one takes it a step further. It also moves HEAD back to one commit but clears the staging area. Your files are still in your working directory, but nothing is staged anymore. This is the default mode of git reset
.
Using git reset --head HEAD~1
It moves HEAD back, clears the staging area, and wipes out the changes in your working directory. It’s like the last commit (and all the changes with it) never existed.
Use it with care — once those changes are gone, it’s difficult to bring them back. So, only run this if you're absolutely sure you don't need anything from that last commit.
Example 1: Undoing the last commit locally (safe rework)
Let’s say you just made a commit, but realized you forgot to update the README file.
Scenario:
You committed changes to main.py
, but forgot to update README.md
. You want to amend the last commit without affecting anyone else, and you haven’t pushed anything yet.
Solution:
Use git reset --soft HEAD~1
to undo the commit and keep your changes staged:
git reset --soft HEAD~1
Now, update your README file, add it to the stage, and recommit everything:
git add README.md
git commit -m "Update main.py and README.md"
✅ Why this works: Since the commit wasn’t pushed, you can rewrite local history without impacting anyone.
Undoing a Pushed Git Commit
So you pushed a commit... and now you regret it. We’ve all been there. But how do you undo it without breaking your team members' work?
Rewriting history with git push --force
Normally, Git only lets you push if your local branch is recently pulled and up to date with the remote one. But you can override this rule using a superpower command like push --force
. It overwrites the entire remote history with whatever’s in your local branch.
That’s why --force
is powerful but dangerous. In a team setting, it can delete your colleagues’ commits if they haven’t been pulled into your local branch. Use it only when you know exactly what you’re doing — and ideally, when you’re the only one working on that repo.
Besides push --force
, there’s also pull --force
, another powerful command that should be used with care. Learn about it here.
Safer alternatives using git revert
If you want to undo a commit without deleting history, git revert
is your best bet. It doesn’t remove the commit; it creates a new one that undoes the changes.
Let’s say you committed a file called streamlit_app.py
, and you want to get rid of it. git revert
will generate a new commit that deletes just that file, leaving the rest of the history intact.
By default, git revert
targets the most recent commit. However, you can also revert older commits by specifying their commit ID.
Example 2: Undoing a pushed commit safely on a shared branch
Let’s say you pushed a broken config change and need to reverse it without deleting history.
Scenario:
You pushed a commit that adds a bad line in config.yaml
. Your team is already working on the same remote branch (main
), so you can’t use reset
or force-push.
Solution:
Use git revert
to undo the commit safely:
git revert <commit-hash>
This will open a text editor to confirm the revert message. Save and close the editor to create a new commit that undoes the previous one.
If the last commit is the one to revert:
git revert HEAD
Then push the changes:
git push origin main
✅ Why this works: git revert
doesn’t rewrite history — it adds a new commit. That makes it safe for shared branches and team workflows.
Visualizing and Confirming Changes
Sometimes, you just want to see what’s happened so far — kind of like scrolling back to previous versions to find that commit you want to undo. Git gives you tools to do exactly that.
Using git log to check the commit history
Run git log
in your terminal, and you’ll get a full list of all the commits on your current branch. You can use this to identify a specific commit to undo or modify.
For example: git log
output might look like:
- e3a1b7f Fix typo in README
- 4f2c9a1 Add login validation
- 9d5b3e8 Initial commit
Each line shows a commit hash and a message. If you ever want to undo a specific change, just copy that commit hash and use it with git revert
.
Using GUI tools or extensions
If you’re not a developer or haven’t worked with version control before, all those command-line instructions can feel like a foreign language. That’s where GUI tools come in. They make Git visual and beginner-friendly, so you can focus on what matters without memorizing commands.
GitHub Desktop
GitHub Desktop is a free app that provides a clean, easy interface to work with your GitHub repositories. Once installed, you can use it to create branches, make commits, push to remote, and manage your project, all with clicks instead of Git commands.
It’s perfect if you’re new to Git or just want a smoother experience managing changes.
GitHub desktop user interface. Image source.
Sourcetree
Sourcetree is a free Git GUI tool for both Mac and Windows. It gives you a visual way to manage branches, commits, and merges without touching the terminal. It's lightweight, beginner-friendly, and works well with popular platforms like GitHub, GitLab, and Bitbucket.
Since it’s built by Atlassian, it also plays nicely with other Atlassian tools like Jira, Confluence, and Bamboo, making it a great pick if your team already uses that ecosystem.
Sourcetree user interface. Image source.
GitKraken
GitKraken is a feature-rich Git GUI that makes working with repositories feel smooth and visual. Its main highlight is the graph view — a colorful, interactive timeline of your repo’s history that makes branches, merges, and commits easy to follow.
On the left, you’ll see your project’s file tree. On the right, you’ll get detailed info about whatever commit, file, or branch you click on. It’s intuitive, beginner-friendly, and packed with powerful features for advanced users too.
GitKraken is free for personal use, but if you’re working in an enterprise or managing private repos, you’ll need a paid plan.
Gitkraken user interface. Image source.
Best Practices for Undoing Git Commits
Undoing a commit isn’t something to fear as long as you follow good practices. Let’s discuss what they are.
Use branches for experimentation
Whenever you’re building something new or fixing a bug, create a separate branch off of main. Do all your work there, test it thoroughly, and only then merge it back into the main branch.
This way, any mistakes or rollbacks stay safely fixed in your branch without messing up the rest of the team’s work.
When in doubt, use git revert
Let’s say you made a few commits on a local branch and accidentally pushed them to the main branch. Oops. Now you want to undo one of those commits without messing up the shared history. That’s where git revert
saves the day.
Run git revert HEAD~1
. This creates a new commit that undoes the changes from the second-to-last commit (because HEAD~1 points to it). It doesn’t delete anything, instead it reverses the changes in a clean, trackable way.
Once you’ve done that, don’t forget to push the fix to the remote using git push origin <your-branch-name>
.
Note: If the changes have already been pushed to a shared branch, use git revert
. It keeps the commit history intact and won’t disrupt your teammates.
Document your changes
You don't need to pull up a Confluence page just to maintain your version history because Git already does that for you. The trick is to use it smartly.
Start with clear, meaningful commit messages. If you're reverting something, make sure the message explains why you're undoing it.
And note down the commit ID and a short summary of what changed. That's usually enough to fill the documentation purpose.
Collaborating in teams
When you use commands like git reset
, rebase
, or commit --amend
on a branch that others are also using, you’re changing commit history. Anyone who pulled the old history will now face conflicts.
So, use rewriting commands only on local or feature branches. If you must force-push or rewrite shared history, communicate this to your team as early as possible.
Conclusion
Git gives you different ways to undo mistakes; but knowing when and how to use each tool is what separates confident developers from confused ones.
We explored how git reset
works in different modes, when to use git revert,
and why it's crucial to pick the right tool based on your situation.
In summary:
- Avoid rewriting history after pushing to shared branches
- Communicate early if you must force-push
- Use
git revert
to safely undo pushed commits
For the next steps, learn how you can revert merge commit here. If you’ve wrapped up the basics and are ready to go deeper, check out our intermediate git course!
Learn Git Fundamentals Today
FAQs
What does git reset --soft HEAD~1 actually do?
It moves your HEAD pointer back one commit but leaves all the changes staged. It’s perfect when you want to revise the commit without losing any work.
When should I use git reset --mixed instead of --soft?
Use --mixed
when you want to keep your file changes but remove them from the staging area. It’s helpful for making edits before re-committing.
Is it dangerous to use git push --force?
Yes, especially in a team environment. It rewrites commit history on the remote branch and can erase teammates’ work if not used carefully.
What’s the difference between git revert and git reset?
git revert
adds a new commit that undoes changes without touching history, while git reset
moves the HEAD and can alter history—especially risky if pushed.
Can I undo a commit without losing my changes?
Yes, use git reset --soft
to keep your changes staged or --mixed
to keep them in your working directory.
What’s the safest way to undo a commit in a shared repo?
Use git revert
. It preserves history by creating a new commit that reverses the changes, avoiding conflicts with teammates.
How do I find the commit I want to undo?
Use git log
to list previous commits. Copy the commit hash of the one you want to revert or reset to.
Can GUI tools like GitHub Desktop revert commits?
Yes, GUI tools provide user-friendly ways to undo commits without needing to remember command-line syntax. They're ideal for beginners.
What happens to commit history after a reset?
With git reset
, the commit(s) are removed from your history—unless they’ve been pushed. Then, the remote still has a record unless you force-push.
Is it okay to amend a commit after pushing?
No, not without coordination. Amending and then force-pushing can disrupt shared work. Only do it in isolated branches or solo projects.
Srujana is a freelance tech writer with the four-year degree in Computer Science. Writing about various topics, including data science, cloud computing, development, programming, security, and many others comes naturally to her. She has a love for classic literature and exploring new destinations.