Creating a Merge Commit to replace Ours by Yours

I have two branches A and B. What I want to do is create a new (merge) commit in with the current state A as the parent, which refers to the file tree described by B, discarding anything from A. Basically story B should be compressed into one commit.

The specific state of the repository consists of two independent branches that do not have a common ancestor (of two previously independent repositories), but describe the same content. Now I want to find "git" - a way to combine them. The main solution (without git) would be to check A and just copy the contents of B to the working tree and make a git commit . This is basically what I did earlier to distribute the contents of the second repository to the first.

To do this with git, I tried

 git checkout A git merge --squash B 

But, fortunately, it caused merge conflicts for all files that differ between A and B, which is definitely not what I expected.

Basically something like

 git merge --squash -s theirs 

must complete the task, but theirs merge theirs does not exist. Reading documents the ability to use something like

 git merge -X theirs 

which is a recursive merger strategy option. But this still makes the merging of conflict-free pieces. Only conflicting pieces are taken directly from theirs .

+6
source share
1 answer

As you comment, of all the associations - your strategies that I list in the git command to create one branch, like another ", the second option is close to what you need:

Shows as a merger, with ours as the first parent.
(suggested by jcwenger )

 git checkout -b tmp upstream git merge -s ours thebranch # ignoring all changes from downstream git checkout downstream git merge --squash tmp # apply changes from tmp but not as merge. git rev-parse upstream > .git/MERGE_HEAD #record upstream 2nd merge head git commit -m "rebaselined the branch from upstream" # make the commit. git branch -D tmp # deleting tmp 
+2
source

Source: https://habr.com/ru/post/922027/


All Articles