How to Keep Your Git History Clean: 7 Habits for Teams

By GitusAI · · 5 min read

A clean git history is not about aesthetics. It makes code review faster, git bisect reliable, reverts safe and onboarding easier. These seven habits get most teams most of the way there.

1. Make atomic commits

Each commit should do one thing and leave the project in a working state. Use git add -p to stage hunks selectively when your working tree contains more than one logical change.

2. Write messages for the reader

Use a clear subject in the imperative mood and explain the reason in the body when it is not obvious. Agree on a convention, such as Conventional Commits, so everyone writes the same way.

3. Do not commit generated or unrelated files

Keep build output, editor settings and secrets out of the repository with a good .gitignore. Review git status before every commit.

4. Clean up before you share

Local commits like "wip" and "fix typo" are fine while you work. Before opening a pull request, tidy them with interactive rebase:

git rebase -i origin/main
# mark fixups as 'f' or 'fixup', reword unclear messages with 'r'

Only rewrite commits that have not been shared with others. Rewriting a shared branch forces everyone else to recover.

5. Choose a merge strategy and stick to it

  • Squash merge: one commit per pull request. Simple linear history, but loses internal steps.
  • Rebase and merge: preserves individual commits in a linear history. Needs well-crafted commits.
  • Merge commit: preserves full context at the cost of a noisier graph.

Any of these works; mixing them randomly does not.

6. Keep pull requests small

Small changes get reviewed carefully; large ones get skimmed. Aim for a diff someone can understand in one sitting, and split refactors from behaviour changes.

7. Automate the boring parts

Use hooks and CI to lint messages, and use tooling for the repetitive work of writing them. GitusAI drafts a message from your staged changes in the VS Code source control panel, so following your team's convention takes a click rather than a context switch. Combined with atomic commits, it makes a tidy history the path of least resistance.

More from the blog