Pulsars

Git Undo — Fix Git Mistakes Instantly

What happened?

Git is a distributed version control system where most operations are reversible — but finding the right reversal command requires understanding Git's internal model of working directory, staging area (index), and commit history. Common mistakes like committing to the wrong branch, pushing sensitive data, or creating a bad merge each require different combinations of git reset, git revert, git rebase, and git filter-branch to fix correctly.

What are the most common Git mistakes and how do you fix them?

The most common git emergencies fall into a few categories: committing to the wrong branch, wanting to undo a recent commit, accidentally pushing sensitive data, and needing to reverse a merge. Each scenario has a different solution depending on whether you've already pushed to the remote. The decision tree above guides you through these branching paths to find the safest fix.

What is the difference between git reset and git revert?

git reset rewrites history by moving the branch pointer backward. It comes in three flavors: --soft (keeps changes staged), --mixed (keeps changes unstaged, the default), and --hard (discards changes entirely). Reset is safe for local, unpushed commits — but dangerous for shared history because it forces collaborators to reconcile diverged branches.

git revert creates a new commit that undoes the changes from a specified commit. It preserves the full history, making it safe for pushed, shared branches. The downside: your history shows both the original commit and the revert, which can be noisy.

Rule of thumb: use reset for unpushed mistakes, revert for pushed ones.

When should you use --force-with-lease instead of --force?

Sometimes you need to force push — for example, after amending a pushed commit or using git filter-repo to remove sensitive data. Always use --force-with-lease instead of --force. The --force-with-lease flag checks that the remote branch hasn't been updated since your last fetch. If someone else pushed new commits, your force push is rejected — preventing you from accidentally overwriting their work.

You can even set it as the default behavior: git config --global push.default current and git config --global alias.fpush "push --force-with-lease".

Working with regular expressions in your git hooks or CI scripts? Try our Regex Tester. Need to set up file permissions on deployed scripts? Check the Chmod Calculator.

Frequently Asked Questions

What's the difference between git reset and git revert?

+

git reset moves the branch pointer backward, effectively erasing commits from history. It's safe for local, unpushed work. git revert creates a new commit that undoes the changes from a previous commit, preserving the full history. Use revert when you've already pushed to a shared branch — it's safe for collaborative workflows because it doesn't rewrite history.

When should I use --force-with-lease instead of --force?

+

Always prefer --force-with-lease over --force when you need to force push. --force-with-lease checks that the remote branch hasn't been updated by someone else since your last fetch. If it has, the push is rejected — preventing you from accidentally overwriting a teammate's work. --force blindly overwrites everything on the remote.

Can I recover a commit after git reset --hard?

+

Yes, usually. Git keeps a local reflog (git reflog) that records all HEAD movements for the past 90 days. Find the commit hash in the reflog output and use git checkout -b recovery-branch <hash> to restore it. However, if you've also run git gc (garbage collection), unreferenced commits may be permanently deleted.

I committed a password to git. Is it compromised?

+

If you pushed the commit, assume the credential is compromised — even if you force-push a fix within seconds. Automated scanners (including GitHub's secret scanning) may have already detected it, and anyone who pulled your repo has a copy. Rotate the credential immediately: revoke API keys, change passwords, generate new tokens. Then use git-filter-repo to remove it from history.

How do I undo a git merge without losing the branch?

+

If the merge hasn't been pushed, use git reset --hard ORIG_HEAD to move back to before the merge. If it was already pushed, use git revert -m 1 <merge-commit-hash> to create a new commit that undoes the merge while preserving history. Note: if you revert a merge and later want to re-merge the same branch, you'll need to revert the revert first.

Related Tools