GitHub & Projects
How to Fix Git Mistakes Without Panicking
Committed to the wrong branch? Pushed a file you shouldn't have? Git is built to be forgiving — six common student mistakes, the exact fix for each, and the one rule that tells you whether a fix is safe.
Every student developer has a Git horror story. You commit to main instead of your feature branch. You push a file with an API key in it. The mistake itself is rarely the disaster. The disaster is the sixty seconds after it, when panic takes over and you run commands you do not understand. Git is built to be forgiving: almost everything can be undone. Here are the six mistakes students make most often, and the exact fix for each.
The golden rule: stop before you type
Before you fix anything, run git status and read the output. It tells you your branch, your staged files, and whether you are mid-merge or mid-rebase. Second, do not run a command you do not understand. A forum answer that says "just run git reset --hard" might be right for the person who asked, and catastrophic for you. If a command has --hard or --force in it, understand what it destroys first. Before anything risky, make a backup branch — it points at exactly where you are now.
git branch backup-before-fixSix mistakes and the fix for each
1. You committed to the wrong branch
You did an hour of work, committed it, and then realised you were on main the whole time. Nothing is lost — the commit exists, it is just on the wrong branch. Create the branch you meant to be on, pointing at your current commit, then move main back one step.
git branch feature-name
git reset --hard HEAD~1If you have already pushed to main, do not do this — rewriting a branch other people use creates a bigger mess than the one you are fixing. Leave the commit where it is, or talk to your teammate first.
2. Your commit message is wrong
You typed "fix stuff" or a message with a typo, and now it sits at the top of your history. If it is the most recent commit and you have not pushed it yet, fix it with amend, which rewrites the last commit. The same trick adds a file you forgot: stage it, run amend with --no-edit, and it joins the same commit.
git commit --amend -m "Add login form validation"
git add forgotten-file.ts
git commit --amend --no-editThe same warning applies as above: only amend commits you have not pushed. Once a commit is on the remote, changing it means rewriting history other people may already have.
3. You committed files you did not mean to
A build folder, a .env file, a dataset — something slipped into a commit that should never have been there. If it is the last commit, undo it without losing your work: a plain reset leaves the files in your working directory and takes them out of the commit. Add a .gitignore entry so it cannot happen again.
git reset HEAD~1If the file is a secret — an API key or a password — removing it from the commit is not enough. Git history keeps every version, so anyone with the repo can still find the key in an old commit. The fix is to rotate the secret: revoke it and generate a new one.
4. You want to undo the last commit but keep the work
The answer depends on one choice: do you want to keep the changes or throw them away? To keep them, use the soft reset. Your commit disappears, but every change stays staged, ready to be committed again. To throw the changes away entirely, use the hard reset: everything in that commit is gone from your working files.
git reset --soft HEAD~1 # keep the changes, undo the commit
git reset --hard HEAD~1 # throw the changes away tooThat is why the hard reset is the command to respect: there is no undo for discarded work. When in doubt, use --soft — you can always discard later, but you cannot un-discard.
5. A merge or a rebase went wrong halfway through
You started a merge, the terminal filled with conflict markers, and now you are staring at files full of <<<<<<< HEAD. Conflict markers look alarming, but they are just Git showing you both versions and asking you to choose. Open each conflicted file, pick the version you want, delete the markers, then stage the file and commit to finish the merge. If the merge was a mistake in the first place, you can walk away cleanly: abort returns everything to how it was before the merge started.
git merge --abort # walk away from a bad merge
git rebase --abort # walk away from a bad rebaseAborting is not giving up. It is choosing to try again when you understand the situation better.
6. You pushed a bad commit to a shared branch
You pushed, your teammate pulled, and then you spotted the bug — or the file that should never have been committed. Do not rewrite the branch: your teammate already has the old commit, and rewriting history on a shared branch leaves both of you with different versions of the truth. Instead, undo the change with a new commit. Revert creates a commit that undoes exactly what the bad commit did, and everyone pulls it normally.
git revert <commit-hash>The one rule that decides whether a fix is safe
Every fix above comes down to one question: has anyone else seen this commit yet? If the commit exists only on your machine, you can amend it, reset it, move it between branches, or delete it — nobody else's copy of the repo is affected. The moment you push, the commit belongs to everyone who has pulled it. That is why experienced developers are relaxed about their own branches and careful about shared ones.
A small safety kit for next time
A few habits make all of this rare and painless. First, make a backup branch before any command with --hard, --force, or rebase in it. Second, learn git reflog: it lists every move your HEAD has made, including commits you thought you had lost, and resetting to a hash from the reflog can bring back work you believed was gone. Third, keep commits small and push often, so a mistake is never more than a few minutes of work.
- git status first — it tells you where you are.
- git branch backup-before-fix before risky commands — your way back.
- git reflog when work seems lost — it remembers forgotten commits.
- git revert on shared branches, git reset on your own.
Git forgives almost everything — as long as you stop, read what it is telling you, and undo with the right command instead of the fastest one.
You will make Git mistakes throughout your career. Every developer does. The difference between a beginner and an experienced developer is not that the experienced one never breaks anything — it is that they have broken it before, they know which undo matches the situation, and they stay calm for the sixty seconds it takes to fix it. The same calm helps elsewhere: writing commit messages your future self can understand makes the history you fix easier to read, and reviewing your own pull request before anyone else sees it catches most of these mistakes before they reach a shared branch.