📜 ⬆️ ⬇️

Introducing Git Merge and Git Rebase: Why and When to Use Them

Often, developers have a choice between Merge (merge) and Rebase (move). In Google, you will see a different opinion, many advise not to use Rebase, as this can cause serious problems. In the article I will explain what merging and moving is, why you should (or should not) use them and how to do it.

image

Git Merge and Git Rebase have the same goal. They are designed to integrate changes from one branch to another. Although the end goal is the same, the principles of work are different.
')
Some people think that you should always use Rebase, others prefer Merge. This has its pros and cons.

Git merge


Merging is common practice for developers using version control systems. Regardless of whether branches are created for testing, bug fixes, or for other reasons, a merge captures changes elsewhere. The merge takes the contents of the source branch and merges them with the target branch. In this process, only the target branch changes. The history of the original branches remains unchanged.
image
Pros:


Minuses:


How to do it

Merge the master branch into the feature branch using the checkout and merge commands.

$ git checkout feature $ git merge master (or) $ git merge master feature 

This will create a new “Merge commit” in the feature branch, which contains the history of both branches.

Git rebase


Rebase is another way to transfer changes from one branch to another. Rebase compresses all changes into one “patch”. It then integrates the patch into the target branch.

Unlike a merge, moving overwrites the history because it transfers the completed work from one branch to another. The process eliminates unwanted history.

image

Pros:


Minuses:


If you make a wrong move, the story will change, and this can lead to serious problems, so make sure you do it!

How to do it

Move the feature branch to the main branch using the following commands.

 $ git checkout feature $ git rebase master 

This moves the entire branch of the function to the main branch. The project history changes, new commits are created for each commit in the main branch.

Interactive move


This allows you to change commits as they move to a new branch. This is better than automatic relocation because it provides complete control over the commit history. As a rule, it is used to clear history before merging the feature branch into master.

 $ git checkout feature $ git rebase -i master 

This will open the editor, listing all the commits that will be moved.

 pick 22d6d7c Commit message#1 pick 44e8a9b Commit message#2 pick 79f1d2h Commit message#3 

This defines exactly what the branch will look like after the move is completed. By organizing objects, you can make the story as you like. You can use the fixup , squash , edit , and so on commands.

image

Which one to use?


So what's better? What do experts recommend?

It is difficult to make the only right decision about what is best to use, since all the teams are different. It all depends on the needs and traditions within the team.

Make decisions based on team competency in Git. Is simplicity or rewriting a story important to you, or maybe something else?

What do I recommend?

As the team grows, it becomes difficult to manage or track changes in development by applying a merge. To have a clean and clear commit history, it is wise to use Rebase.

Advantages of Rebase:


Now you know even a little, but the difference between Merge and Rebase. I am sure you will make the right decision and will use what is right for you.

Do not forget:

 code = coffee + developer 

Source: https://habr.com/ru/post/432420/


All Articles