Skip to content

Don't do git merge

LIU Hao edited this page Jan 24, 2024 · 7 revisions

Steps to describe the issue

1. Create a base branch.

  • lh_mouse@lhmouse-xps ~ $ mkdir test_repo/
  • lh_mouse@lhmouse-xps ~ $ cd test_repo/
  • lh_mouse@lhmouse-xps ~/test_repo $ git init -b base
    Initialized empty Git repository in /home/lh_mouse/test_repo/.git/
  • lh_mouse@lhmouse-xps ~/test_repo $ echo one >> f.txt
  • lh_mouse@lhmouse-xps ~/test_repo $ git add f.txt
  • lh_mouse@lhmouse-xps ~/test_repo $ git commit -m "one"
    [base (root-commit) 05df618] one
    1 file changed, 1 insertion(+)
    create mode 100644 f.txt
  • lh_mouse@lhmouse-xps ~/test_repo $ git log --oneline
    05df618 (HEAD -> base) one

2. Fork a branch and commit something.

  • lh_mouse@lhmouse-xps ~/test_repo $ git checkout -B dev
    Switched to a new branch 'dev'
  • lh_mouse@lhmouse-xps ~/test_repo $ echo "dev two" >> f.txt
  • lh_mouse@lhmouse-xps ~/test_repo $ git commit -m"dev two" f.txt
    [dev 42a89ec] dev two
    1 file changed, 1 insertion(+)
  • lh_mouse@lhmouse-xps ~/test_repo $ git log --oneline
    42a89ec (HEAD -> dev) dev two
    05df618 (base) one

3. Return to the base branch and commit something else.

  • lh_mouse@lhmouse-xps ~/test_repo $ git checkout base
    Switched to branch 'base'
  • lh_mouse@lhmouse-xps ~/test_repo $ echo "base three" >> f.txt
  • lh_mouse@lhmouse-xps ~/test_repo $ git commit -m"base three" f.txt
    [base b3d829b] base three
    1 file changed, 1 insertion(+)
  • lh_mouse@lhmouse-xps ~/test_repo $ git log --oneline
    b3d829b (HEAD -> base) base three
    05df618 one

4. Try merging the two branches with a conflict.

  • lh_mouse@lhmouse-xps ~/test_repo $ git merge --no-ff dev
    Auto-merging f.txt
    CONFLICT (content): Merge conflict in f.txt
    Automatic merge failed; fix conflicts and then commit the result.
  • lh_mouse@lhmouse-xps ~/test_repo $ git diff
    diff --cc f.txt
    index 33f639d,e9837ff..0000000
    --- a/f.txt
    +++ b/f.txt
    @@@ -1,2 -1,2 +1,6 @@@
      one
    ++<<<<<<< HEAD
     +base three
    ++=======
    + dev two
    ++>>>>>>> dev

5. Resolve the conflict by discarding base changes.

  • lh_mouse@lhmouse-xps ~/test_repo $ nano f.txt
  • lh_mouse@lhmouse-xps ~/test_repo $ git diff
    diff --cc f.txt
    index 33f639d,e9837ff..0000000
    --- a/f.txt
    +++ b/f.txt
  • lh_mouse@lhmouse-xps ~/test_repo $ git add f.txt
  • lh_mouse@lhmouse-xps ~/test_repo $ git commit
    [base 3d2c56d] Merge branch 'dev' into base
  • lh_mouse@lhmouse-xps ~/test_repo $ git log --oneline
    3d2c56d (HEAD -> base) Merge branch 'dev' into base
    b3d829b base three
    42a89ec (dev) dev two
    05df618 one

6. Examine the file, which contains no change from the base branch.

  • lh_mouse@lhmouse-xps ~/test_repo $ cat f.txt
    one
    dev two
    

7. Examine the history, which contains no deletion; the change has vanished into thin air.

  • lh_mouse@lhmouse-xps ~/test_repo $ git log -p
    commit 3d2c56d4bf6edcf327103b6a5e19bfc3a7e1de66 (HEAD -> base)
    Merge: b3d829b 42a89ec
    Author: LIU Hao <[email protected]>
    Date:   2024-01-24 15:18:24 +0800
    
        Merge branch 'dev' into base
    
    commit b3d829b43f577bda4498456cff31d980c9934410
    Author: LIU Hao <[email protected]>
    Date:   2024-01-24 15:01:45 +0800
    
        base three
    
    diff --git a/f.txt b/f.txt
    index 5626abf..33f639d 100644
    --- a/f.txt
    +++ b/f.txt
    @@ -1 +1,2 @@
     one
    +base three
    
    commit 42a89ec457bf1391a6e674fec759e2f318c04a00 (dev)
    Author: LIU Hao <[email protected]>
    Date:   2024-01-24 14:57:32 +0800
    
        dev two
    
    diff --git a/f.txt b/f.txt
    index 5626abf..e9837ff 100644
    --- a/f.txt
    +++ b/f.txt
    @@ -1 +1,2 @@
     one
    +dev two
    
    commit 05df618feb13c0e7e0a157da2112c2bd43d24881
    Author: LIU Hao <[email protected]>
    Date:   2024-01-24 14:51:57 +0800
    
        one
    
    diff --git a/f.txt b/f.txt
    new file mode 100644
    index 0000000..5626abf
    --- /dev/null
    +++ b/f.txt
    @@ -0,0 +1 @@
    +one

Conclusion

Incorrect use of git merge may result in invisible data loss in history. In addition, it also complicates bisecting.

So, don't do git merge.