Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » How to clone or checkout a specific branch ?(Check out a branch)
How to clone or checkout a specific branch ? [message #934097] Fri, 05 October 2012 16:53 Go to next message
kaleeswaran S is currently offline kaleeswaran SFriend
Messages: 6
Registered: September 2012
Junior Member
In my java application i am using Jgit to clone the github project. It is working fine. My scenario is i am having git repo with branches like (master, kalees).

I want to checkout a specific brach kalees. How to checkout a branch using Jgit.

My code so far i am using is

Git repo = Git.cloneRepository().setURI(url).setDirectory(directory).call();
for (Ref b : repo.branchList().setListMode(ListMode.ALL).call()) {
S_LOGGER.debug("(standard): cloned branch " + b.getName());

Which clones the github repository. and i tried to checkout a kalees branch like


It does not checkout the master branch only..

if i use jgit like repo1.checkout.setName("refs/remotes/origin/kalees").call();

it checkout the kalees branch. Is this the correct way to checkout a branch ???

If this is correct , i am using jgit pull command

Git git = File(dir)); //checkout is the folder with .git
CheckoutCommand checkout = git.checkout();
Ref call = checkout.setName("kalees").call();

which throws error like it
throws org.eclipse.jgit.api.errors.DetachedHeadException: HEAD is detached.
What could be the , whether this is a checkout issue or pull issue ?
Re: How to clone or checkout a specific branch ? [message #941269 is a reply to message #934097] Fri, 12 October 2012 10:50 Go to previous message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 269
Registered: July 2009
Senior Member
It's like in native git. When you clone a repo (with jgit or native git) then you get in your local repository
- a remote tracking branch refs/remote/origin/xxx for every branch refs/heads/xxx which exists on the remote repo
- a branch refs/heads/yyy for that branch yyy to which the remote repo's HEAD pointed to. If the remote repo HEAD pointed to a branch called test then after the clone you will have on one local (non-tracking) branch test (and no main branch).

In your case I guess that the remote repo's HEAD pointed to master. If you clone that you'll end up locally with the branches refs/remotes/origin/master, refs/remotes/origin/kalees and refs/heads/master. Therefore it's clear you can checkout "master" but you cannot checkout "kalees". In git (and jgit) when you specify the name of the branch to checkout git will try to find it by prefixing the name with "refs", refs/heads". So master is found (because refs/heads/master exists) and kalees is not found. But origin/kalees can be checked out (because refs/heads/origin/kalees is there).

Here is some example code which clones and checks out every branch:

	public static void main(String args[]) throws IOException, GitAPIException,
			JGitInternalException {
		File tmpDir = new File(System.getProperty(""), "tmp"
				+ System.currentTimeMillis());
		try {
			Git r = Git.cloneRepository().setDirectory(tmpDir)
					.setProgressMonitor(new TextProgressMonitor()).call();
			for (Ref f : r.branchList().setListMode(ListMode.ALL).call()) {
				System.out.println("checked out branch " + f.getName()
						+ ". HEAD: " + r.getRepository().getRef("HEAD"));
			// try to checkout branches by specifying abbreviated names
			try {
			} catch (RefNotFoundException e) {
				System.err.println("couldn't checkout 'test'. Got exception: "
						+ e.toString() + ". HEAD: "
						+ r.getRepository().getRef("HEAD"));
		} finally {

	static void rm(File f) {
		if (f.isDirectory())
			for (File c : f.listFiles())

Previous Topic:rebase fails: "cannot delete file"
Next Topic:fatal: ambiguous argument 'origin/master': unknown revision or path not in the working tree.
Goto Forum:

Current Time: Wed Apr 08 07:07:10 GMT 2020

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

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

Back to the top