Git Commit Message Best Practices
In this page:
git commit -m "Summary line in imperative mood" -m "Longer explanation of what and why."
git commit -m "type: short description"
एक अच्छे Commit Message की Structure
Convention एक छोटी summary line है (ideally ~50 characters से कम), एक खाली line, फिर एक body paragraph जो change के पीछे के reasoning को explain करता है। summary अकेले एक-line git log --oneline list में सही sense देना चाहिए; body वह जगह है जहाँ आप *क्यों* explain करते हैं, क्योंकि diff खुद पहले से *क्या* दिखाता है।
उदाहरण: The Structure of a Good Commit Message
git commit -m "Fix null pointer in checkout flow" -m "The cart total crashed when the discount field was empty because it was never defaulted to zero."
Semantic Prefixes इस्तेमाल करना
feat:, fix:, docs:, और style: (Conventional Commits convention का हिस्सा) जैसे prefixes आपको एक नज़र में बताने देते हैं कि एक commit किस तरह का change है बिना इसका पूरा message पढ़े, और changelog generators जैसे tools release notes बनाने के लिए उन्हें automatically parse कर सकते हैं।
उदाहरण: Using Semantic Prefixes
git commit -m "feat: add dark mode toggle"
git commit -m "fix: correct off-by-one error in pagination"
Messages को Imperative रखना
"Added login validation" के बजाय "Add login validation" लिखना उस phrasing से match करता है जो Git के अपने auto-generated messages इस्तेमाल करते हैं (जैसे "Merge branch"), और यह apply करने पर यह commit *क्या करता है* describe करने वाले एक command जैसा naturally पढ़ता है — एक convention जो एक mixed-author log को style-consistent रखता है।
उदाहरण: Keeping Messages Imperative
git commit -m "Add login validation"
Issues और Pull Requests Reference करना
किसी commit या PR description में Closes #15 (या Fixes, Resolves) लिखना सिर्फ documentation नहीं है — GitHub, GitLab, और similar platforms उन exact keywords को parse करते हैं और commit merge होते ही automatically referenced issue बंद कर देते हैं।
उदाहरण: Referencing Issues and Pull Requests
git commit -m "Fix broken image upload" -m "Closes #15"
एक Commit Message Amend करना
git commit --amend आपकी last commit का message editor में खोलता है fix करने के लिए, एक नई commit add करने के बजाय commit को पूरी तरह replace करते हुए — लेकिन यह सिर्फ push करने से पहले करें, क्योंकि एक pushed commit amend करना उसका hash बदल देता है और sync करने के लिए एक force push चाहिए होता है।
उदाहरण: Amending a Commit Message
git commit --amend -m "Fix typo in login form label"
- लगभग 50 characters से ज़्यादा लंबी एक subject line लिखना, जो
git log --onelineऔर hosting UIs में cut off हो जाती है। - subject और body के बीच खाली line skip करना, ताकि tools body को title का हिस्सा मान सकें।
Add loginके बजायAdded loginजैसे past tense में लिखना, जो Git के अपनेMerge branchजैसे messages से match नहीं करता।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: