There is a lot of debate about which is better, Git Merge and Rebase. Today’s Git Rebase and Merge blog answers all your questions about Git Rebase and Git Merge. Both techniques are used for the same purpose and are a bit difficult to understand because they are similar. At the end of the blog, learn when to use Git Rebase and Merge.
The GitMerge and GitRebase commands are used to combine the work of multiple developers into a single code. The ultimate goal of these two commands is the same, but their usage is different. Today in this blog, I’ll try to understand Git merging and Git rebasing.
Scenario:
Are you working on new features in your project? You may have created something else a branch for this purpose. But what if you want to add these changes to your master branch? Next, let’s take a look at these commands, as MERGE or REBASE can be useful.
git-merge – Join two or more development histories together.
For example:
The four master branches commit m1, m2, m3, m4, and the functional branch commits m1, m2, f1, f2, and f3. Now I would like to combine both changes in one place. In this scenario, Merge or rebase commands. Let’s see the difference between these two commands and their use.
Git Merge scenario:
In this scenario
1. First, there is a master branch and a master branch, and there are two commits (m1, m2).
2. We got a new piece and created it for this new branch called Feature. This feature branch Commit with data up to m1 and m2.
$git fetch
$git checkout feature
3. Now you have implemented the code in the feature branch and made two commits in the feature branch. They are called f1 and f2. The functional branch has four commit logs (m1, m2, f1, f2).
4. Next, merge the feature branch into the master branch. Use the merge command for this scenario.
Note: While someone else is working on the master branch and committing the code in 2 Commits named as m3, m4.
$git checkout master
$git log
This master branch is logged
5. Here we are using the –squash auto-merge option to avoid merge conflicts.
$ git merge --squash feature
After reviewing the project structure, get all the files from the feature branch. Let’s take a look at the master branch log.
Note: You have successfully merged the feature branch into the master. Here we miss the story Feature branch commit message. When committing the history of a feature branch Messages need to use rebase. |
The structure after merging should look like the below diagram. * mc is a merge commit
Note: This is a new scenario based on the merge method. In this scenario, we’ll go back to what the previous project would look like.
$ git reset --hard HEAD~1 ---> i am deleting latest commits (last 3 commits i deleted)
Next, check the log of the master branch. Now you can only see the committed m1 and m2 messages.
6. Switch to the feature branch.
$git checkout feature
$git log
7. m4 commits while some other people are pushing to the master code with m3.
see the logs
Git Rebase scenario:
If you don’t switch between feature branches, think of yourself as in the feature branch.
$git checkout feature
$git log
Then run git rebase on the feature branch.
$ git rebase master
let’s see the logs:
Then check out to the master branch.
$git checkout master
Next, check the log of the master branch. There are only m1, m2, m3, and m4 commits. Then run the rebase command on the master branch.
$git rebase feature
Then check the logs. This time, the commits are displayed in the logs m1, m2, m3, m4, f1, and f2…
$git log
Now you can also see the branch commits for the history feature …