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.