Git Anti-patterns
Git anti-patterns
Recognise risky Git practices, reproduce their consequences safely, and replace them with reviewable workflows.
Explain why unreviewed changes and history rewriting can harm a team.
Use feature and hotfix branches rather than editing the mainline directly.
Investigate diffs and evidence before accepting a change.
Common anti-patterns
| Anti-pattern | Problem | Prefer instead |
|---|---|---|
Developing features directly on main | Unfinished work becomes the default branch | Short-lived feature branch and reviewed integration |
Committing urgent fixes directly to main | Urgency can bypass verification | Small hotfix branch, focused test and expedited review |
| Self-approving a pull request | Missing independent review | A collaborator checks functionality, clarity and scope |
| Force-pushing a shared mainline | Other developers’ history can be invalidated | Protect mainline branches; coordinate history changes |
| Large, unrelated commits | Hard to review or revert | Small, purposeful commits |
| Committing secrets | Credentials may persist in repository history | Environment variables, secret managers and immediate revocation |
Practical: replace a direct commit with a reviewed change
-
Create a fresh local repository.
mkdir -p ~/git-antipattern-lab && cd ~/git-antipattern-lab git init -b main git config user.name "Git Student" git config user.email "student@example.invalid" printf '# Project rules\n\nKeep documentation accurate.\n' > README.md git add README.md git commit -m "docs: initialise rules" -
Create a feature branch and deliberately introduce a typo in a new file.
git switch -c feature/install-guide printf 'Instalation steps:\n1. Connect the board.\n' > INSTALL.md git add INSTALL.md git commit -m "docs: add installation guide" -
Review the proposed change before merging.
git diff main...feature/install-guide git log --oneline main..feature/install-guide
-
Fix the spelling in
INSTALL.mdusing a text editor (or the command below); keep the installation instructions intact.sed -i 's/Instalation/Installation/' INSTALL.md -
Commit the corrected file and inspect the complete feature diff again.
git add INSTALL.md git commit -m "docs: correct installation heading" git diff main...feature/install-guide -
Integrate the reviewed feature locally and confirm the result.
git switch main git merge --no-ff feature/install-guide -m "merge: installation guide" git log --oneline --graph --decorate --all cat INSTALL.md
Production defects and hotfixes
-
Start from the current, stable
mainand create a hotfix branch.git switch main git switch -c hotfix/install-heading -
Add a short troubleshooting instruction to
INSTALL.md, then commit it.printf '\nIf the board is not detected, check the USB cable.\n' >> INSTALL.md git add INSTALL.md git commit -m "fix: document USB troubleshooting" -
Inspect the change and return it to
mainusing a merge.git diff main...hotfix/install-heading git switch main git merge --no-ff hotfix/install-heading -m "merge: USB documentation hotfix"