10 Common Git Production Mistakes and How to Fix Them
The article lists ten typical Git production accidents—such as deleting an unmerged branch, losing changes with reset --hard, committing sensitive data, wrong merges, and force‑push overwrites—explains each scenario, provides exact command‑line recovery steps, analyzes their frequency and severity, and offers preventive best practices and a quick‑reference cheat sheet.
Ten common Git production accidents
Accident 1: Deleting an unmerged branch
Scenario: Accidentally deleted a branch that had not been merged or pushed.
# git branch -D feature/payment
# Oops! The branch and its commits are gone.Fix: Use git reflog to locate the last commit of the branch and recreate it.
# git reflog
# a1b2c3d HEAD@{5}: checkout: moving from feature/payment to main
# git checkout -b feature/payment a1b2c3d
# Branch restored.Accident 2: git reset --hard discards changes
Scenario: Ran git reset --hard HEAD~1 and lost recent modifications.
# git reset --hard HEAD~1
# All changes are gone!Fix: Find the lost commit via git reflog and reset to it.
# git reflog
# d4e5f6g HEAD@{0}: reset: moving to HEAD~1
# a1b2c3d HEAD@{1}: commit: feat: payment feature
# git reset --hard a1b2c3dAccident 3: Committing sensitive information
Scenario: A secret key or password was accidentally added and committed.
# git add .
# git commit -m "feat: config"
# .env file was also committed!Fix: Rotate the compromised key immediately, then purge the secret from history using git filter-branch or the BFG tool, and force‑push the cleaned history.
# git filter-branch --force --index-filter "git rm --cached --ignore-unmatch .env" \
--prune-empty --tag-name-filter cat -- --all
# or use BFG:
# java -jar bfg.jar --delete-files .env
# git push --force --allAccident 4: Merging the wrong branch
Scenario: Merged feature/payment into main before the feature was ready.
# git checkout main
# git merge feature/payment
# Realized the feature wasn't tested yet!Fix (not pushed): Reset to the state before the merge.
# git reset --hard ORIG_HEAD
# or
# git reset --hard HEAD~1Fix (already pushed): Revert the merge commit.
# git revert -m 1 HEAD
# git push origin mainAccident 5: Pushing to the wrong branch
Scenario: Directly pushed commits to main instead of a feature branch.
# git checkout main
# git add .
# git commit -m "feat: new feature"
# git push origin main
# Oops – should not push directly to main!Fix: Revert the unwanted commit on main, then move the changes to the correct branch.
# git revert HEAD
# git push origin main
# git checkout -b feature/correct
# git cherry-pick <commit-hash>
# git push origin feature/correctAccident 6: Rebase conflict after push is rejected
Scenario: Rebased a shared branch and the subsequent push was rejected.
# git checkout feature
# git rebase main
# git push origin feature
# ! [rejected] feature -> feature (non-fast-forward)Fix: If you are the sole contributor, force‑push with lease; otherwise abort the rebase and merge instead.
# git push --force-with-lease
# or
# git rebase --abort
# git merge mainAccident 7: Wrong conflict resolution
Scenario: During a merge conflict, the --ours version was chosen, discarding the correct remote changes.
# git merge feature
# git checkout --ours src/OrderService.java
# git add .
# git commitFix: If not yet committed, reset and redo the merge; if already committed, reset to the previous commit and resolve again.
# git reset --hard HEAD
# git merge feature
# (resolve conflicts again)
# or
# git reset --hard HEAD~1
# git merge feature
# (resolve conflicts again)Accident 8: Force‑push overwrites teammates' commits
Scenario: Executing git push --force removed a teammate's work.
# git push --force
# Teammate's commit disappears!Fix: Recover the lost commit from the teammate's local repository or from git reflog, then push the restored history and ask everyone to re‑pull.
# git reflog
# git reset --hard <hash>
# git push
# (notify the team to pull again)Accident 9: Losing uncommitted changes when switching branches
Scenario: Forgetting to stash before git checkout feature results in the warning “Your local changes would be overwritten”. Using git checkout -f discards the changes.
# git checkout feature
# error: Your local changes would be overwritten by checkout.
# git checkout -f feature
# Changes are lost!Fix: If no other work was done, switch back to the original branch; otherwise use git reflog to locate the lost changes.
# git checkout -f main
# (changes should still be there if not overwritten)
# git reflogAccident 10: Cherry‑picking the wrong commit
Scenario: A wrong commit was cherry‑picked onto a release branch.
# git checkout release
# git cherry-pick <commit-hash>
# This commit should not be on release.Fix: If not pushed, reset the branch; if already pushed, revert the commit.
# git reset --hard HEAD~1 # not pushed yet
# or
# git revert HEAD
# git pushAccident frequency analysis (personal experience)
Each accident type is rated by occurrence frequency and danger level using star symbols (⭐). The most frequent and dangerous issues are accidental branch deletion, reset --hard loss, and committing sensitive information.
Prevention measures
Command safety replacements
Replace git push --force with git push --force-with-lease (checks remote updates first).
Replace git reset --hard with a combination of git stash + git reset to avoid losing work.
Replace git branch -D with git branch -d after confirming the branch is merged.
Replace git checkout -f with git stash followed by git checkout to keep local changes.
Alias configuration
# .gitconfig
[alias]
pf = push --force-with-lease
graph = log --graph --oneline --all
re = reflog
brd = "!git branch -d"
brD = "!git branch -D"Team guidelines
Branch protection: Disallow direct pushes to main.
Code review: All merges must go through pull‑request approval.
CI/CD: Commits must pass automated tests before merging.
Sensitive data: Use .gitignore to exclude secret files.
Commit messages: Enforce standards with tools like commitlint.
Emergency cheat sheet
Deleted branch: git checkout -b <branch> <hash> Reset --hard regret: git reset --hard <hash> Sensitive info committed: git filter-branch or BFG
Wrong merge (not pushed): git reset --hard ORIG_HEAD Wrong merge (pushed): git revert -m 1 HEAD Push to wrong branch (pushed): git revert then git cherry-pick <commit-hash> to correct branch
Rebase conflict: git rebase --abort Force‑push overwrite: git reset --hard <hash> (recover from teammate or reflog)
Lost uncommitted changes: git stash then git stash pop Cherry‑pick error: git reset --hard HEAD~1 (if not pushed) or git revert HEAD (if pushed)
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Coder Trainee
Experienced in Java and Python, we share and learn together. For submissions or collaborations, DM us.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
