Git Merge vs Rebase: Which One Should You Use?
Git merge vs rebase for beginners, on a real repository: what each one does to your commits, why a rebase gives them new IDs, what happens when you rebase a branch you already pushed (a rejected push, then every commit twice), how conflicts differ, and a simple rule for picking between them.

Your branch is behind main
Your branch is missing new work from main. Git merge, or rebase? Those are your two ways to catch up.
Pick rebase at the wrong moment, and your next push is rejected. By the end, you'll know what each one does to your commits, and one rule for picking.
git rebase mainSuccessfully rebased and updated refs/heads/login.
git pushTo /srv/git/app.git ! [rejected] login -> login (non-fast-forward) error: failed to push some refs to '/srv/git/app.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. If you want to integrate the remote changes, hint: use 'git pull' before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Here's a real repository. Git log draws its history with asterisks and slashes. It's doing its best. Main is the team's branch. Login is yours, with two commits.
git log --oneline --graph --all* dd09acf (origin/main, main) Change the home page title * 23c0935 Add the prices page | * bc08fae (HEAD -> login, origin/login) Check the password | * 0d5e2df Add the login form |/ * 98573fd Add the home page * 33179f2 Start the app
As a picture: each box is a commit, with its ID. Yours add a login form and a password check, and you've already pushed both to the team's server.
Since you branched off, a teammate has added two commits to main: a prices page, and a new title. Your branch doesn't have them yet.
Merge: one new commit
Option one: merge. You're on login, and you type git merge main, which means: bring main's commits into my branch. Git lists the files that came in. Now the log again. Your two commits are still here, with the same IDs. The teammate's two are here. And on top, there's one new commit that joins the two lines: a merge commit.
git merge mainMerge made by the 'ort' strategy. index.html | 2 +- prices.html | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 prices.html
git log --oneline --graph* 6c81648 (HEAD -> login) Merge branch 'main' into login |\ | * dd09acf (origin/main, main) Change the home page title | * 23c0935 Add the prices page * | bc08fae (origin/login) Check the password * | 0d5e2df Add the login form |/ * 98573fd Add the home page * 33179f2 Start the app
Here it is as a picture. Your line of commits, main's line, and the merge commit tying them together. An ordinary commit has one parent: the commit it was built on. A merge commit has two. Now notice what merge did not do. It didn't change any commit that already existed. It only added one. That's why merge is safe on any branch. The price: every time you catch up this way, you get one more merge commit, and over months the history fills with joins that are hard to read. One merge in the Linux kernel ties together sixty-six branches at once.
To compare fairly, undo that merge: git reset, dash dash hard, ORIG_HEAD. Reset moves your branch back, and ORIG_HEAD is Git's bookmark for where it was just before the merge. One warning: reset hard also throws away any edits you haven't committed.
git reset --hard ORIG_HEADHEAD is now at bc08fae Check the password
git log --oneline --graph --all* dd09acf (origin/main, main) Change the home page title * 23c0935 Add the prices page | * bc08fae (HEAD -> login, origin/login) Check the password | * 0d5e2df Add the login form |/ * 98573fd Add the home page * 33179f2 Start the app
Rebase: your commits, replayed
Option two: rebase. The name says it: give my commits a new base. Same branch, same shape of command: git rebase main. Git lifts your two commits off, goes to the newest commit on main, and replays them there, one at a time. Successfully rebased. Before we look at the log, guess first: do your two commits still have the same IDs? No. The same messages and the same changes, but new IDs. And the history is one straight line, with no merge commit.
git log --oneline --graph* 49a5edf (HEAD -> login) Check the password * f82c84f Add the login form * dd09acf (origin/main, main) Change the home page title * 23c0935 Add the prices page * 98573fd Add the home page * 33179f2 Start the app
As a picture. Before, your two commits were built on the commit where you branched off. Rebase made a copy of each one on top of main's newest commit, and then moved your branch, login, onto the copies. So login now holds main's two new commits, with your two on top. The originals aren't gone yet. Your branch no longer points to them. But ORIG_HEAD still does. And so does the copy of login you pushed to the server. Keep that server copy in mind.
What's inside a commit
So why new IDs? Here's your login form commit, the way Git stores it. The command is git cat-file. First the original, then the copy. Same author. Same message. But look at the parent line. The original's parent is the commit you branched off from. The copy's parent is main's newest commit. A commit's ID is a fingerprint of everything inside it. And one of the things inside it is its parent's ID. A new parent means a new fingerprint. So rebase can't move a commit. It can only make a new one.
git cat-file -p ORIG_HEAD~1tree 7bc1509a05e8e1468c1bd658813d8d2fe8028b22 parent 98573fdc23595f494c0427eb2e6d79b14643d55c author Dev <dev@example.com> 1796396986 +0000 committer Dev <dev@example.com> 1796396986 +0000 Add the login form
git cat-file -p HEAD~1tree 010f60bd4fcae301c53708a1ce42f47d0f203fa8 parent dd09acf164f8a9c6f8e5606042a3ecd1db0e934b author Dev <dev@example.com> 1796396986 +0000 committer Dev <dev@example.com> 1796396991 +0000 Add the login form
Conflicts
Replaying commits one at a time has a cost. You feel it when you and a teammate edit the same line. Git can't pick between the two versions, so it stops and asks you. That's a conflict. Login has no conflicts, so here's a second branch of yours, banner, set up to show one. Both of its commits change the page title. Your teammate changed that same line on main. Merge main in, and Git stops with one conflict. It puts both versions of the line into the file, with markers around them. You keep the version you want and delete the markers. With merge, you do that just once. We'll back out with git merge, dash dash abort, and try rebase instead. Git replays your first commit, and stops on a conflict. You fix the line. Then you run git add, to tell Git it's fixed. Then git rebase, dash dash continue. Then it replays your second commit, and stops again. Merge asks you once. Rebase can ask once for every commit it replays.
git switch bannerSwitched to branch 'banner'
git merge mainAuto-merging index.html CONFLICT (content): Merge conflict in index.html Automatic merge failed; fix conflicts and then commit the result.
cat index.html<<<<<<< HEAD <h1 class="big">Welcome!</h1> ======= <h1>Welcome to the shop</h1> >>>>>>> main <p>Home page</p>
git merge --abort
git rebase mainAuto-merging index.html CONFLICT (content): Merge conflict in index.html error: could not apply 7941983... Make the title bigger hint: Resolve all conflicts manually, mark them as resolved with hint: "git add/rm <conflicted_files>", then run "git rebase --continue". hint: You can instead skip this commit: run "git rebase --skip". hint: To abort and get back to the state before "git rebase", run "git rebase --abort". Could not apply 7941983... Make the title bigger
git add index.html
git rebase --continue[detached HEAD 69dc75b] Make the title bigger 1 file changed, 1 insertion(+), 1 deletion(-) Auto-merging index.html CONFLICT (content): Merge conflict in index.html error: could not apply 825f4f7... Add an exclamation mark hint: Resolve all conflicts manually, mark them as resolved with hint: "git add/rm <conflicted_files>", then run "git rebase --continue". hint: You can instead skip this commit: run "git rebase --skip". hint: To abort and get back to the state before "git rebase", run "git rebase --abort". Could not apply 825f4f7... Add an exclamation mark
Rebasing a pushed branch
Now, remember that push. Merge is a bridge between two roads. Both roads stay exactly where they were. Rebase is a moving truck. It picks up your house and sets it down at the end of the main road: the same house, at a new address. And anyone who wrote down the old address? Their mail goes to an empty lot.
That old address is the catch. Remember, you had already pushed login, so the team's server still has your two original commits. Run git status now. Your branch and the server's copy have diverged. Your laptop has four commits the server doesn't: main's two, plus your two copies. And the server has two that your branch no longer has: your original commits. So you push, and the server says no. The push is rejected. A normal push only adds commits. It never replaces them.
git log --oneline -2 ORIG_HEADbc08fae (origin/login) Check the password 0d5e2df Add the login form
git statusOn branch login Your branch and 'origin/login' have diverged, and have 4 and 2 different commits each, respectively. (use "git pull" if you want to integrate the remote branch with yours) nothing to commit, working tree clean
Under the rejection, Git adds a hint: run git pull first. Pull downloads the server's copy of your branch and combines it with yours. But a plain git pull stops too. The two sides have gone different ways, and Git wants to know how to combine them. So pick the classic way: git pull, dash dash no rebase. That tells it to merge. No errors. It looks fixed. Now check the log. Guess first: how many times does 'Add the login form' appear? Twice. The pull merged your originals back in, right beside their copies.
git pullhint: You have divergent branches and need to specify how to reconcile them. hint: You can do so by running one of the following commands sometime before hint: your next pull: hint: hint: git config pull.rebase false # merge hint: git config pull.rebase true # rebase hint: git config pull.ff only # fast-forward only hint: hint: You can replace "git config" with "git config --global" to set a default hint: preference for all repositories. You can also pass --rebase, --no-rebase, hint: or --ff-only on the command line to override the configured default per hint: invocation. fatal: Need to specify how to reconcile divergent branches.
git pull --no-rebaseMerge made by the 'ort' strategy.
git log --oneline074b76b (HEAD -> login) Merge branch 'login' of /srv/git/app into login 49a5edf Check the password f82c84f Add the login form dd09acf (origin/main, main) Change the home page title 23c0935 Add the prices page bc08fae (origin/login) Check the password 0d5e2df Add the login form 98573fd Add the home page 33179f2 Start the app
Every commit of yours is now in the history two times, with a merge commit on top.
Here's the way out. First, undo that pull with the same reset: back to ORIG_HEAD, the bookmark from just before it. Then replace the server's copy on purpose: git push, dash dash force with lease. Forced update. The server now has your rebased commits. Force with lease is the careful kind of force: it refuses if someone else has pushed to that branch in the meantime. But it only protects the server. It can't fix a teammate's laptop.
git reset --hard ORIG_HEADHEAD is now at 49a5edf Check the password
git push --force-with-leaseEnumerating objects: 7, done. Counting objects: 100% (7/7), done. Delta compression using up to 10 threads Compressing objects: 100% (6/6), done. Writing objects: 100% (6/6), 644 bytes | 644.00 KiB/s, done. Total 6 (delta 1), reused 0 (delta 0), pack-reused 0 To /srv/git/app.git + bc08fae...49a5edf login -> login (forced update)
git statusOn branch login Your branch is up to date with 'origin/login'. nothing to commit, working tree clean
Here is that teammate's laptop. Before you rebased, they got your login branch, and added one commit of their own on top of your originals. After your forced push, they pull, the same merging way. Look at their log. Your originals, your copies, their commit, and a merge. The duplicates are back, on a machine your force push can't reach.
git pull --no-rebaseremote: Enumerating objects: 7, done. remote: Counting objects: 100% (7/7), done. remote: Compressing objects: 100% (6/6), done. remote: Total 6 (delta 1), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (6/6), 624 bytes | 624.00 KiB/s, done. From /srv/git/app + bc08fae...49a5edf login -> origin/login (forced update) Merge made by the 'ort' strategy. index.html | 2 +- prices.html | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 prices.html
git log --oneline48ffeeb (HEAD -> login) Merge branch 'login' of /srv/git/app into login b77752b Add a forgot-password link 49a5edf (origin/login) Check the password f82c84f Add the login form dd09acf (origin/main, origin/HEAD, main) Change the home page title 23c0935 Add the prices page bc08fae Check the password 0d5e2df Add the login form 98573fd Add the home page 33179f2 Start the app
Git's own manual is blunt about it: rebasing a branch that others have based work on is a bad idea. Having pushed isn't the problem. Someone building on your commits is. So only rebase a branch that nobody else has built on.
Which one to use
So how do the two fit together? Most teams use both. Rebase your own branch onto main to catch up, the way we did with login. Then switch to main, and merge login in. Your commits already sit on top of main, so there's nothing to join, and Git doesn't make a merge commit.
git switch mainSwitched to branch 'main' Your branch is up to date with 'origin/main'.
git merge loginUpdating dd09acf..49a5edf Fast-forward login.html | 4 ++++ login.py | 2 ++ 2 files changed, 6 insertions(+) create mode 100644 login.html create mode 100644 login.py
Git just slides the name main forward along your commits. That's called a fast-forward. Six commits, one straight line, zero merge commits.

So, which one do you pick? Merge leaves your commits as they are, and adds one extra commit. It's safe everywhere. Rebase gives you one straight line, but new commit IDs, and maybe a conflict per commit. On a branch only you use, rebase to catch up. If you'd already pushed it, push again with force with lease. Once someone else has built on it, merge.

If merge and rebase now look like two different tools, subscribe for more Git, tested the same way.
The lab and every command are linked in the description, so you can tangle up a practice repository before a real one. Thanks for watching Cloud Explainers.
Sources
Demos: one container on our own Mac standing in for your laptop, a second user as the teammate, and a local shared repository as the server. The app and its commits are made up; every command, output and commit ID was real. Watch the full video on YouTube.