← Back to Git Course | Chapter 8: Best Practices | Lesson 2 of 6

Git Commit Message Best Practices

एक अच्छा commit message एक moving box पर एक clear label जैसा है: ऊपर एक छोटा title और, ज़रूरत पड़े तो, कुछ lines जो बताएँ कि अंदर क्या है और क्यों।
Syntax
bash
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

bash
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

bash
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

bash
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

bash
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

bash
git commit --amend -m "Fix typo in login form label"
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. लगभग 50 characters से ज़्यादा लंबी एक subject line लिखना, जो git log --oneline और hosting UIs में cut off हो जाती है।
  2. subject और body के बीच खाली line skip करना, ताकि tools body को title का हिस्सा मान सकें।
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.