Merge vs Rebase

Every software developer needs to synchronize two branches, for example local branch with remote branch. Let’s say we want to add our changes to remote master branch. We have two options: merge and rebase.

Best way is to show that in example.

Basic structure of master branch:

We create our branch (green). While we work on it somebody can add new changes (commits) to the master branch:

Our changes look fine and now we want to add it to the master.

1. Using merge.

Merge creates a new commit and the structure is not linear. We can see something like „merge commit” and it gives us information that the new branch was added to the master.

2. Using rebase.

Rebase adds changes in linear structure. We don’t know if a new branch was merged, we just see that commits were added.

What should I use?

Generally adding changes through merge is considered to be safer, because we have more information about added changes. Merge preserves history and rebase rewrites history. Merge is used when multiple developers work on the master branch.

Git commands

Eample with “rebase”, you can replace it with “merge”.

1. We check if we have conflicts with master.

git checkout my_branch

git rebase master

If there are any conflicts we will see it now and resolve.

2. Apply changes to master

git checkout master

git rebase my_branch

Leave a Reply

Your email address will not be published. Required fields are marked *