Skip to Content
DevopsGitGit WorkflowMerge & Rebase

Merge and Rebase

When working with branches in Git, you often need to integrate changes from one branch into another. The two most common methods are merge and rebase.

Both combine the work of different branches but they do it differently.

Git Merge

git merge combines the history of two branches by creating a merge commit.

Basic command

Terminal
git merge branch-name

Example:

Terminal
git checkout main git merge feature-login

This merges the changes from feature-login into main.

You merge into the branch you are currently on.

Git Merge Branch

Fast-Forward Merge

A Fast-Forward merge happens when the target branch has no new commits since the feature branch was created. In this case, Git does not create a merge commit. Instead, it simply moves the branch pointer forward to the latest commit.

This keeps the project history linear and clean.

Terminal
git merge --ff-only feature-login
Git Merge Branch

To force the Fast Forward merge u can use git merge --ff-only feature-login but if there’s a changes in the main (New commits), git will throw an error

Squash Merge

A squash merge combines all commits from a branch into one single commit before merging. And the commits will not appear in the main branch

Terminal
git merge --squash feature-login
Git Merge Branch

Git Rebase

git rebase is used to integrate changes from one branch into another by reapplying commits on top of a new base commit. Unlike git merge, which creates a merge commit, rebase rewrites the commit history to keep it linear.

The most common cases are simple rebase, rebase with conflicts, and interactive rebase.

Basic command

Terminal
git rebase branch-name

Example:

Terminal
# you shloud be in the branch feature-login git checkout feature-login git rebase feature-login

When you rebase a branch, Git takes the commits from your current branch and put them on top of the main.

Simple Rebase

This is the most common case. The commits of the current branch are reapplied on top of another branch.

Git rebase branch

Or you can rebase from main:

Git rebase branch

Rebase With Conflicts

If the same lines of code are changed in both branches, a conflict happens during the rebase.

Git rebase branch
- function ( a ,b ) { - const a; - const b; - const result = a + b; - return result - } + function( a, b) { + return a + b + }

Git will show something like:

CONFLICT (content): Merge conflict in function.js

To resolve it:

# Fix the conflict in the files <<<<<<< HEAD (current Change) const a; const b; const result = a + b; return result ======= return a + b >>>>>>> Commit id: 5200b20 (Incoming changes)
Terminal
# Add the resolved files git add file.txt # Continue the rebase git rebase --continue

Other useful commands:

Terminal
# if you want to cancel the rebase use `--abort` git rebase --abort # if you want to skip the conflict commit and delete it, and continue the rebase use `--skip` git rebase --skip

Interactive Rebase

Interactive rebase lets you edit the commits while rebasing.

Terminal
git checkout feature git rebase -i main
$ git rebase -i main pick f051d68 add feature commit # # Rebase ba79301..f051d68 onto ba79301 (1 command) # # Commands: # p, pick <commit> = use commit # r, reword <commit> = use commit, but edit the commit message # e, edit <commit> = use commit, but stop for amending # s, squash <commit> = use commit, but meld into previous commit # f, fixup [-C | -c] <commit> = like "squash" but keep only the previous ... --INSERT--

This does the same rebase, but Git opens an editor so you can:

  • reorder commits
  • squash commits
  • edit commits
  • remove commits
  • change commit messages

Rebase -i commands

In practice, developers mostly use these:


CommandShortDescription
pickpKeep the commit as it is
rewordrKeep the commit but edit the commit message
editePause the rebase to modify the commit
squashsCombine the commit with the previous commit and edit the message
fixupfCombine the commit with the previous commit and discard its message
execxRun a shell command during the rebase
dropdRemove the commit completely
breakbStop the rebase temporarily
labellLabel the current commit (used for advanced rebases)
resettReset HEAD to a label
mergemCreate a merge commit during rebase
Last updated on