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:
![](http://codebymade.com/wp-content/uploads/2023/07/basic.png)
We create our branch (green). While we work on it somebody can add new changes (commits) to the master branch:
![](http://codebymade.com/wp-content/uploads/2023/07/base-pic-new-commits-1024x310.png)
Our changes look fine and now we want to add it to the master.
1. Using merge.
![](http://codebymade.com/wp-content/uploads/2023/07/base-pic-merge-1024x336.png)
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.
![](http://codebymade.com/wp-content/uploads/2023/07/base-pic-rebase-1-1024x153.png)
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