← Back to Git Course | Chapter 7: Collaboration | Lesson 5 of 6

Git Squash Commits

Commits squash करना submit करने से पहले बहुत सारे messy draft pages को एक clean final page में combine करने जैसा है।
Syntax
bash
git rebase -i HEAD~number_of_commits
# change "pick" to "squash" on the commits to combine

Squashing का मतलब क्या है?

Squashing कई छोटी, अक्सर messy commits ("wip", "fix typo", "actually fix it") को एक clean commit में merge करता है जो finished change को एक single logical unit की तरह represent करता है — तब उपयोगी जब आपकी in-progress commit history काम करते समय आपके लिए ज़्यादा उपयोगी है बजाय बाद में log पढ़ने वाले किसी के लिए।

उदाहरण: What does Squashing Mean?

bash
git log --oneline
# wip
# fix typo
# actually fix it

Interactive Rebase Instructions इस्तेमाल करना

git rebase -i <base> आपकी commits list करने वाला एक editor खोलता है; पहली को pick छोड़ना और इसके बाद हर commit को squash (या s) में बदलना Git को बताता है कि rebase चलने पर उन हर एक को इसके ऊपर वाली में fold कर दे।

उदाहरण: Using interactive rebase instructions

bash
git rebase -i HEAD~3
# pick a1b2c3d Add login form
# squash e4f5g6h fix typo
# squash h7i8j9k actually fix it

Combined Commit Message लिखना

rebase चलने के बाद, Git सभी squashed commits से messages combine करता है और आपके पूरे change को describe करने वाला एक final message लिखने के लिए एक editor खोलता है, आपकी choice के हिसाब से originals के हिस्से discard या रखते हुए।

उदाहरण: Writing the Combined Commit Message

bash
git rebase -i HEAD~3
# combine messages into one final message describing the change

एक Merge के दौरान Squashing

कई hosting platforms एक pull request पर एक button की तरह squash-merge offer करते हैं, merge time पर branch की हर commit को target branch पर एक commit में collapse करते हुए — यह आपको खुद एक interactive rebase चलाए बिना same clean-history result देता है।

उदाहरण: Squashing during a Merge

bash
git merge --squash feature-x
git commit -m "Add feature x"

Squashed Commits Push करना

पहले से pushed commits को squash करना history rewrite करता है, इसलिए एक plain git push reject हो जाएगा — remote branch update करने के लिए आपको git push --force-with-lease चाहिए, और किसी ऐसी branch पर ऐसा करना जिसे कोई और पहले से pull कर चुका है उनकी local history diverge करवाएगा और उन्हें real pain देगा।

उदाहरण: Pushing Squashed Commits

bash
git push --force-with-lease
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. हर commit को squash mark करना, पहली सहित, जबकि पहली को pick रहना ज़रूरी है।
  2. पहले से pushed commits squash करना, फिर एक force push चाहिए होना और दूसरों को disrupt करना।
  3. git rebase -i HEAD~3 गलत count के साथ इस्तेमाल करना, ताकि इसमें बहुत कम या बहुत ज़्यादा commits शामिल हो जाएँ।
🔒

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.