Skip to content
Team WorkflowChapter 52 / 57Beginner6 min read

Git Branches & Commits

Git commit message best practices and branch naming, small checkpoint commits, never rewriting pushed history, and the dangers of stash on a shared tree.

On this page

1. What is this practice?

Some parts of this chapter are universal (main stays deployable, small commits, never rewriting pushed history) and some are a house style (one lowercase line, no body). Understand the two separately.

2. Why does it matter?

Git is your undo button and the team's memory at the same time. Big commits cannot be undone, wrong commands can erase other people's work, and rewritten history breaks the team's clones.

Without disciplineWith discipline
Commit straight to mainA short-lived branch, then a PR
A day's work in one commitA small commit at every checkpoint
fixes, wip, updateA clear line saying what changed and why
Amend and force push after pushingPushed history is never rewritten
git add -A picks up everythingStage only your own paths
The branch lingers after mergeDelete it on merge

3. When to follow it

LevelHow importantWhy
Learning projectBranches and clear commit messagesThe earlier you build the Git habit, the better
Production codeThe full rule, with a protected mainA wrong merge or rewrite breaks production
Senior levelBranch protection, PR rules, keeping history cleanGuard rails for the team

4. Bad vs good code

Branch names

bash
git switch -c my-name/fixes
git switch -c ui
git switch -c feature/Payroll_Export
bash
git switch -c feat/payroll-export
git switch -c fix/csv-upload-encoding
git switch -c db/add-invoice-status-index
TypeWhen
feat/A new capability
fix/A bug fix
chore/Tooling, dependencies, cleanup
refactor/No change in behaviour
docs/Documentation only
db/A migration only

Commit messages

bash
git commit -m "fixes"
git commit -m "WIP"
git commit -m "Updated stuff."
bash
git commit -m "reject uploads that are not really csvs"
git commit -m "show invoice total in the export header"
git commit -m "retry webhook delivery twice before failing"

Staging

bash
git add -A
git stash
bash
git add src/invoices/export.ts src/invoices/export.test.ts
git commit -m "add csv export for invoices"

What is the difference?

ThingBadGood
BranchA person's name or something meaninglessType and task
MessageSays nothingWhat changed, and why
StagingEverythingOnly your own files
Shared treestash, reset, cleanWork only on your own paths

5. How to apply it

The git flow for one task
A new branch from main: type/kebab-summary
Do the work, keep the tree working
At every checkpoint stage your own paths and commit
Before pushing, squash only your own unpushed fixups
Open a PR
Delete the branch after merge

House style: one lowercase line

In this style a commit message is a single lowercase line: what changed and why, under 72 characters, no period at the end. No body, no trailer, no attribution line (like "Generated with" or Co-Authored-By).

The reasoning: the branch type already tells the category, small commits do not need long explanations, and reasoning that must last a long time goes into the repo's decisions log, where it is easy to find.

Other teams differ, and that is fine too:

StyleExampleWhere it is common
Conventional Commitsfeat(api): add csv exportOpen source, automatic changelog and versioning
Subject + bodyA summary on the first line, a blank line, then a paragraphThe Linux kernel, git itself
TrailersCo-Authored-By, Signed-off-byPair programming, projects that use DCO

Follow the style of the team you are on. What matters is consistency and a clear "what and why".

Universal rules

  • Never change a pushed commit with --amend or a force push. It breaks other people's history.
  • Squash fixups only on your own unpushed branch.
  • Do not squash the whole repo history into one commit, all the reasoning is lost.
  • On a shared tree, git stash, git checkout <path>, git reset, git clean and git restore can take or erase someone else's work. Run them only when you are sure the tree holds only your work.

6. Exceptions and common mistakes

When the rule does not apply: in a solo learning repo, committing straight to main is fine. Release branches (v1) are fine in products that have a staging lane and where the README says which branch deploys where.

Mistake: One big commit at the end of the day

Why? If a bug shows up you cannot undo half the work, and review becomes hard.

Better: Make a small commit at every working checkpoint.

Mistake: Amend or force push after pushing

Why? Teammates' local history diverges from yours and work can be lost.

Better: Fix the mistake with a new commit.

Mistake: git stash on a shared tree

Why? Stash picks up every uncommitted change, not just yours.

Better: Commit your own files explicitly, or work in a separate worktree.

7. What to learn before and after

Final quick revision

CheckYes?
Branch is type/kebab-summary
Small checkpoint commits
Message: what and why
Stage only your own paths
Pushed history not rewritten
Branch deleted after merge