6

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.

Uwe Krüger
  • 403
  • 1
  • 4
  • 8
  • Would one of those `merge --their` strategies be helpful here? http://stackoverflow.com/questions/4911794/git-command-for-making-one-branch-like-another/4912267#4912267 – VonC Aug 03 '12 at 10:30
  • The mentioned simulation #2 should do the job. I just used it up to the `merge --squash`. Afterwards in the history of B there is a merge from A, but this is OK. If not desired, B can be resetted again. – Uwe Krüger Aug 03 '12 at 11:24
  • Ok, I have added #2 as an answer. – VonC Aug 03 '12 at 11:28

1 Answers1

2

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
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250