Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » Just do a simple merge(How to use the merge command to merge two files)
Just do a simple merge [message #1690522] Fri, 27 March 2015 22:26 Go to next message
Arne Boockmeyer is currently offline Arne BoockmeyerFriend
Messages: 2
Registered: March 2015
Junior Member
Hi all,

I use JGit only in a simple setup. No branches, no forks, just one central repository. Now it could be possible, that two people changes the same file at the same position and a merge happens, when the second user pulls the repository before he pushes his changes.

Now I like to give the user the choice to say "ours" or "theirs" and merge the files in this way.

final MergeCommand mc = git.merge();
try {
    for (final ObjectId head : git.getRepository().readMergeHeads()) {
        mc.include(head);
    }
} catch (final NoWorkTreeException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
} catch (final IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
mc.setStrategy(MergeStrategy.THEIRS);
try {
    final MergeResult mr = mc.call();
} catch(... lots of cases){}


When I run this, a CheckoutConflictException appears. When I run this with MergeStrategy.OURS, a WrongRepositoryStateException appears.

Can somebody tell me, how to fix this?

Thank you!
Re: Just do a simple merge [message #1691784 is a reply to message #1690522] Thu, 09 April 2015 15:41 Go to previous message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 269
Registered: July 2009
Senior Member
What you try doesn't even work in native git. If I understood you right you first triggered a "git pull" which lead to conflicts. And then, in that repo which is still in the middle of a merge and which contains conflicts you trigger a new merge command? This can't work. If you want to start a merge (with JGit or with native git) you shouldn't be in a state where you have unsuccesfully pulled. The best way to try this out is to work with command line git and see what you have to do there. This should tell how to programm it with jgit.

I tried to reproduce what you did with native git. Native git gives you a hint what you have to do after the pull. For those files conflicting you should fix the conflict by "git add" new content. Alternativly you could also checkout OURS or THEIRS for the conflicting files. When done resolving the conflicts you can just "git commit" to finally finish the pull.

+ git init --bare central
Initialized empty Git repository in /tmp/central/
+ git clone central/ bob
Cloning into 'bob'...
warning: You appear to have cloned an empty repository.
done.
+ cd bob/
+ echo -e '1\n2\n3'
+ echo -e '1\n2\n3'
+ git add .
+ git commit -m initial
[master (root-commit) 22cc5a7] initial
 2 files changed, 6 insertions(+)
 create mode 100644 common1
 create mode 100644 common2
+ git push
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 225 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To /tmp/central/
 * [new branch]      master -> master
+ cd ..
+ git clone central/ dave
Cloning into 'dave'...
done.
+ cd bob/
+ echo -e '1bob\n2\n3'
+ echo -e '1\n2bob\n3'
+ echo bobsContent
+ git add .
+ git commit -m bobsChanges
[master 4fec0dd] bobsChanges
 3 files changed, 3 insertions(+), 2 deletions(-)
 create mode 100644 bobsFile
+ git push
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (5/5), 353 bytes | 0 bytes/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To /tmp/central/
   22cc5a7..4fec0dd  master -> master
+ cd ../dave
+ echo -e '1\n2\n3dave'
+ echo -e '1\n2dave\n3'
+ echo davesContent
+ git add .
+ git commit -m davesChanges
[master b5118ee] davesChanges
 3 files changed, 3 insertions(+), 2 deletions(-)
 create mode 100644 davesFile
+ git pull
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 5 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (5/5), done.
From /tmp/central
   22cc5a7..4fec0dd  master     -> origin/master
Auto-merging common2
CONFLICT (content): Merge conflict in common2
Auto-merging common1
Automatic merge failed; fix conflicts and then commit the result.
+ git merge -s ours FETCH_HEAD
error: merge is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.




Ciao
Chris
Previous Topic:EGit doesn't understand two consecutive asterisks in gitignore file
Next Topic:Egit shows differences that don't exist
Goto Forum:
  


Current Time: Tue Mar 31 07:18:49 GMT 2020

Powered by FUDForum. Page generated in 0.02407 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top