Git habits that keep a team out of trouble

Most Git disasters are caused by small habits. Here are the boring ones that let a team ship on a Friday without fear, with the commands I use.

By Laercio Borges

· 5 min read

Most Git disasters I have seen were not caused by Git. They were caused by small habits: a commit with forty unrelated changes, a branch that lived for two months, a --force at the end of a long day, a .env file that "nobody will see".

None of the habits below are new. They are boring on purpose. Boring habits are what let a team ship on a Friday without fear.

Commit small, and commit one idea at a time

A commit should do one thing. Fix the login timeout. Rename a function. Add a test. When a commit does one thing, you can read it, review it, revert it and find it later with git bisect.

If you changed several things in one file, you do not have to commit them together. git add -p lets you stage parts of a file:

git add -p src/auth/session.ts
git diff --cached
git commit

Read git diff --cached before every commit. It takes ten seconds and it catches a surprising number of mistakes: a debug console.log, a password in a test, a file you did not mean to touch.

Write messages for the person who debugs this later

That person is often you, six months from now, at night, with production down. Help them.

A good commit message has a short first line that says what the commit does, written like a command, and a body that says why:

Fix login timeout for users with slow connections

Sessions expired after 30 seconds when the token refresh took
longer than the request timeout. Refresh now starts 60 seconds
before expiry and retries once.

Compare that with fix stuff or wip. The first one answers the question everyone asks during an incident: why did this change? Keep the first line under about 50 characters so it fits in git log --oneline and in most tools.

Keep branches short-lived

A branch is cheap to create but expensive to keep. Every day a branch lives, the main branch moves further away and the merge gets harder.

Some habits that help:

  • Name branches by purpose: fix/login-timeout, feat/export-csv, chore/upgrade-node-22.
  • Merge or close a branch within days, not weeks. If the work is big, split it and merge the parts behind a feature flag.
  • Delete branches after they are merged, and clean up local copies of deleted remote branches with git fetch --prune.

Do not rewrite history other people use

Rewriting history is fine on your own branch before anyone else has it. git commit --amend, git rebase -i and git pull --rebase keep your own work clean.

Once a branch is shared, it is different. If you must push a rewritten branch, never use plain --force. Use this instead:

git push --force-with-lease origin feat/export-csv

--force-with-lease refuses to push if someone else pushed to that branch since your last fetch. Plain --force would delete their work without a warning. Protect main completely: nobody force-pushes to it, ever.

Review small changes, and review the why

If a change is too big to review in one sitting, it is too big. Reviewers start to skim, and skimming is how bugs get approved.

When I review, I ask three questions before I read any line of code:

  1. What problem does this change solve? It should be in the description.
  2. How was it tested? "It works on my machine" is a start, not an answer.
  3. What happens if this is wrong? A typo in a page title and a change to payment code do not need the same level of care.

AI review tools can help here. In GitMoon, AI code review on a commit uses tokens from your plan, and the free Starter plan includes 2,500 tokens each month. Use it as a second pair of eyes, not as the reviewer of record. The AI does not know what your customer asked for. You do.

Keep secrets out of the repository from day one

This is the habit that saves the most pain. A secret in Git history is very hard to remove completely. It is in every clone, every fork and every backup.

Start every project with a .gitignore that already covers the usual files:

.env
.env.*
!.env.example
*.pem
*.key
node_modules/
dist/

Commit a .env.example with the variable names and fake values, so new people know what they need without seeing real values:

DATABASE_URL=postgres://user:password@localhost:5432/app
STRIPE_SECRET_KEY=sk_test_replace_me

Then add a check that runs before each commit. A pre-commit hook with a secret scanner such as gitleaks stops most mistakes before they reach a remote. If a secret does get committed, change it at the provider first. Cleaning the history is the second step, not the first. I wrote the full process in how to protect your Git repositories.

Tag what you release

A tag gives a name to the exact code that went to production. When a customer says "it broke after the last update", you can see exactly what changed:

git tag -a v1.4.0 -m "Release 1.4.0: CSV export"
git push origin v1.4.0
git log --oneline v1.3.2..v1.4.0

Use annotated tags (-a) for releases. They store who made the tag and when.

Know your way back

Git almost never loses work that was committed. When something goes wrong, git reflog shows where your branch pointed before, including commits that look gone after a bad rebase or reset:

git reflog
git branch rescue HEAD@{3}

Knowing this one command makes people less afraid of Git, and people who are less afraid of Git invent fewer strange workarounds.

Where GitMoon fits in these habits

I built GitMoon with defaults that match these habits:

  • New repositories are private by default. Public is a choice the owner makes, not an accident.
  • You can manage branches, tags and releases in the dashboard, and see the commit history of each repository.
  • AI code review gives you a second opinion on a commit, paid with tokens from your plan.
  • The Explore feed shows only repositories their owners chose to make public, so sharing your work is opt-in.

GitMoon is still young, and it does not accept git push over SSH or HTTPS today. The habits above work the same on any Git host. If you are thinking about a move, read moving from GitLab or GitHub to GitMoon first, and if you are curious how we got here, the short history of Git hosting starts with tarballs and email.

FAQ

How often should I commit?

Commit every time you finish one small idea that still builds and passes tests. For most people that is several times in an hour of real work, not once a day.

What makes a good commit message?

A short first line, under about 50 characters, that says what the commit does, and a body that says why. Write it for the person who will debug this code later.

Should I use merge or rebase?

Rebase your own branch to keep it clean before you share it. Merge shared branches, and never rewrite history that other people already pulled. If you must force-push your own shared branch, use --force-with-lease.

How do I keep secrets out of Git?

Add .env and key files to .gitignore before the first commit, commit a .env.example with fake values, and run a secret scanner in a pre-commit hook.

Try GitMoon

The Starter plan is free. It has 5 repositories, private by default, and 2,500 AI tokens each month.

Keep reading