Thursday, November 12, 2020

git merge: 3-way merging


If it is not the linear path, then we need the 3-way merging.

First of all, create a new branch called bugFix from the master branch, and add a new commit called C4 to it.

---------------------------------------------------------------------------------

(master)

C1 -> C2 -> C3

-> C4

(bugFix branch)


Before merging the C4 commit to the bugFix branch, the master branch has added a new commit called C5. From the chart below, it is not the linear path anymore.

---------------------------------------------------------------------------------

(master) C1 -> C2 -> C3 -> C5 -> C4 (bugFix branch)


Use the 'git merge' command to merge the bugFix branch to the master branch.

---------------------------------------------------------------------------------

(master) C1 -> C2 -> C3 -> C5 -> C6 -> C4 -> (bugFix branch)


The 'git merge' command will help to create a new commit C6 and its parents are C4 and C5.

Below are the logs from my experiment (3-way merging without conflict)


Step 1:


Check the status of the bugFix branch.


    $ git log
    commit ec75b69d779c9f1d735f7aa1b7a551b9039eab6b (HEAD -> bugfix)
    Author: Frank <frank@demo.com>
    Date:   Timestamp

        1 commit for bugFix branch

    commit b4c8993ddff375af46ccb0f2a632e172ffdf1bfc
    Author: Frank <frank@demo.com>
    Date:   Timestamp

        1 commit for master


We notice that the bugFix branch pointed to the ec75b69d commit, but there is no info from the master branch in the git log.


Step 2:


Switch to the master branch and show the status.


    $ git checkout master

    $ git log
    commit d8563b55f6932b7819f4a6b107b8878ca7e73cff (HEAD -> master)
    Author: Frank <frank@demo.com>
    Date:   Timestamp

        2 commit for master branch

    commit b4c8993ddff375af46ccb0f2a632e172ffdf1bfc
    Author: Frank <frank@demo.com>
    Date:   Timestamp

        1 commit for master


Step 3:


Run the 'git merge' command to merge the bugFix branch to the master branch.


    $ git merge bugFix

    Then there is a prompt message:
    ----------------------------------------
    Merge branch 'bugFix'
    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.
    ----------------------------------------

    // Message after saving the prompt message
    Merge made by the 'recursive' strategy.
    2.txt | 1 +
    1 file changed, 1 insertion(+)
    create mode 100644 2.txt

It is the auto-generated commit coming from the 'git merge' command.


Step 4:


Check the status.


    $ git log
    commit dc32187755d5bf601156db6e8e1cb6c9efd67f31 (HEAD -> master)
    Merge: d8563b5 ec75b69
    Author: Frank <frank@demo.com>
    Date:   Timestamp

        Merge branch 'bugfix'

    commit d8563b55f6932b7819f4a6b107b8878ca7e73cff
    Author: Frank <frank@demo.com>
    Date:   Timestamp

        2 commit for master branch

    commit ec75b69d779c9f1d735f7aa1b7a551b9039eab6b (bugfix)
    Author: Frank <frank@demo.com>
    Date:   Timestamp

        1 commit for bugfix branch

    commit b4c8993ddff375af46ccb0f2a632e172ffdf1bfc
    Author: Frank <frank@demo.com>
    Date:   Timestamp

        1 commit for master


The commit dc3218 is the auto-generated commit by the 'git merge' command.

Check the detail for that commit.

    $ git cat-file -p dc3218
    tree 7eab585dfd08ca9f7524e71369236c074c9e4d93
    parent d8563b55f6932b7819f4a6b107b8878ca7e73cff
    parent ec75b69d779c9f1d735f7aa1b7a551b9039eab6b
    author Frank <frank@demo.com>
    committer Frank <frank@demo.com>

    Merge branch 'bugFix'

We notice that there are two parents in this commit object!!

Check the first parent info that shows that it is the latest commit from the master branch.


    $ git cat-file -p d8563b
    tree f6fa4e35a4c085d03ea91b14ba15f2ebfe4d09a7
    parent b4c8993ddff375af46ccb0f2a632e172ffdf1bfc
    author Frank <frank@demo.com>
    committer Frank <frank@demo.com>

    2 commit for master branch


Check the second parent info that shows that it is the latest commit from the bugFix branch.


    $ git cat-file -p ec75b69d7
    tree 434943a8265129a744745e5d12fa2625a784b283
    parent b4c8993ddff375af46ccb0f2a632e172ffdf1bfc
    author Frank <frank@demo.com>
    committer Frank <frank@demo.com>

    1 commit for bugfix branch


No comments:

Post a Comment