KKAIROS
Practical guideIllustrative4 min read

Git Merge vs Rebase: How Each Integrates Changes and Shapes History

Merge joins histories and may fast-forward without a merge commit; rebase replays commits onto a new base and rewrites their ids.

Published:

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

TE-10-A: a fast-forward merge creates no merge commit

Setup for this run, inside a throwaway directory (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'one\n' > f.txt; git add f.txt; git commit -qm one
git checkout -q -b topic; printf 'two\n' >> f.txt; git add f.txt; git commit -qm two
git checkout -q -

Command:

git merge --ff-only topic >/dev/null; echo parents=$(git log -1 --format=%p | wc -w); echo commits=$(git log --oneline | wc -l)

Recorded output:

parents=1
commits=2

TE-10-B: a rebase rewrites commit ids

Setup for this run, inside a throwaway directory (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'one\n' > f.txt; git add f.txt; git commit -qm one
git checkout -q -b topic; printf 'two\n' >> f.txt; git add f.txt; git commit -qm two
git checkout -q main; printf 'main\n' > m.txt; git add m.txt; git commit -qm main

Command:

git checkout -q topic; old=$(git rev-parse topic); out=$(git rebase main 2>&1); rc=$?; printf '%s\n' "$out" | sed 's/\x1b\[[0-9;]*[A-Za-z]//g'; echo rebase=$rc; echo changed=$([ "$old" != "$(git rev-parse HEAD)" ] && echo yes || echo no); echo commits=$(git log --oneline | wc -l)

Recorded output:

Rebasing (1/1)

Successfully rebased and updated refs/heads/topic.
rebase=0
changed=yes
commits=3

TE-10-C: expected conflict, non-zero exit: rebasing a branch onto a main that changed the same line stops with exit status 1

Setup for this run, inside a throwaway directory (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm base
git checkout -q -b side; printf 'side\n' > f.txt; git add f.txt; git commit -qm side
git checkout -q main; printf 'main\n' > f.txt; git add f.txt; git commit -qm main
git checkout -q side

The file ../out.txt sits one level above the working folder, inside the same throwaway tree. It holds the output of the git command; the output block below shows only the count and the status line printed by the command.

Command:

git rebase main >../out.txt 2>&1; rc=$?; echo conflict_lines=$(grep -c CONFLICT ../out.txt); git status --short; exit $rc

Recorded output:

conflict_lines=1
UU f.txt

Exit status: 1. This status and the output match what was written before the run.

Reading the recorded output

In TE-10-A, parents=1 is the number of words that git log -1 --format=%p printed for the newest commit, so that commit has one parent and is not a merge commit. The value commits=2 is the number of commits listed. In TE-10-B, the rebase printed Rebasing (1/1) and Successfully rebased and updated refs/heads/topic, with exit status 0. The line changed=yes says the id at HEAD differs from the old id of topic. The value commits=3 is the count of listed commits; by the setup, those are one, main and the replayed two.

TE-10-C: by the setup, main and side each changed f.txt from the same starting line. The rebase exited with status 1. Its output holds one line with the word CONFLICT, and git status --short lists UU f.txt.

Ad

Limits

Commit ids after a rebase differ on every run, because the committer date enters the hash. Compare old and new in the same run only.

Source

Evidence

Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-10-A | a fast-forward merge creates no merge commit | 0 | PASS | b4ac3ace9fcc | | TE-10-B | a rebase rewrites commit ids | 0 | PASS | ef17cc81650c | | TE-10-C | expected conflict, non-zero exit: rebasing a branch onto a main that changed the same line stops with exit status 1 | 1 | PASS | e6d575265516 |

Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.

All full guides: All guides