Debug School

rakesh kumar
rakesh kumar

Posted on

Difference between git rebase and git merge and git pull with examples

Git Merge:

Git merge is a command used to integrate changes from one branch into another branch. It creates a new commit that combines the changes from the source branch into the target branch. This results in a merge commit, which has two or more parents, representing the branches' histories coming together.

Example scenario:

Let's say you have a branch named feature_branch and you want to merge its changes into the main branch.

# Switch to the target branch (main)
git checkout main

# Perform the merge
git merge feature_branch
Enter fullscreen mode Exit fullscreen mode

After executing the merge, a new commit will be created on the main branch, incorporating the changes from feature_branch. The commit history will look something like this:

main:   A -- B -- C -- M
                \    /
feature:         D -- E
Enter fullscreen mode Exit fullscreen mode

M is the merge commit, and it contains the combined changes from feature_branch and main.

Git Rebase:

Git rebase is a command used to reapply commits on top of a different base commit. It moves the entire branch's history to a new base commit, effectively rewriting the branch's commit history. This creates new commits that apply the changes to the latest state of the target branch.

Example scenario:

Let's assume the same starting point as before:

main:   A -- B -- C
                \
feature:         D -- E
Enter fullscreen mode Exit fullscreen mode

To rebase feature_branch on top of main, you would use the following commands:

# Switch to the feature branch
git checkout feature_branch
Enter fullscreen mode Exit fullscreen mode
# Perform the rebase
git rebase main
Enter fullscreen mode Exit fullscreen mode

After the rebase, the commit history will look like this:

main:   A -- B -- C
                     \
feature:              D' -- E'
Enter fullscreen mode Exit fullscreen mode

D' and E' are the new commits, which contain the same changes as D and E, but they are now based on the tip of main.

Git Pull:

git pull is a combination of two Git commands: git fetch and either git merge or git rebase. The default behavior is to perform a git merge, but you can configure it to use rebase through the pull.rebase configuration.

Example scenario:

Assume the following situation:

main:   A -- B -- C
                \
feature:         D -- E
Enter fullscreen mode Exit fullscreen mode

If you execute git pull with the default settings, it will perform a merge:

# While on the feature branch, pull changes from main
git pull origin main
Enter fullscreen mode Exit fullscreen mode

The result will be a merge commit:

main:   A -- B -- C
                \    \
feature:         D -- E -- M
Enter fullscreen mode Exit fullscreen mode

If you set git pull to use rebase by default:

# Set git pull to use rebase
git config --global pull.rebase true
Enter fullscreen mode Exit fullscreen mode

Then, executing git pull will perform a rebase instead of a merge:

While on the feature branch, pull changes from main using rebase

git pull origin main
Resulting in a new commit history:

main:   A -- B -- C
                     \
feature:              D' -- E'
Enter fullscreen mode Exit fullscreen mode

D' and E' are new commits, and the feature_branch has been rebased on top of the latest commit in main.

In summary, git merge integrates changes with a new merge commit, git rebase moves the branch's history to a new base commit, and git pull is a convenience command for fetching and merging or rebasing changes from a remote branch into the current branch. The choice between merge and rebase depends on the desired commit history and the project's workflow.

best practice
It is best practice to always rebase your local commits when you pull before pushing them. As nobody knows your commits yet, nobody will be confused when they are rebased but the additional commit of a merge would be unnecessarily confusing. Published commits are, however, usually merged, for example when branches are merged.

To avoid typing --rebase whenever you pull you can config git to use it as default:

git config --global pull.rebase true
Enter fullscreen mode Exit fullscreen mode

Git pull --rebase vs. --merge

Top comments (0)