Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » JGit checkout() is not like `git checkout`- Help(Trouble understanding JGit checkout)
JGit checkout() is not like `git checkout`- Help [message #1755177] Tue, 28 February 2017 17:26 Go to next message
bj cagnina is currently offline bj cagninaFriend
Messages: 3
Registered: January 2017
Junior Member
My git intuition is failing me when using JGit for checkouts.

My situation:

I open my git repo with Git.open(), everything looks good. I am on refs/heads/master, I do a Git.fetch().call() and Git.pull().call() just to make sure everything is updated and current.

I have an upstream branch: refs/remotes/origin/featureBranch.

Now, in this scenario, my normal git workflow would simply be:
`git checkout featureBranch`, not `git checkout -b featureBranch`, the latter of which is the real "create new branch".

However, it seems that JGit wants me to `this.git.checkout().setCreateBranch(true).setName("featureBranch").call()` -- which is misleading to me but I suppose is fine. Here is where I get into trouble:

After either a `git checkout featureBranch` or `git checkout -b featureBranch`, I am about to do `git pull --rebase`.

After checking out / creating / whatever JGit is doing, if I try to do a rebase in JGit here using:

PullCommand pullCommand = this.git.pull();
pullCommand.setRemoteBranchName("featureBranch");
pullCommand.setRebase(true);
PullResult ret = pullCommand.call();

The pull fails, and the error from ret.getRebaseResult().getStatus() is simply "STOPPED", which is uninformative.

When I look at my repo, It looks like there was a merge conflict that couldn't resolve--which doesn't make sense? I either just checked out the exact remote featureBranch--which wouldn't have a merge conflict--or I created a new local featureBranch then am trying to rebase remote changes onto it--which again, wouldn't create a conflict if using `git pull --rebase`

Any and all help is appreciated! I am drowning here. Thank you much.

[Updated on: Tue, 28 February 2017 17:31]

Report message to a moderator

Re: JGit checkout() is not like `git checkout`- Help [message #1755224 is a reply to message #1755177] Wed, 01 March 2017 10:06 Go to previous messageGo to next message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 233
Registered: July 2009
Senior Member
I would like to comment first on the checkout issue.

Why can you say "git checkout featureBranch" and JGit forces you to do something similar to "git checkout -b featureBranch ...". Native git seems to be able to do the checkout without branch creation while JGit is not. The thing is that in native git "git checkout featureBranch" also does a branch creation under the hood. When you issue "git checkout featureBranch" then native git tries to find a ref called featureBranch but can't find it. There is no branch, no tag, called featureBranch. But there is a remote tracking branch called "remotes/origin/featureBranch". And there seems to be a shortcut in native git that when you checkout a branch which doesn't exist locally but where a remote tracking branch with that name exists then a new local branch is created and checked out. That new branch is configured to have the remote branch as upstream.

JGit doesn't have this shortcut. JGit want's that you do the steps explicitly.

But maybe I am not fully understanding your setup. Therefore ... let's speak git:

> git --version
git version 2.10.2
> git clone https://github.com/chalstrick/testRepo.git
Cloning into 'testRepo'...
remote: Counting objects: 129, done.
remote: Total 129 (delta 0), reused 0 (delta 0), pack-reused 129
Receiving objects: 100% (129/129), 7.80 KiB | 0 bytes/s, done.
Resolving deltas: 100% (69/69), done.
> cd testRepo/
> git show-ref
ad867e69fc40c28a91775e7c26cbf493b3880b38 refs/heads/master
f39d5061a91430cb53c57561626897ef27401f3f refs/remotes/origin/Add_E
625d04966f1f78014cd5208ec9791b255cfad370 refs/remotes/origin/Add_Pi
84ec6164c2fad7c5c751ee856cc820407547cabf refs/remotes/origin/AddingSubMult
91003743cec7efeefe63ce9043f8845ac61800f6 refs/remotes/origin/AddingTests
ad867e69fc40c28a91775e7c26cbf493b3880b38 refs/remotes/origin/HEAD
470402e59e27c084b904df3d3d77e451d1c2de3e refs/remotes/origin/Modify_Add_Double
a1c3efbee8184b98dd2142c2352bd5224b68c2e9 refs/remotes/origin/Modify_Add_Float
f981e98035e640e319d7aaa1b3f364f2c2877913 refs/remotes/origin/RefactorCalc
ad867e69fc40c28a91775e7c26cbf493b3880b38 refs/remotes/origin/master
> git log Add_Pi
fatal: ambiguous argument 'Add_Pi': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
> ### See: native git does not know about a ref called Add_Pi
> ### Although it will allow you to checkout Add_Pi and it will create a new branch automatically
> git checkout Add_Pi
Branch Add_Pi set up to track remote branch Add_Pi from origin.
Switched to a new branch 'Add_Pi'
> git show-ref
625d04966f1f78014cd5208ec9791b255cfad370 refs/heads/Add_Pi
ad867e69fc40c28a91775e7c26cbf493b3880b38 refs/heads/master
f39d5061a91430cb53c57561626897ef27401f3f refs/remotes/origin/Add_E
625d04966f1f78014cd5208ec9791b255cfad370 refs/remotes/origin/Add_Pi
84ec6164c2fad7c5c751ee856cc820407547cabf refs/remotes/origin/AddingSubMult
91003743cec7efeefe63ce9043f8845ac61800f6 refs/remotes/origin/AddingTests
ad867e69fc40c28a91775e7c26cbf493b3880b38 refs/remotes/origin/HEAD
470402e59e27c084b904df3d3d77e451d1c2de3e refs/remotes/origin/Modify_Add_Double
a1c3efbee8184b98dd2142c2352bd5224b68c2e9 refs/remotes/origin/Modify_Add_Float
f981e98035e640e319d7aaa1b3f364f2c2877913 refs/remotes/origin/RefactorCalc
ad867e69fc40c28a91775e7c26cbf493b3880b38 refs/remotes/origin/master
> ### There is a new branch now called refs/heads/Add_Pi
> git log Add_Pi
commit 625d04966f1f78014cd5208ec9791b255cfad370
Author: Christian Halstrick <christian.halstrick@sap.com>
...
Re: JGit checkout() is not like `git checkout`- Help [message #1755251 is a reply to message #1755224] Wed, 01 March 2017 16:48 Go to previous messageGo to next message
bj cagnina is currently offline bj cagninaFriend
Messages: 3
Registered: January 2017
Junior Member
Interesting. Thanks for that observation. So how do I achieve this in JGit?

When I look at the doc for checkout, aside from setUpstreamMode(), I do not see anything that will allow me to have "configured to have the remote branch as upstream." So if setUpstreamMode() is the correct approach, what mode would be the one to choose?:
http://download.eclipse.org/jgit/docs/jgit-2.3.1.201302201838-r/apidocs/org/eclipse/jgit/api/CheckoutCommand.html


Re: JGit checkout() is not like `git checkout`- Help [message #1755271 is a reply to message #1755251] Wed, 01 March 2017 21:42 Go to previous messageGo to next message
Matthias Sohn is currently offline Matthias SohnFriend
Messages: 924
Registered: July 2009
Senior Member
Upstream branch of a local branch is configured in git config,
see classes BranchConfig, BranchTrackingStatus and BranchTrackingStatusTest in JGit
Re: JGit checkout() is not like `git checkout`- Help [message #1759722 is a reply to message #1755251] Mon, 17 April 2017 14:44 Go to previous message
Daniel Holz is currently offline Daniel HolzFriend
Messages: 2
Registered: April 2017
Junior Member
bj cagnina wrote on Wed, 01 March 2017 16:48
Interesting. Thanks for that observation. So how do I achieve this in JGit?

When I look at the doc for checkout, aside from setUpstreamMode(), I do not see anything that will allow me to have "configured to have the remote branch as upstream." So if setUpstreamMode() is the correct approach, what mode would be the one to choose?:
http://download.eclipse.org/jgit/docs/jgit-2.3.1.201302201838-r/apidocs/org/eclipse/jgit/api/CheckoutCommand.html




That doc you linked to seems to have what you want as an example:

Quote:

Create a new tracking branch for a remote branch and check it out:
 git.checkout().setCreateBranch(true).setName("stable")
                .setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM)
                .setStartPoint("origin/stable").call();
 

Previous Topic:finding when push has completed.
Next Topic:clone all repositories
Goto Forum:
  


Current Time: Tue May 30 03:41:22 GMT 2017

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

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