0

I have a bare Git repository. Let's say I have some content in branch master and the head is pointing at commit "C1".

test.txt at C1 contains

line-1

Now, I create a new branch ref R1 and add a new commit C11 with its parent commit being C1 (head of R1).

test.txt at C11 contains

line-1
line-2
line-3

I pulled master and see that a new commit C2 is added. It contains test.txt as

line-1
line-2

Now, I want to merge C2 with C11 and create a new commit.

The issue I am facing is while merging.

I think this should auto-merge without any conflicts.

Bare Git Merge Scenario

To merge:

Merger merger = MergeStrategy.RECURSIVE.newMerger(git.getRepository(), true);
merger.merge('C2', 'C11');

Here, the merge result is conflicted. But ideally, it should have been a non-conflicting auto-merge.

The JGit API for merging org.eclipse.jgit.merge.MergeAlgorithm#merge has the following input:


base

line-1

ours

line-1
line-2

theirs

line-1
line-2
line-3

What could be missing here / Anything special needs to be done while merging for bare repositories? Thanks.

Omkar Shetkar
  • 3,488
  • 5
  • 35
  • 50
  • What is the ref you created? Is it a branch? – evolutionxbox Sep 17 '21 at 09:40
  • Yes. `R1` is a branch ref. – Omkar Shetkar Sep 17 '21 at 10:18
  • 2
    I’m not convinced that bare repos can solve merge conflicts as they do not have a working tree to resolve them. – evolutionxbox Sep 17 '21 at 10:29
  • I was wondering how Github manages it. I guess Github creates bare repositories. – Omkar Shetkar Sep 17 '21 at 10:43
  • They do? I thought github forces the user to correct merge conflicts? – evolutionxbox Sep 17 '21 at 11:16
  • For merge conflicts, yes. But for non-conflicting changes, it auto-merges the files. In my post, I am more interested merging non-conflicting changes. For example, `test.txt` content in `master` and `R1`. – Omkar Shetkar Sep 17 '21 at 12:23
  • 1
    https://stackoverflow.com/a/35846057/989920 does this help? – evolutionxbox Sep 17 '21 at 12:25
  • Although jgit generally *duplicates* the things Git does, jgit is not Git and whatever answers you get here will be specific to jgit only, so I've snipped the [tag:git] tag. – torek Sep 17 '21 at 19:37
  • 1
    To merge non-conflicting commits, you could use an 'in-core' merger, created with `newMerger(repo, true)`, which doesn't touch the work dir. It should give you the merged tree from which you can construct a commit. JGit also provides low-level API to create commits. You could use the `MergeCommand` as a template to build a bare repo merger. A few details noted here may also help: https://www.codeaffine.com/2014/10/20/git-internals/ – Rüdiger Herrmann Sep 19 '21 at 09:27
  • With 'in-core' merger, it seems merge fails even when both commits contain auto-mergeable content. For example, in the above post, `test.txt` is auto-mergeable. Even for this, I get conflict during the merge. Does 'in-core' work only when both commits contain changes in different files and are not conflicting? Or am I missing anything here? Thanks. – Omkar Shetkar Sep 20 '21 at 10:44
  • Updated my post with some more details of the merge conflict scenario. – Omkar Shetkar Sep 21 '21 at 10:52

0 Answers0