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

 3r3303. 3r3-31. Often, developers have a choice between Merge (merge) and Rebase (move). In Google, you will see different opinions, 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.
 3r3303.
 3r3303. Introducing Git Merge and Git Rebase: Why and When to Use Them
 3r3303.
 3r3303. 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.
 3r3303.
 3r3303. Some people think that you should always use Rebase, others prefer Merge. This has its pros and cons.
 3r3303.
 3r3303.

Git Merge


 3r3303. 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.
 3r3303.
 3r3303. Pros:
 3r3303.
 3r3303.
 3r3303.
simplicity;
 3r3303.
keeps complete history and chronological order;
 3r3303.
supports the context of the thread.
 3r3303.
 3r3303. Cons:
 3r3303.
 3r3303.
 3r3303.
commit history can be filled (polluted) with multiple commits;
 3r3303.
debugging with git bisect can be tricky.
 3r3303.
 3r3303. How to do it
 3r3303.
 3r3303. Merge the master branch into the feature branch using the commands checkout and merge .
 3r3303.
 3r3303.
$ git checkout feature
$ git merge master
(or) 3r3303. $ git merge master feature

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

Git Rebase


 3r3303. 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.
 3r3303.
 3r3303. Unlike a merge, moving overwrites the history because it transfers the completed work from one branch to another. The process eliminates unwanted history.
 3r3303.
 3r3303. 3r3108.
 3r3303.
 3r3303. Pros:
 3r3303.
 3r3303.
 3r3303.
Simplifies a potentially complex story
 3r3303.
Simplify single-commit manipulation 3-3-33273.  3r3303.
Avoiding merging commits in busy repositories and branches 3-333273.  3r3303.
Cleans intermediate commits, making them one commit, which is useful for the DevOps commands 3r-3273.  3r3303.
 3r3303. Cons:
 3r3303.
 3r3303.
 3r3303.
Compressing features to multiple commits can hide the context of
 3r3303.
Moving public repositories can be dangerous when working in a 3r-3273 team.  3r3303.
more work appears.  3r3303.
For recovery with deleted branches, a forced push is required. This leads to the update of all branches that have the same name, both locally and remotely, and this is terrible.
 3r3303.
 3r3303. If you make a wrong move, the story will change, and this can lead to serious problems, so make sure you do it!
 3r3303.
 3r3303. How to do it
 3r3303.
 3r3303. Move the feature branch to the main branch using the following commands.
 3r3303.
 3r3303.
$ git checkout feature
$ git rebase master

 3r3303. 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.
 3r3303.
 3r3303.

Interactive move


 3r3303. This allows you to change commits as they move to a new branch. This is better than automatic relocation because it provides full control over the commit history. As a rule, it is used to clear the history before merging the feature branch into master.
 3r3303.
 3r3303.
$ git checkout feature
$ git rebase -i master

 3r3303. This will open the editor, listing all the commits that will be moved.
 3r3303.
 3r3303.
pick 22d6d7c Commit message # 1
pick 44e8a9b Commit message # 2
pick 79f1d2h Commit message # 3

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

Which one to use?


 3r3303. So what's better? What do experts recommend?
 3r3303.
 3r3303. 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.
 3r3303.
 3r3303. Make decisions based on team competency in Git. Is simplicity or rewriting a story important to you, or maybe something else?
 3r3303.
 3r3303. What do I recommend?
 3r3303.
 3r3303. 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.
 3r3303.
 3r3303. Advantages of Rebase:
 3r3303.
 3r3303.
 3r3303.
You develop locally: if you have not shared your work with anyone else. At this point, you should prefer moving over to merge to keep your history in order. If you have a personal repository plug that is not shared with other developers, you can rebase even after you've moved to your branch.
 3r3303.
Your code is ready for review: you have created a pull request. Others analyze your work and potentially pin it to their fork for a local review. At this point you should not move your work. You must create a remake commit and update the branch. It helps to track requests for pull requests and prevents accidental breaking of history.
 3r3303.
The review is done and ready for integration into the target branch. Congratulations! You are about to delete your feature branch. Considering that from this point on, other developers will not fetch-merging these changes, this is your chance to change your story. At this stage, you can rewrite history and reset the original commits, and these annoying "rework" and "merge" merge into a small set of targeted commits. Creating an explicit merge for these commits is optional, but important. It records when the function has reached master.
 3r3303.
 3r3303. 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.
 3r3303.
 3r3303. Don't forget:
 3r3303.
 3r3303.
code = coffee + developer
3r33232. 3r3303. 3r3303. 3r3303.
3r3303. 3r33232. 3r3303. 3r3303. 3r3303. 3r3303.
+ 0 -

Add comment