Skip to content
Team WorkflowChapter 52 / 57Beginner6 min read

Git Branches & Commits

Git commit message best practices aur branch naming, chhote checkpoint commits, pushed history na badalna aur shared tree par stash ke khatre.

On this page

1. Yeh practice kya hai?

Is chapter ke kuch hisse universal hain (main deployable, chhote commits, pushed history na badalna) aur kuch ek house style hain (lowercase ek line, koi body nahi). Dono ko alag alag samjhein.

2. Kyun zaroori hai?

Git aap ka undo button aur team ki yaaddasht dono hai. Bari commits undo nahi ho saktin, ghalat commands doosron ka kaam mita deti hain, aur rewrite ki hui history team ke clones tor deti hai.

Bina disciplineDiscipline ke saath
Seedha main par commitShort-lived branch, phir PR
Ek din ka kaam ek commit meinHar checkpoint par chhota commit
fixes, wip, updateKya badla aur kyun, saaf line
Push ke baad amend aur force pushPushed history kabhi rewrite nahi
git add -A sab utha leta haiSirf apne paths stage
Branch merge ke baad pari rehti haiMerge par delete

3. Kab follow karein?

LevelKitna zarooriKyun
Learning projectBranches aur saaf commit messagesGit ki aadat jitni jaldi utna acha
Production codePoora rule, protected main ke saathGhalat merge ya rewrite production torta hai
Senior levelBranch protection, PR rules, history ki safaiTeam ke liye guard rails

4. Bura vs acha code

Branch names

bash
git switch -c mera-naam/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
TypeKab
feat/Nayi capability
fix/Bug fix
chore/Tooling, dependencies, cleanup
refactor/Behaviour mein koi change nahi
docs/Sirf documentation
db/Sirf migration

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"

Farq kya hai?

CheezBuraAcha
BranchInsaan ka naam ya bemaaniType aur kaam
MessageKuch nahi batataKya badla, kyun
StagingSab kuchSirf apni files
Shared treestash, reset, cleanSirf apne paths par kaam

5. Kaise apply karein?

Ek task ka git flow
main se nayi branch: type/kebab-summary
Kaam karein, tree chalta rahe
Har checkpoint par apne paths stage aur commit
Push se pehle sirf apni unpushed fixups squash
PR kholein
Merge ke baad branch delete

House style: ek lowercase line

Is style mein commit message ek hi lowercase line hai: kya badla aur kyun, 72 characters se kam, aakhir mein period nahi. Koi body nahi, koi trailer nahi, koi attribution line nahi (jaise "Generated with" ya Co-Authored-By).

Dalil: branch type pehle se category batata hai, chhote commits ko lambi wazahat ki zaroorat nahi, aur jo reasoning lambe arse tak chahiye woh repo ke decisions log mein jati hai, jahan dhoondhna aasaan hai.

Doosri teams mukhtalif hain, aur woh bhi theek hain:

StyleMisaalKahan aam
Conventional Commitsfeat(api): add csv exportOpen source, auto changelog aur versioning
Subject + bodyPehli line summary, khaali line, phir paragraphLinux kernel, git khud
TrailersCo-Authored-By, Signed-off-byPair programming, DCO wale projects

Jis team mein hon, us ka style follow karein. Asal cheez consistency aur saaf "kya aur kyun" hai.

Universal rules

  • Push hui commit kabhi --amend ya force push se na badlein. Doosron ki history toot jati hai.
  • Fixups sirf apni unpushed branch par squash karein.
  • Poori repo history ko ek commit mein squash na karein, saari reasoning mit jati hai.
  • Shared tree par git stash, git checkout <path>, git reset, git clean, git restore doosre ka kaam le ya mita sakte hain. Inhein sirf tab chalayein jab yaqeen ho ke tree mein sirf aap ka kaam hai.

6. Exceptions aur common mistakes

Kab rule nahi lagta: akelay ke learning repo mein main par seedha commit chal jata hai. Release branches (v1) un products mein theek hain jahan staging lane ho aur README mein likha ho kaunsi branch kahan deploy hoti hai.

Galati: Ek bara commit, din ke aakhir mein

Why? Bug aaye to aadha kaam undo nahi ho sakta, aur review mushkil ho jata hai.

Behtar: Har chalte checkpoint par chhota commit karein.

Galati: Push ke baad amend ya force push

Why? Teammates ki local history aap ki history se alag ho jati hai aur kaam kho sakta hai.

Behtar: Nayi commit se ghalti theek karein.

Galati: Shared tree par git stash

Why? Stash har uncommitted change utha leta hai, sirf aap ka nahi.

Behtar: Apni files explicitly commit karein, ya alag worktree mein kaam karein.

7. Pehle aur baad mein kya seekhein?

Final quick revision

CheckHaan?
Branch type/kebab-summary
Chhote checkpoint commits
Message: kya aur kyun
Sirf apne paths stage
Pushed history rewrite nahi
Merge ke baad branch delete