TExcelTAD
Home › Git › Git Best Practices

Git Best Practices

Git lets you do almost anything, including things you'll regret. These habits keep a history useful and a team happy.

1. Commit small, focused changes

One commit should do one thing: fix one bug, add one function, rename one concept. Small commits are easier to review, to understand later, to revert and to cherry-pick. If your message needs the word "and", consider two commits.

You can split work in the working folder into separate commits by staging selectively:

bash · try it yourself
mkdir app && cd app && git init -q
printf 'def total(items):\n    return sum(items)\n' > cart.py
printf '# Shop\n' > README.md
git add . && git commit -q -m "Initial shop code"
printf 'def total(items):\n    return round(sum(items), 2)\n' > cart.py
printf '# Shop\n\nRun with python3 shop.py\n' > README.md
git add cart.py && git commit -q -m "Round cart total to cents"
git add README.md && git commit -q -m "Document how to run the shop"
git log --oneline --stat
4bb02d8 Document how to run the shop
 README.md | 2 ++
 1 file changed, 2 insertions(+)
f60593c Round cart total to cents
 cart.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
0894da6 Initial shop code
 README.md | 1 +
 cart.py   | 2 ++
 2 files changed, 3 insertions(+)

2. Write messages for the future reader

Weak Better
fix Fix crash when cart is empty
changes Rename User.mail to User.email
update stuff Upgrade parser to 2.1 for Unicode support

A summary line in the imperative mood (≈50 chars), a blank line, then why in the body. Some teams use the Conventional Commits prefixes, which tools can turn into changelogs: feat: add search, fix: handle empty cart, docs: explain setup.

3. Never commit secrets or generated files

Passwords, API keys and .env files must stay out of the repository: once pushed, assume a secret is leaked, even if you delete it in a later commit (it remains in history). Build output (dist/, node_modules/, *.class) is reproducible and only adds noise. List them in .gitignore before the first commit:

bash · try it yourself
mkdir api && cd api && git init -q
printf '.env\nnode_modules/\ndist/\n' > .gitignore
echo "DB_PASSWORD=hunter2" > .env
mkdir node_modules dist && touch node_modules/lib.js dist/app.js
echo "console.log('api')" > server.js
git add .
git status -s
A  .gitignore
A  server.js

Only the .gitignore and the real source file were staged.

4. Pull before you push, and keep branches short

Integrate others' work often (git pull, or merge main into your branch). A branch that lives for weeks drifts far from main and ends in a painful merge.

5. Don't rewrite shared history

Commands that change existing commits (commit --amend, rebase, reset to an older commit followed by a force push) are fine on commits only you have. Once others have pulled them, rewriting forces everyone to repair their copies. On shared branches, undo with git revert instead, which adds a new commit.

bash · try it yourself
mkdir app && cd app && git init -q
echo "price = 10" > config.txt && git add . && git commit -q -m "Set price"
echo "price = 1000" > config.txt && git commit -q -am "Raise price"
# The bad commit is already shared: undo it with a new commit
git revert --no-edit HEAD
git log --oneline
cat config.txt
[main 3bc3346] Revert "Raise price"
 Date: Thu Jan 15 10:00:00 2026 +0000
 1 file changed, 1 insertion(+), 1 deletion(-)
3bc3346 Revert "Raise price"
9e40e3b Raise price
31ea0cd Set price
price = 10

6. Use branches and pull requests

Keep main releasable. Do work on branches, open a pull request, and let someone review it. Protect main on your hosting service so nobody pushes to it directly.

7. Check before you act

git status before committing, git diff --staged before committing, git log --oneline --graph --all before merging or rebasing. They take seconds and prevent most accidents.

Quick checklist

  • One logical change per commit, with a clear message
  • .gitignore set up; no secrets, no build output
  • Branch per task; merge or rebase on main often
  • Review the staged diff before committing
  • Never force-push shared branches; prefer revert
  • Tag releases (git tag -a v1.2.0)

Test yourself

You accidentally pushed an API key in a commit. What is the right first step?

  1. Delete the file in a new commit; that removes it
  2. Revoke/rotate the key immediately: it must be treated as leaked
  3. Rename the file
  4. Nothing, private repositories are safe

How should you undo a bad commit that teammates have already pulled?

  1. git reset --hard and force push
  2. git revert <commit>
  3. Delete the repository
  4. git commit --amend

Which commit message follows the recommended style?

  1. Updated files
  2. Fix rounding error in invoice totals
  3. fixed a bug and also refactored stuff and updated docs
  4. asdf