← Back to Git Course | Chapter 3: Branching | Lesson 6 of 9

Git rebase Command

Rebasing आपके finished pages उठाकर उन्हें story के newest version के अंत में फिर से glue करने जैसा है, ताकि ऐसा लगे कि आपने उन्हें last में एक straight line में लिखा।
Syntax
bash
git checkout feature_branch
git rebase base_branch
git rebase -i base_branch
git rebase --continue

Basic Rebase

Rebasing commits की एक sequence लेता है और उन्हें एक-एक करके एक अलग base commit के ऊपर replay करता है, एक merge द्वारा छोड़ी जाने वाली branching structure की तुलना में एक cleaner, linear-दिखने वाली history produce करते हुए।

उदाहरण: Basic Rebase

bash
git checkout feature-x
git rebase main

Rebase Continue करना

अगर rebase के दौरान किसी commit को replay करना एक conflict पैदा करे, Git operation के बीच में pause हो जाता है -- आप affected files में conflict resolve करते हैं, उन्हें stage करते हैं, फिर बाकी commits replay करना resume करने के लिए git rebase --continue चलाते हैं।

उदाहरण: Continuing Rebase

bash
git add file.txt
git rebase --continue

Interactive Rebase

git rebase -i <commit> आपकी हाल की commits list करने वाला एक interactive editor खोलता है, आपको messages reword करने, कई commits को एक में squash करने, उन्हें reorder करने, या जिन्हें अब नहीं चाहिए उन्हें drop करने देते हुए -- history share करने से पहले इसे साफ करने के लिए powerful।

उदाहरण: Interactive Rebase

bash
git rebase -i HEAD~3

किसी दूसरी Branch पर Rebase करना

git rebase --onto <newbase> <oldbase> <branch> सिर्फ आपकी branch की unique commits को एक अलग target पर replay करता है, एक intermediate branch को पूरी तरह skip करते हुए -- तब उपयोगी जब आपकी branch का actual parent बदल गया हो।

उदाहरण: Rebasing Onto Another Branch

bash
git rebase --onto main old-feature new-feature

Safe Rebase Practices

कभी उन commits को rebase न करें जो पहले से pushed हैं और जिन पर दूसरों ने build किया हो -- ऐसा करना उस history को rewrite करता है जिसे दूसरे लोगों के clones अभी भी reference करते हैं। अगर आपको बिल्कुल एक rebased branch push करनी है, plain force push के बजाय --force-with-lease इस्तेमाल करें, जो कम से कम किसी और के नए commits को चुपचाप overwrite होने से बचाता है।

उदाहरण: Safe Rebase Practices

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. एक ऐसी branch rebase करना जिसे दूसरे पहले से pull कर चुके हैं, जो shared history rewrite करता है और teammates के लिए problems का कारण बनता है।
  2. एक conflict के दौरान git add और git rebase --continue के बजाय git commit चलाना।
  3. गलत branch पर rebase करना, जैसे feature पर git rebase feature चलाना main के बजाय feature पर git rebase main के बजाय।

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.