My team and I work on a large repository with some core plugins and multiple independent floating plugin repositories. Recently, a plugin has been promoted to the main repository with some changes, but I also have a WIP feature on the old repo. I’d like to safely merge my changes from the old repo to the new subdirectory.
Here’s the current folder setup, where I’m trying to merge foo_workInProgress/src
into BigRepo/src/plugins/foo/src
. I don’t need to keep history, since that already got lost when the files were copied to BigRepo and trying to isolate just my commits seems like more effort than it’s worth. Strictly speaking git diff
and sufficient patience should be enough, but the lure of automation claims many.
- BigRepo
- src/plugins
- foo
- src
- bar
- baz
- foo_workInProgress
- src
Prior Art
There are several related-seeming questions that don’t quite seem to work. Here’s three of them:
- Git merge subdirectory with another subdirectory seems like exactly what I want, but the only accepted answer links a blog post that no longer exists.
- Merging git repositories at different directory levels is concerned with merging independent repositories preserving history, not resolving a one-time merge conflict.
- Git merge two overlapping repositories also is concerned with preserving history, which complicates the solution a lot.
A little awkward, but I got this to happen by wiping foo/src
in BigRepo
and re-creating it, to force Git to treat the different branches as a merge. Before figuring that out I spent a very long time trying to persuade --no-ff
to do what I wanted.
From the branch you want to merge into (e.g. main
), begin by refreshing src
:
$ cd src/plugins/foo
$ rm -r src
$ git commit -m "Temporarily clear src"
$ git revert HEAD
Then go back to that empty state and add the old repo’s version:
$ git checkout HEAD^
$ git switch -c updates
$ cp /path/to/foo_workInProgress/src ./src
$ git commit -m "A detailed commit message"
And now you can merge as usual:
$ git checkout main
$ git merge updates
This produces the correct result, including all the merge conflicts. It also generalizes easily to doing the same thing on multiple directories in bulk, should that be necessary (e.g. maybe your changes are localized to only specific files and you don’t need to conflict on every diff).