Reputation: 1073
I have two branches, branch_a
and branch_b
.
branch_a
:
dir_1/a lot of files
branch_b
:
dir_1/a lot of changes
I now think it would be best if instead of just merging, I keep both directories for now.
Desired result:
dir_1/a lot of files
dir_2/a lot of changes
Of cause I could just copy all the files from branch_b
into dir_2
, but that would lose the history. Is there a "git-way" to solve this?
Upvotes: 1
Views: 44
Reputation: 114440
If possible, I would avoid having two copies of your content side-by-side at all. It will make merging the two back together more involved. However, without more information, I can not suggest an alternative, so here goes.
I am assuming that branch_a
and branch_b
have some common ancestor commit, but are not simple fast-forwards of each other. If they are, this likely won't work because any attempt to merge will result in a fast-forward, including the rename step.
In branch_a
, which contains the "legacy" version, move dir_1
to its intended path:
$ git checkout branch_a
$ git mv dir_1 dir_1-legacy
$ git commit -m 'Created separate folder'
I would recommend making a separate branch for the merged result, so you can do something like
$ git checkout -b branch_transition branch_b
If you prefer to just update branch_b
with the result, just do
$ git checkout branch_b
In either case, you can now merge in the two folders, preserving the history of each:
$ git merge branch_a
CONFLICT (modify/delete): dir_1/XXX deleted in a and modified in HEAD. Version HEAD of dir_1/XXX left in tree.
...
Automatic merge failed; fix conflicts and then commit the result.
$ git add dir_1
$ git commit -m 'Merged divergent paths'
The step git add dir_1
is necessary in response to the failed automatic merge, which happens to be exactly what you want under the circumstances.
I would also recommend bumping branch_a
back one commit, so that any future changes in it do not operate on the dir_1-legacy
folder:
git checkout branch_a
git reset --hard HEAD~
Re-merging
Once your transition is complete, you can re-merge the two folders using a similar technique. Let's say that you are on branch_transition
, and would like to merge the two folders together. You can do this by creating a new temporary branch, deleting one version of the folder in each of the branches, renaming one, and merging:
git checkout branch_transition
# Create temp branch to hold the legacy code
git checkout -b temp_transition
git rm dir_1
git mv dir_1-legacy dir_1
git commit -m 'Converted legacy to baseline'
# Set up merge on the transition branch
git checkout branch_transition
git rm dir_1-legacy
# Do the merge
git merge --strategy ours temp_transition
git branch -d temp_transition
You will need to use --strategy ours
to get the non-legacy version to trump the legacy one according to my experiments. This is not the same as doing --strategy recursive -X ours
, which does not work as intended.
Upvotes: 1