Understanding Git Using Bank Theory — Git Merge Vs Git Rebase
(In continuation with the previous blog, Understanding GIT using Bank Theory, Today we will take a look at the two very important concepts of GIT i.e. Git Merge & Git Rebase.
GIT Merging
Merging in git is like keeping all the independent lines created by git branches into a single branch. It creates a new commit after the merging is done. Merging is done usually in the main branch.
syntax-
$ git merge new
GIT Rebase
whereas git rebase allows developers to integrate changes from one branch to another. It rewrites the commit history and displays the last commit done on the branch. Usually, the feature branch is rebased.
syntax-
$ git rebase main
As we have a better understanding of both terms, let’s connect it with our Bank Theory.
Battle Round: Git Merge vs Git Rebase
In continuation to our discussion, as branch Delhi is a child branch of Head-office we have to ensure that the head-office branch database gets updated with the new accounts records.
Here comes the role of git merge. We will merge our data of the Delhi branch onto the Head-office branch. Ensure you are at the Head-office branch before you actually merge from the Delhi branch.
$ git checkout head-office
$ git merge delhi
/home/shivani/uneritx-bank/.git/ MERGE_MSG
Merge branch 'delhi' into head-office
Esc :wq!
So, it simply merged the accounts in the head-office branch and created a new commit. Merge branch 'delhi' into head-office
$ git log commit
f0737cbeaa0cc5a54eda6362aff194d3d55b4df9 (HEAD -> head-office, delhi)
Merge: d6e98e5 b0a9035
Author:Author: Shivani Sahasrabudhe shivani@uneritx.com
Date: Sat Jul 3 22:17:50 2021 +0530
Merge branch 'delhi' into head-office
commit d6e98e5486fd2ebf0ea4b9e9bb72fa1deabce61a
Author:Author: Shivani Sahasrabudhe shivani@uneritx.com
Date: Sat Jul 3 22:17:17 2021 +0530
created harry's account
....
“Oh! What we can see is that there is a new account created in the Head-office branch by the name harry. ac.”
Since the Head-office is a parent branch, the Delhi branch must keep itself up to date with the Head-office branch. Any new accounts ( commits ) added to the head-branch must reflect in the Delhi branch and vice-versa.
Here comes the concept of Rebase. We will rebase our Head-office branch onto Delhi to ensure that the branch is up-to-date with the head-office branch records. Making sure no account information is missing in the Delhi Branch.
NOTE: Git rebase is used when you are working on a local Repository. If you are working on a remote hosted Repository, you can use git pull & git fetch commands instead.
$ git checkout delhi
$ git rebase head-office First, rewinding head to replay your work on top of it...
Fast-forwarded delhi to head-office.
We could see that rebase has re-written the account history ( commit history ). Added the commits from the head-office branch.created harry's account
You can check the account details and logs using.
$ git log
ubuntu@ubuntu-desktop~/uneritx-bank
$ git log
commit f0737cbeaa0cc5a54eda6362aff194d3d55b4df9 (HEAD -> delhi, head-office)
Merge: d6e98e5 b0a9035
Author:Author: Shivani Sahasrabudhe shivani@uneritx.com
Date: Sat Jul 3 22:17:50 2021 +0530
Merge branch 'delhi' into head-office commit d6e98e5486fd2ebf0ea4b9e9bb72fa1deabce61a
Author:Author: Shivani Sahasrabudhe shivani@uneritx.com
Date: Sat Jul 3 22:17:17 2021 +0530 created harry's account commit b0a9035456b670b6ef1fa8af6136882723a04b9f
Author:Author: Shivani Sahasrabudhe shivani@uneritx.com
Date: Sat Jul 3 22:16:03 2021 +0530 created tom, jerry account commit d61fc9497d88e8c5d8026d22c9261a071d170d94
Author:Author: Shivani Sahasrabudhe shivani@uneritx.com
Date: Sat Jul 3 22:15:31 2021 +0530
Next, we will try to explain the difference between Git Pull vs Git Fetch. Till then, stay tuned!