Merge error after converting Git submodule to subtree

后端 未结 4 1343
时光说笑
时光说笑 2021-02-07 05:36

I have a project where I was originally using submodules for some dependent code. It turns out that submodules are not really appropriate for this project (and they are hard to

4条回答
  •  无人及你
    2021-02-07 06:13

    (Caveat: I've never worked with subtrees and I don't know how complicated your actual repo is, so these solutions may not actually work for you.)

    From playing around with your sample repo, I've found two solutions that both seems to work, though they produce different commit trees:

    1. Use git merge -s resolve origin/branch

      ~/q14224966[master]> git reset --hard origin/master
      HEAD is now at a231acd add submodule
      ~/q14224966[master]> touch other.c && git add . && git commit -m "New commit."
      [master bc771ac] New commit.
       0 files changed
       create mode 100644 other.c
      ~/q14224966[master]> git merge -s resolve origin/branch 
      Trying really trivial in-index merge...
      error: Merge requires file-level merging
      Nope.
      Trying simple merge.
      Simple merge failed, trying Automatic merge.
      Adding sub/Makefile
      Adding sub/README
      Adding sub/src/main.c
      Merge made by the 'resolve' strategy.
       .gitmodules    | 3 ---
       sub            | 1 -
       sub/Makefile   | 1 +
       sub/README     | 1 +
       sub/src/main.c | 1 +
       5 files changed, 3 insertions(+), 4 deletions(-)
       delete mode 160000 sub
       create mode 100644 sub/Makefile
       create mode 100644 sub/README
       create mode 100644 sub/src/main.c
      ~/q14224966[master]> ls
      README   main.c   other.c  sub/
      ~/q14224966[master]> cd sub/
      ~/q14224966/sub[master]> ls
      Makefile  README    src/
      ~/q14224966/sub[master]> git status
      # On branch master
      # Your branch is ahead of 'origin/master' by 5 commits.
      #
      nothing to commit (working directory clean)
      ~/q14224966/sub[master]> cd ..
      ~/q14224966[master]> git status
      # On branch master
      # Your branch is ahead of 'origin/master' by 5 commits.
      #
      nothing to commit (working directory clean)
      

      Here's the resulting commit tree: git commit tree - merge option

    2. Use a rebase instead of a merge:

      ~/q14224966[master]> git reset --hard origin/master 
      HEAD is now at a231acd add submodule
      ~/q14224966[master]> touch other.c && git add . && git commit -m "New commit."
      [master ae66060] New commit.
       0 files changed
       create mode 100644 other.c
      ~/q14224966[master]> git rebase origin/branch 
      First, rewinding head to replay your work on top of it...
      Applying: New commit.
      ~/q14224966[master]> ls
      README   main.c   other.c  sub/
      ~/q14224966[master]> cd sub/
      ~/q14224966/sub[master]> ls
      Makefile  README    src/
      ~/q14224966/sub[master]> git status
      # On branch master
      # Your branch is ahead of 'origin/master' by 4 commits.
      #
      nothing to commit (working directory clean)
      ~/q14224966/sub[master]> cd ..
      ~/q14224966[master]> git status
      # On branch master
      # Your branch is ahead of 'origin/master' by 4 commits.
      #
      nothing to commit (working directory clean)
      

      Here's the resulting commit tree: git commit tree - rebase option

提交回复
热议问题