Ravindra BagaleCourses & study guides मराठी Track your progress

Guides

Git Rebase vs Merge: कब क्या इस्तेमाल करें (उदाहरणों के साथ)

Merge join commit रखता है; rebase commits replay करता है। सिर्फ local rebase; shared main PR से merge।

Friends, merge keeps the story; rebase rewrites the story. Chala mitrano — never rebase a branch others already pulled.

Quick answer

Hold this vertical picture:

  1. Diverged branches देखें।
  2. Merge आज़माएँ।
  3. Rebase आज़माएँ।
  4. Shared rebase न करें।
  5. Decision card मानें।

Language: English · मराठी · हिंदी

Feature branchYour commits Main moved aheadNew commits on main Option A: mergeExtra merge commit Option B: rebaseReplay your commits Push carefullyNo force on shared
Rebase vs merge

यह चुनाव क्यों ज़रूरी

TCS/Accenture में उलझा history reviews धीमा करता है। Merge और rebase दोनों काम जोड़ते हैं — अलग कहानियाँ।

Why: Reviews/rollbacks history shape पर निर्भर। What: merge = join commit; rebase = replay। How: Lab पर अभ्यास, फिर team policy।

Example 1: Lab with main and feature

Type this:

rm -rf /tmp/rb-lab && mkdir -p /tmp/rb-lab && cd /tmp/rb-lab && git init -b main && git config user.email lab@example.com && git config user.name Lab && printf 'v1\n' > app.txt && git add app.txt && git commit -m 'main1' && git checkout -b feature && printf 'feat\n' >> app.txt && git commit -am 'feat1' && git checkout main && printf 'main2\n' >> app.txt && git commit -am 'main2' && git log --oneline --all --graph

You will see:

* main2\n| * feat1\n*/\n* main1

What it means: Main and feature diverged — perfect for comparing merge vs rebase.

Example 2: Merge keeps a join commit

Type this:

cd /tmp/rb-lab && git checkout -b demo-merge main && git merge feature -m 'merge feature' && git log --oneline --graph -n 5

You will see:

*   merge feature\n|\\n| * feat1\n* | main2\n|/

What it means: Merge adds a join commit. History shows when branches met.

Example 3: Fresh clone for rebase demo

Type this:

rm -rf /tmp/rb2 && mkdir -p /tmp/rb2 && cd /tmp/rb2 && git init -b main && git config user.email lab@example.com && git config user.name Lab && printf 'v1\n' > app.txt && git add . && git commit -m 'main1' && git checkout -b feature && printf 'feat\n' >> app.txt && git commit -am 'feat1' && git checkout main && printf 'main2\n' >> app.txt && git commit -am 'main2'

You will see:

[main ...] main2

What it means: Separate lab so rebase does not fight the merge demo.

Concept 1: Merge मिलन बिंदु रखता है

Merge दो parents वाला commit।

Why: शाखाएँ कब मिलीं दिखे। What: git merge feature। How: Shared main पर policy के अनुसार।

Example 2: Merge keeps a join commit

Type this:

cd /tmp/rb-lab && git checkout -b demo-merge main && git merge feature -m 'merge feature' && git log --oneline --graph -n 5

You will see:

*   merge feature\n|\\n| * feat1\n* | main2\n|/

What it means: Merge adds a join commit. History shows when branches met.

Concept 2: Rebase काम फिर चलाता है

Rebase commits नई base पर ले जाता है।

Why: Linear history पढ़ने में आसान। What: checkout feature; rebase main। How: सिर्फ अछूती शाखाओं पर।

Example 3: Fresh clone for rebase demo

Type this:

rm -rf /tmp/rb2 && mkdir -p /tmp/rb2 && cd /tmp/rb2 && git init -b main && git config user.email lab@example.com && git config user.name Lab && printf 'v1\n' > app.txt && git add . && git commit -m 'main1' && git checkout -b feature && printf 'feat\n' >> app.txt && git commit -am 'feat1' && git checkout main && printf 'main2\n' >> app.txt && git commit -am 'main2'

You will see:

[main ...] main2

What it means: Separate lab so rebase does not fight the merge demo.

Example 4: Rebase feature onto main

Type this:

cd /tmp/rb2 && git checkout feature && git rebase main && git log --oneline --graph -n 5

You will see:

* feat1\n* main2\n* main1

What it means: Rebase replays feat1 on top of main2 — linear history, no merge commit.

Example 5: See upstream status words

Type this:

cd /tmp/rb2 && git status -sb

You will see:

## feature

What it means: After rebase your feature tip moved. If already pushed, a plain push may be rejected.

Example 6: When rebase is safe

Type this:

printf '%s\n' 'SAFE: local-only feature not pulled by others' 'UNSAFE: shared feature or main already on remote teammates'

You will see:

SAFE: local-only feature not pulled by others\nUNSAFE: shared feature or main already on remote teammates

What it means: Golden rule for Infosys interviews: never rewrite published shared history.

Concept 3: Team policy card

Local rebase + PR merge सुरक्षित जोड़ी।

Why: Shared main पर force-push दूसरों को तोड़ता है। What: Local feature rebase; main में PR merge। How: Safe vs unsafe याद करें।

Example 7: Prefer merge into main

Type this:

printf '%s\n' 'Team policy: rebase feature onto main locally, open PR, merge with merge commit or squash'

You will see:

Team policy: rebase feature onto main locally, open PR, merge with merge commit or squash

What it means: Many Accenture projects allow local rebase + PR merge — not rebase of main.

Example 8: Conflict during rebase

Type this:

printf '%s\n' 'git status' 'edit file' 'git add FILE' 'git rebase --continue' 'or: git rebase --abort'

You will see:

git status\nedit file\ngit add FILE\ngit rebase --continue\nor: git rebase --abort

What it means: Rebase conflicts pause like merge; continue or abort.

Example 9: Compare graphs side by side

Type this:

printf '%s\n' 'MERGE graph: diamond with join commit' 'REBASE graph: straight line of commits'

You will see:

MERGE graph: diamond with join commit\nREBASE graph: straight line of commits

What it means: Interviewers love this one-line difference.

Example 10: Squash note

Type this:

printf '%s\n' 'squash merge = one commit on main from whole feature' 'useful for noisy WIP commits'

You will see:

squash merge = one commit on main from whole feature\nuseful for noisy WIP commits

What it means: Squash is a merge strategy option — different from rebase.

Example 11: Do not force-push main

Type this:

printf '%s\n' 'git push --force on main: banned without written team rule'

You will see:

git push --force on main: banned without written team rule

What it means: Force-push after rebase on shared main breaks teammates.

Example 12: Decision card

Type this:

printf '%s\n' '1 update local main' '2 rebase feature onto main (local only)' '3 push feature' '4 merge via PR into main'

You will see:

1 update local main\n2 rebase feature onto main (local only)\n3 push feature\n4 merge via PR into main

What it means: Standard Netflix-style service team card.

Decision card

  1. Local main अपडेट
  2. Local feature rebase
  3. Feature push
  4. PR से main में merge

Suppose: Infosys shared feature

तीन लोग push — rebase न करें।

Suppose: Solo hotfix

सिर्फ आप — rebase करें, PR खोलें।

Ravindra Bagale's Tip

TCS and Accenture teams usually merge into main. Rebase your local feature onto latest main before the PR, then merge.

Check yourself (quiz)

Q1. Merge क्या जोड़ता है?

दो parents वाला join commit

Q2. Rebase क्या करता है?

Commits नई base पर replay

Q3. Rebase कब unsafe?

दूसरों ने pull की शाखाओं पर

Q4. सुरक्षित जोड़ी?

Local rebase, फिर PR merge

Q5. Main पर force-push?

नहीं, लिखित नियम बिना

Q6. Graph फ़र्क?

Merge diamond; rebase सीधी रेखा

Reading tracking

Mark this guide when you finish. Your browser remembers it on this device.

Merge = मिलन। Rebase = rewrite। Shared main rewrite न करें। Did you understand? Let us try it once more.

Frequently asked questions

Merge vs rebase in one line?

Merge adds a join commit; rebase replays commits onto a new base.

When is rebase safe?

On a local feature branch nobody else pulled.

Should I rebase main?

No — update main with merge or pull per team rules.

What if rebase conflicts?

Edit, git add, git rebase --continue (or --abort).

Force-push after rebase?

Only your solo branch, preferably --force-with-lease; never shared main.

What do interviewers want?

The safe card: local rebase, PR merge into main.