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 yetAlready pushed
Safe to rewrite history?Yes, it's only on your computerNo, others may have it
Toolsreset, commit --amend, rebasegit 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:

Terminal
git reset --hard HEAD~2
Output
HEAD is now at d067870 Add homepage

“Add about page” and “Add contact form” are gone from git log. But not from the reflog:

Terminal
git reflog
Output
d067870 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 homepage

HEAD@{1} is where HEAD was one step ago, right before the reset. Go back there:

Terminal
git reset --hard HEAD@{1}
Output
HEAD is now at 2685493 Add contact form

Both commits are back.

A deleted branch

Terminal
git branch -D feature/chat
Output
Deleted 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:

git reflog
2685493 HEAD@{0}: checkout: moving from feature/chat to main
e31b597 HEAD@{1}: commit: Add chat
Terminal
git branch feature/chat e31b597

The 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):

Terminal
git revert HEAD
git push
git log --oneline
98d0127 Revert "Break the contact form"
f55fa74 Break the contact form
2685493 Add contact form

The 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:

Output
error: commit 708e9a0… is a merge but no -m option was given.
fatal: revert failed

A 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:

Terminal
git revert -m 1 HEAD
git push

The 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:

Terminal
git branch feature/search
git reset --hard origin/main
git switch feature/search

The 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:

StepWhy
1. Revoke the key nowBots 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 filegit rm --cached .env, add .env to .gitignore, commit and push.
3. Optionally, clean the historyThe 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

ProblemFix
Lost commits after a reset or rebasegit reflog, then git reset --hard HEAD@{n}
Deleted a branchgit branch name hash
Wrong last commit, not pushedgit commit --amend
Bad commit, already pushedgit revert hash
Bad merge, already pushedgit revert -m 1 hash
Committed on main instead of a branchgit branch new, git reset --hard origin/main
Pushed a secretRevoke it first, then git rm --cached
Cancel a merge or rebase in progressgit merge --abort / git rebase --abort