Git and GitHub · Part 4 · Team workflows
Handling real-world mess
Almost nothing is lost
Sooner or later, everyone runs the wrong command: a reset --hard that wipes a day's work, a broken commit pushed to main, an API key on GitHub. The good news is that Git almost never deletes a commit straight away. This lesson is a set of rescues, each starting from the question to ask first: has it been pushed?
| Not pushed yet | Already pushed | |
|---|---|---|
| Safe to rewrite history? | Yes, it's only on your computer | No, others may have it |
| Tools | reset, commit --amend, rebase | git revert, a new commit |
The safety net: git reflog
The reflog is a diary of everywhere HEAD has been: every commit, reset, switch and rebase, on this computer. Even a commit no branch points to any more is still listed, and kept for 30 days by default.
Three commits, then a mistake:
git reset --hard HEAD~2HEAD is now at d067870 Add homepage“Add about page” and “Add contact form” are gone from git log. But not from the reflog:
git reflogd067870 HEAD@{0}: reset: moving to HEAD~2
2685493 HEAD@{1}: commit: Add contact form
e3f4eb8 HEAD@{2}: commit: Add about page
d067870 HEAD@{3}: commit (initial): Add homepageHEAD@{1} is where HEAD was one step ago, right before the reset. Go back there:
git reset --hard HEAD@{1}HEAD is now at 2685493 Add contact formBoth commits are back.
A deleted branch
git branch -D feature/chatDeleted branch feature/chat (was e31b597).Git even prints the hash it pointed to. If that's scrolled away, the reflog has it, as the last commit made on the branch:
2685493 HEAD@{0}: checkout: moving from feature/chat to main
e31b597 HEAD@{1}: commit: Add chatgit branch feature/chat e31b597The branch is back, with all its commits.
A broken commit pushed to main
The commit is on GitHub, and teammates may have pulled it, so it can't be removed from history. git revert adds a new commit that does the opposite (Lesson 7):
git revert HEAD
git push98d0127 Revert "Break the contact form"
f55fa74 Break the contact form
2685493 Add contact formThe site works again, the history is honest, and nobody's copy breaks. On a protected main, the revert goes through a pull request like any change; GitHub's Revert button on a merged pull request makes one.
A merge that should be undone
Reverting a merge commit needs one more piece of information:
error: commit 708e9a0… is a merge but no -m option was given.
fatal: revert failedA merge commit has two parents, and Git needs to know which side to keep. -m 1 means the first parent, the branch that was merged into, usually main:
git revert -m 1 HEAD
git pushThe feature's changes are gone from main, and the feature branch is untouched for fixing. One trap: merging that same branch again later brings back only the new commits, because Git sees the old ones as already merged. To bring the whole feature back, revert the revert first.
Committed on main by mistake
The commit should have been on a feature branch, and isn't pushed yet (git status says ahead of 'origin/main' by 1 commit). Make a branch where the commit is, then move main back:
git branch feature/search
git reset --hard origin/main
git switch feature/searchThe new branch keeps the commit, and main matches GitHub again. For a commit that belongs on another existing branch, switch to that branch and git cherry-pick it (Lesson 15), then reset main the same way.
A secret pushed to GitHub
An API key, a password or a .env file reached GitHub. The order of the steps matters:
| Step | Why |
|---|---|
| 1. Revoke the key now | Bots scan GitHub for keys within minutes. Make a new key in the service's dashboard and delete the old one. This is the step that actually fixes it. |
| 2. Stop tracking the file | git rm --cached .env, add .env to .gitignore, commit and push. |
| 3. Optionally, clean the history | The old commit still holds the key. Tools like git filter-repo rewrite every commit to remove it, but that changes every hash after it, and forks and clones keep the old copy anyway. |
Deleting the file in a new commit isn't enough: git log --all -- .env still finds the commit that added it, and so can anyone else.
A fork far behind the original
The fix is the same as in Lesson 19, whatever the gap: git fetch upstream, git switch main, git merge upstream/main, git push. If the fork's main has commits of its own by mistake, move them to a branch first, then git reset --hard upstream/main and git push --force-with-lease, since the fork is yours alone.
Quick reference
| Problem | Fix |
|---|---|
| Lost commits after a reset or rebase | git reflog, then git reset --hard HEAD@{n} |
| Deleted a branch | git branch name hash |
| Wrong last commit, not pushed | git commit --amend |
| Bad commit, already pushed | git revert hash |
| Bad merge, already pushed | git revert -m 1 hash |
| Committed on main instead of a branch | git branch new, git reset --hard origin/main |
| Pushed a secret | Revoke it first, then git rm --cached |
| Cancel a merge or rebase in progress | git merge --abort / git rebase --abort |