Let’s say we have three branches feature1, 2 and master branch.
Where master branch is our final branch. Git learns each branch
as same. The each branch can’t see other branch commits until a
merge happened. There are two ways for merging branch, once is
fast-forward merge and other one is 3-way merge.
Fast Forward Merge
Let’s say we have some commits in master branch and we have
created a new branch and made some commits in that branch as
well. We can do fast forward only when there is no further
commits done in the receiving branch i.e. master branch. Below
are some steps for fast forward merge.
- Create a new feature branch from the main branch
- Make changes in the new branch and commit them
- Checkout main branch (receiving branch)
- Merge feature branch to the current receiving branch
- Delete the feature branch
Let’s try the fast forward merge.
[varelite1@managed1 repo]$ git status
On branch master
nothing to commit, working tree clean
[varelite1@managed1 repo]$ git branch
* master
[varelite1@managed1 repo]$ git branch work1
[varelite1@managed1 repo]$ git checkout work1
Switched to branch 'work1'
[varelite1@managed1 repo]$ git branch
master
* work1
[varelite1@managed1 repo]$ touch data{1..5}
[varelite1@managed1 repo]$ echo "some files" > data1
[varelite1@managed1 repo]$ git add .
[varelite1@managed1 repo]$ git commit -m "first commit in work1"
[work1 7656400] first commit in work1
5 files changed, 1 insertion(+)
create mode 100644 data1
create mode 100644 data2
create mode 100644 data3
create mode 100644 data4
create mode 100644 data5
[varelite1@managed1 repo]$ echo "Hello Varelite" > varelite.txt
[varelite1@managed1 repo]$ git add varelite.txt
[varelite1@managed1 repo]$ git commit -m "second commit"
[work1 c54f550] second commit
1 file changed, 1 insertion(+), 2 deletions(-)
[varelite1@managed1 repo]$ ls
code1 code2 data1 data2 data3 data4 data5 index.html index.php managed1.txt managed2.txt test1 test2 test3 varelite.txt
[varelite1@managed1 repo]$ git branch
master
* work1
[varelite1@managed1 repo]$ git log
commit c54f550d45aeace16fc9b61e541d6519749eb4d2 (HEAD -> work1)
Author: varelite1 <varelit1@kb.com>
Date: Sun May 26 18:35:13 2024 +0530
second commit
commit 765640061aa1015c02185e39dd0234bfe1675ecf
Author: varelite1 <varelit1@kb.com>
Date: Sun May 26 18:34:40 2024 +0530
first commit in work1
[varelite1@managed1 repo]$ git checkout master
Switched to branch 'master'
[varelite1@managed1 repo]$ git log
commit a1ce2aa6b28165035101af8b741535af92c1f956 (HEAD -> master, origin/master)
Merge: 3b10806 55946ab
Author: somethingtostudy12 <78009632+somethingtostudy12@users.noreply.github.com>
Date: Sat May 25 16:06:02 2024 +0530
Merge pull request #1 from somethingtostudy12/staging
merging staging to master
Now you are on master branch just merge it.
[varelite1@managed1 repo]$ git merge work1
Updating a1ce2aa..c54f550
Fast-forward
data1 | 1 +
data2 | 0
data3 | 0
data4 | 0
data5 | 0
varelite.txt | 3 +--
6 files changed, 2 insertions(+), 2 deletions(-)
create mode 100644 data1
create mode 100644 data2
create mode 100644 data3
create mode 100644 data4
create mode 100644 data5
[varelite1@managed1 repo]$ ls
code1 code2 data1 data2 data3 data4 data5 index.html index.php managed1.txt managed2.txt test1 test2 test3 varelite.txt
[varelite1@managed1 repo]$ git log
commit c54f550d45aeace16fc9b61e541d6519749eb4d2 (HEAD -> master, work1)
Author: varelite1 <varelit1@kb.com>
Date: Sun May 26 18:35:13 2024 +0530
second commit
You can see that one file added. You can see Fast-forward
mentioned. There is also two sha1 hash is mentioned. First one
was when there is no merge so HEAD was on that sha1 hash. Once
merged happened then you can see second sha1 hash.
3 Way Merge
In this case we will have some more commits in master branch
after new branch named feature has been created. Here fast
forward will not work. You need to do 3 Way merge and for that
we will find a commit which is common to both and nearest
commit which is called Ancestor. How git do it. Basically, it
creates a new commit on the basis commit of master and feature
branch with Ancestor commit.
Let’s do it.
[varelite1@managed1 repo]$ git status
On branch master
nothing to commit, working tree clean
[varelite1@managed1 repo]$ git branch work2
[varelite1@managed1 repo]$ git checkout work2
Switched to branch 'work2'
[varelite1@managed1 repo]$ git branch
master
* work2
[varelite1@managed1 repo]$ ls
code1 code2 data1 data2 data3 data4 data5 index.html index.php managed1.txt managed2.txt test1 test2 test3 varelite.txt
[varelite1@managed1 repo]$ echo "work2" >> work2-1.txt
[varelite1@managed1 repo]$ git add work2-1.txt
[varelite1@managed1 repo]$ git commit -m "first commit in work2 branch"
[work2 6fcc8ba] first commit in work2 branch
1 file changed, 1 insertion(+)
create mode 100644 work2-1.txt
[varelite1@managed1 repo]$ echo "work2" > work2-2.txt
[varelite1@managed1 repo]$ git add work2-2.txt
[varelite1@managed1 repo]$ git commit -m "Second commit in work2 branch"
[work2 f1ad573] Second commit in work2 branch
1 file changed, 1 insertion(+)
create mode 100644 work2-2.txt
[varelite1@managed1 repo]$ ls -l work2-*
-rw-r--r--. 1 varelite1 varelite1 6 May 26 19:25 work2-1.txt
-rw-r--r--. 1 varelite1 varelite1 6 May 26 19:26 work2-2.txt
[varelite1@managed1 repo]$ git log
commit f1ad5730eaa3a6a6e72bcb329547fd6996ceb5fa (HEAD -> work2)
Author: varelite1 <varelit1@kb.com>
Date: Sun May 26 19:26:19 2024 +0530
Second commit in work2 branch
commit 6fcc8baa4f32a051b5ed53f5f4ec110de9b070ae
Author: varelite1 <varelit1@kb.com>
Date: Sun May 26 19:25:29 2024 +0530
first commit in work2 branch
commit c54f550d45aeace16fc9b61e541d6519749eb4d2 (master)
Author: varelite1 <varelit1@kb.com>
Date: Sun May 26 18:35:13 2024 +0530
second commit
[varelite1@managed1 repo]$ git checkout master
Already on 'master'
If you will see master still at old commit. No work2 commit is
there.
[varelite1@managed1 repo]$ git log
commit c54f550d45aeace16fc9b61e541d6519749eb4d2 (HEAD -> master)
Author: varelite1 <varelit1@kb.com>
Date: Sun May 26 18:35:13 2024 +0530
second commit
commit 765640061aa1015c02185e39dd0234bfe1675ecf
Author: varelite1 <varelit1@kb.com>
Date: Sun May 26 18:34:40 2024 +0530
Let’s make some commit in master branch now.
[varelite1@managed1 repo]$ echo "8th commit" > master1.txt
[varelite1@managed1 repo]$ git add master1.txt
[varelite1@managed1 repo]$ git commit -m "commit after work2 branch commits"
[master d3b8d28] commit after work2 branch commits
1 file changed, 1 insertion(+)
create mode 100644 master1.txt
[varelite1@managed1 repo]$
Now will go ahead and merge the work2 branch to master. It will
take you to edit mode where you can see the reason of that.
[varelite1@managed1 repo]$ git merge work2
Merge branch 'work2' with master a 3 way merge (commit message)
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
Merge made by the 'ort' strategy.
work2-1.txt | 1 +
work2-2.txt | 1 +
2 files changed, 2 insertions(+)
create mode 100644 work2-1.txt
create mode 100644 work2-2.txt
[varelite1@managed1 repo]$ ls
code1 data1 data3 data5 index.php managed2.txt test1 test3 work2-1.txt
code2 data2 data4 index.html managed1.txt master1.txt test2 varelite.txt work2-2.txt
Now you have all files in master branch. You have all commit
logs now.
commit 2f379351f27ca75c4336cd869c26a05f46b6fa1f (HEAD -> master)
Merge: d3b8d28 f1ad573
Author: varelite1 <varelit1@kb.com>
Date: Sun May 26 20:40:05 2024 +0530
Merge branch 'work2' with master a 3 way merge
commit d3b8d288edb9f53daae0bcccec4dd9d61d88a832
Author: varelite1 <varelit1@kb.com>
Date: Sun May 26 20:37:01 2024 +0530
commit after work2 branch commits
commit f1ad5730eaa3a6a6e72bcb329547fd6996ceb5fa (work2)
Author: varelite1 <varelit1@kb.com>
Date: Sun May 26 19:26:19 2024 +0530
Second commit in work2 branch
commit 6fcc8baa4f32a051b5ed53f5f4ec110de9b070ae
Author: varelite1 <varelit1@kb.com>
Date: Sun May 26 19:25:29 2024 +0530
first commit in work2 branch