Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Merge Commit replacing Ours by Theirs

Tags:

git

I have two branches A and B. What I want to do is to create a new (merge) commit on A with the current state of A as parent that refers to the file tree described by B discarding anything from A. Basically the history of B should be squashed into a single commit.

The concrete repository state consists of two independend branches, that do not have a common ancestor (coming from two formererly independent repositories), but that describe the same content. Now I want to find a "git"-way to bring them together. A basic solution (without git) would be to checkout A and just copy the content of B into the working tree and do a git commit. That is basically what I've done earlier to propagate the content of the second repository into the first one.

To do it with git I've tried

git checkout A
git merge --squash B

But unforunately it generated merge conflicts for all files that differ between A and B, what is definately not what I expected.

Basically something like

git merge --squash -s theirs

should do the job, but the merge strategy theirs does not exist. Reading the docu shows the possibility of using something like

git merge -X theirs

which is an option to the merge strategy recursive. But this still does a merge of non-conflicting chunks. Only the conflicting chunks are taken directly from theirs.

like image 456
Uwe Krüger Avatar asked Aug 03 '12 09:08

Uwe Krüger


People also ask

Does a merge create a new commit?

Merge branchesGit creates a new commit (M) that is referred to as a merge commit that results from combining the changes from your feature branch and master from the point where the two branches diverged.

What type of merge creates a new merge commit?

Explicit merges are the default merge type. The 'explicit' part is that they create a new merge commit.


1 Answers

As you comment, from all the merge --theirs strategies I list in "git command for making one branch like another", the second option is close to what you need:

Shows as a merge, with ours as the first parent.
(proposed 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
like image 190
VonC Avatar answered Oct 02 '22 22:10

VonC