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

Guides

Git Rebase vs Merge: When to Use Which (With Examples)

Merge keeps a join commit; rebase replays your commits onto a new base. Rebase only local branches; merge into shared main via team PR.

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. See diverged main and feature.
  2. Try merge — note the join commit.
  3. Try rebase — note the linear line.
  4. Never rebase shared branches.
  5. Follow local-rebase then PR-merge 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

Why this choice matters

At TCS and Accenture, messy history slows reviews. Merge and rebase both combine work — they tell different stories.

Why: Reviews and rollbacks depend on history shape. What: merge = join commit; rebase = replay commits. How: Practice both on a lab, then follow 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 keeps the meeting point

Merge creates a commit with two parents.

Why: You need to see when branches joined. What: git merge feature with a message. How: Use on shared main when policy says so.

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 replays your work

Rebase moves your commits onto a new base.

Why: Linear history is easier to read. What: git checkout feature; git rebase main. How: Only on branches nobody else uses yet.

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 is the usual safe combo.

Why: Force-pushing shared main breaks others. What: Rebase local feature; merge into main via PR. How: Memorize safe vs unsafe and the decision card.

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 (memorize)

  1. Update local main
  2. Rebase your local feature onto main
  3. Push feature (force-with-lease only if you alone use it)
  4. Merge into main through the team PR flow

Suppose: Infosys shared feature branch

Three people push to feature/x. Do not rebase; merge main into feature or merge feature into main.

Suppose: Solo hotfix branch

Only you use it. Rebase onto latest main, then open 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. What does merge add?

A join commit with two parents

Q2. What does rebase do?

Replays your commits onto a new base

Q3. When is rebase unsafe?

On branches others already pulled

Q4. Usual safe combo?

Local rebase of feature, then merge into main via PR

Q5. Force-push main?

No, unless a written team rule allows it

Q6. Merge vs rebase graph?

Merge shows a diamond; rebase is a straight line

Reading tracking

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

Merge = meeting point. Rebase = rewrite. Never rewrite shared main. 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.