Sefism is free for learners. We are here to help you grow, one step at a time.Join for free
Sefism.

Best practices · Stacks and tools

Git Best Practices for Developers

Opens with credits

A practical guide to using Git well on a team, checked against the official Git documentation and the Pro Git book. It covers commits and messages, branches and pull requests, rebase vs merge, safe history rewriting, keeping secrets out, .gitignore, commit signing and how to recover from mistakes.

Written by Tauseef Fayyaz7 chapters, 26 practicesAbout 15 min24 quiz questionsReviewed 12 October 2026

What you will learn

7 chapters · about 15 min

  1. 1Commits and commit messagesHow big a commit should be and how to write a message that still makes sense a year from now.4 practices · 3 min
  2. 2Branches and pull requestsA branching flow that keeps the main branch releasable and pull requests easy to review.4 practices · 2 min
  3. Rebase vs mergeWhen to rebase, when to merge, and the one rule that keeps rebasing safe.4 practices · 2 minOpens with credits
  4. Rewriting history safelyHow to force push without losing anyone's work, and how to undo commits that are already shared.3 practices · 2 minOpens with credits
  5. Secrets and .gitignoreKeep credentials and generated files out of the repository from day one, and know exactly what to do if a secret leaks.4 practices · 2 minOpens with credits
  6. Signing commits and tagsProve that commits and releases really come from you, using the SSH key you probably already have.3 practices · 2 minOpens with credits
  7. Recovering from mistakesAlmost nothing you commit is ever really lost. These are the commands that get you out of the usual messes.4 practices · 2 minOpens with credits

About this guide

These Git best practices are for developers who use Git every day and want a history that teammates can read, review and trust. They are checked against the official documentation on git-scm.com for Git 2.56, the Pro Git book, and GitHub's own guides on signing and removing sensitive data.

Good Git habits pay off in small ways all the time. A clear commit message saves a reviewer ten minutes. A small pull request gets merged the same day. Pushing with --force-with-lease instead of --force means you never wipe out a teammate's work. Knowing about the reflog turns a scary mistake into a two-minute fix.

The first two chapters, on commits and on branches and pull requests, cover what reviewers and future maintainers notice most, so start there. Later chapters cover rebase vs merge, rewriting history safely, keeping secrets and junk out of the repository, signing your commits, and recovering lost work.

If you are a student or a fresh graduate, your GitHub history is often the first code a hiring team looks at. Clean commits and readable pull requests are an easy way to show you can work on a real team.

Common questions

What is the difference between git rebase and git merge?

Merge joins two branches with a merge commit and keeps both histories as they were. Rebase replays your commits on top of another branch, creating new commits and a straight history. Rebase your own unshared work to stay current, and never rebase commits others may have based work on.

Is git push --force-with-lease safe?

It is much safer than --force, because it refuses to overwrite the remote branch if someone else pushed since you last fetched. Background fetches by editors can weaken that check, so add --force-if-includes too. Never force push to main or other shared branches.

How do I recover a commit I lost after git reset --hard?

Run git reflog to list where HEAD has been, find the commit from before the reset, and create a branch on it with git branch rescue <hash>. Reflog entries are kept for 90 days by default. Uncommitted changes that were discarded are not in the reflog and usually cannot be recovered.

What should I do if I committed an API key to Git?

Revoke or rotate the key first, since anything pushed should be treated as copied. Then, if needed, remove it from history with git filter-repo --sensitive-data-removal, force push, and ask collaborators to re-clone. Forks and existing clones keep the old commits, which is why rotation matters most.

How long should a git commit message be?

The Git documentation recommends a summary line of no more than 50 characters, then a blank line, then a body explaining the change in more detail. Many teams wrap the body at about 72 characters. Focus the body on why the change was made.

Should I sign my Git commits?

Yes, it is cheap and proves a commit came from you. The easiest way is SSH signing: set gpg.format to ssh, point user.signingkey at your public key, enable commit.gpgsign, and add the key to your Git host as a signing key.

Sources (8)

Checked against these, last on 12 October 2026.

  1. git-commit Documentation, Gitgit-scm.com/docs/git-commit
  2. git-push Documentation, Gitgit-scm.com/docs/git-push
  3. git-reflog Documentation, Gitgit-scm.com/docs/git-reflog
  4. gitignore Documentation, Gitgit-scm.com/docs/gitignore
  5. Git Branching: Rebasing, Pro Gitgit-scm.com/book/en/v2/Git-Branching-Rebasing
  6. git-config Documentation, Gitgit-scm.com/docs/git-config
  7. Removing sensitive data from a repository, GitHub Docsdocs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository
  8. Telling Git about your signing key, GitHub Docsdocs.github.com/en/authentication/managing-commit-signature-verification/telling-git-about-your-signing-key

Picked from the other libraries by shared topics.

Project structures

See all
Work with me

Stuck on something specific?

Writing only gets you so far. If you want an answer to your situation rather than the general case, book a session and we will work through it together. Sessions are free for approved Sefism members, and a few slots open each week.

  • 1:1 Mentorship
  • Fresh Graduate Mentorship
  • Career Guidance

Brand deals, promos and partnerships: tauseeffayyaz@gmail.com