Top Description Inners Fields Constructors Methods
org.eclipse.jgit.api

public Class CheckoutCommand

extends GitCommand<Ref>
Class Inheritance
Static Imports
org.eclipse.jgit.treewalk.TreeWalk.OperationType.CHECKOUT_OP

Checkout a branch to the working tree.

Examples (git is a org.eclipse.jgit.api.Git instance):

Check out an existing branch:

git.checkout().setName("feature").call();

Check out paths from the index:

git.checkout().addPath("file1.txt").addPath("file2.txt").call();

Check out a path from a commit:

git.checkout().setStartPoint("HEADˆ").addPath("file1.txt").call();

Create a new branch and check it out:

git.checkout().setCreateBranch(true).setName("newbranch").call();

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();
See Also
Git documentation about Checkout

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum

Field Summary

Modifier and TypeField and Description
private Set<String>
private boolean
private CheckoutCommand.Stage
private boolean
private boolean
private boolean
private ProgressMonitor
private String
private boolean
private List<String>
private RevCommit
private String
private CheckoutResult
private CreateBranchCommand.SetupUpstreamMode
Inherited from org.eclipse.jgit.api.GitCommand:
repo

Constructor Summary

AccessConstructor and Description
protected
CheckoutCommand(Repository repo)

Constructor for CheckoutCommand

Method Summary

Modifier and TypeMethod and Description
public CheckoutCommand

Returns:

this
addPath
(String
path to update in the working tree and index (with / as separator)
path
)

Add a single slash-separated path to the list of paths to check out.

public CheckoutCommand

Returns:

this
addPaths
(List<String>
paths to update in the working tree and index (with / as separator)
p
)

Add multiple slash-separated paths to the list of paths to check out.

public Ref
call()

Implements abstract org.eclipse.jgit.api.GitCommand.call.

Implements java.util.concurrent.Callable.call.

Computes a result, or throws an exception if unable to do so.

private void
private void
checkoutPath(DirCacheEntry entry, ObjectReader reader, Checkout checkout, String path, DirCacheCheckout.CheckoutMetadata checkoutMetadata)

protected CheckoutCommand

Returns:

this instance
checkoutPaths
()

Checkout paths into index and working directory, firing a org.eclipse.jgit.events.WorkingTreeModifiedEvent if the working tree was modified.

private void
private void
private String
public CheckoutResult

Returns:

the result, never null
getResult
()

Get the result, never null

private String
private ObjectId
private boolean
private void
public CheckoutCommand

Returns:

this
setAllPaths
(boolean
true to checkout all paths, false otherwise
all
)

Set whether to checkout all paths.

public CheckoutCommand

Returns:

this instance
setCreateBranch
(boolean
if true a branch will be created as part of the checkout and set to the specified start point
createBranch
)

Specify whether to create a new branch.

public CheckoutCommand

Returns:

this instance
setForced
(boolean
if set to true then allow the checkout even if workingtree or index doesn't match HEAD. Overwrite workingtree files and index content with the new content in this case.
forced
)

Allow a checkout even if the workingtree or index differs from HEAD.

public CheckoutCommand

Returns:

this instance
setForceRefUpdate
(boolean
if true and the branch with the given name already exists, the start-point of an existing branch will be set to a new start-point; if false, the existing branch will not be changed
forceRefUpdate
)

Specify to force the ref update in case of a branch switch.

public CheckoutCommand

Returns:

this instance
setName
(String
the name of the branch or commit
name
)

Specify the name of the branch or commit to check out, or the new branch name.

public CheckoutCommand

Returns:

this instance
setOrphan
(boolean
if true a orphan branch will be created as part of the checkout to the specified start point
orphan
)

Specify whether to create a new orphan branch.

public CheckoutCommand

Returns:

this instance
setProgressMonitor
(ProgressMonitor
a progress monitor
monitor
)

Set progress monitor

public CheckoutCommand

Returns:

this
setStage
(CheckoutCommand.Stage
the stage to check out
stage
)

When checking out the index, check out the specified stage (ours or theirs) for unmerged paths.

public CheckoutCommand

Returns:

this instance
setStartPoint
(String
commit name to check out
startPoint
)

Set the name of the commit that should be checked out.

public CheckoutCommand

Returns:

this instance
setStartPoint
(RevCommit
commit to check out
startCommit
)

Set the commit that should be checked out.

public CheckoutCommand

Returns:

this instance
setUpstreamMode
(CreateBranchCommand.SetupUpstreamMode
corresponds to the --track/--no-track options; may be null
mode
)

When creating a branch with setCreateBranch(boolean), this can be used to configure branch tracking.

Inherited from org.eclipse.jgit.api.GitCommand:
checkCallablegetRepositorysetCallable

Field Detail

actuallyModifiedPathsback to summary
private Set<String> actuallyModifiedPaths
checkoutAllPathsback to summary
private boolean checkoutAllPaths
checkoutStageback to summary
private CheckoutCommand.Stage checkoutStage
createBranchback to summary
private boolean createBranch
forcedback to summary
private boolean forced
forceRefUpdateback to summary
private boolean forceRefUpdate
monitorback to summary
private ProgressMonitor monitor
nameback to summary
private String name
orphanback to summary
private boolean orphan
pathsback to summary
private List<String> paths
startCommitback to summary
private RevCommit startCommit
startPointback to summary
private String startPoint
statusback to summary
private CheckoutResult status
upstreamModeback to summary
private CreateBranchCommand.SetupUpstreamMode upstreamMode

Constructor Detail

CheckoutCommandback to summary
protected CheckoutCommand(Repository repo)

Constructor for CheckoutCommand

Parameters
repo:Repository

the org.eclipse.jgit.lib.Repository

Method Detail

addPathback to summary
public CheckoutCommand addPath(String path)

Add a single slash-separated path to the list of paths to check out. To check out all paths, use setAllPaths(boolean).

If this option is set, neither the setCreateBranch(boolean) nor setName(String) option is considered. In other words, these options are exclusive.

Parameters
path:String

path to update in the working tree and index (with / as separator)

Returns:CheckoutCommand

this

addPathsback to summary
public CheckoutCommand addPaths(List<String> p)

Add multiple slash-separated paths to the list of paths to check out. To check out all paths, use setAllPaths(boolean).

If this option is set, neither the setCreateBranch(boolean) nor setName(String) option is considered. In other words, these options are exclusive.

Parameters
p:List<String>

paths to update in the working tree and index (with / as separator)

Returns:CheckoutCommand

this

Since
4.6
callback to summary
public Ref call() throws GitAPIException, RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException, CheckoutConflictException

Implements abstract org.eclipse.jgit.api.GitCommand.call.

Implements java.util.concurrent.Callable.call.

Doc from org.eclipse.jgit.api.GitCommand.call.

Computes a result, or throws an exception if unable to do so.

Execute the command

Returns:Ref

Doc from java.util.concurrent.Callable.call.

computed result

Annotations
@Override
checkOptionsback to summary
private void checkOptions()
checkoutPathback to summary
private void checkoutPath(DirCacheEntry entry, ObjectReader reader, Checkout checkout, String path, DirCacheCheckout.CheckoutMetadata checkoutMetadata)
checkoutPathsback to summary
protected CheckoutCommand checkoutPaths() throws IOException, RefNotFoundException

Checkout paths into index and working directory, firing a org.eclipse.jgit.events.WorkingTreeModifiedEvent if the working tree was modified.

Returns:CheckoutCommand

this instance

Exceptions
IOException:
if an IO error occurred
RefNotFoundException:
if Ref couldn't be resolved
checkoutPathsFromCommitback to summary
private void checkoutPathsFromCommit(TreeWalk treeWalk, DirCache dc, RevCommit commit, Checkout checkout) throws IOException
checkoutPathsFromIndexback to summary
private void checkoutPathsFromIndex(TreeWalk treeWalk, DirCache dc, Checkout checkout) throws IOException
getBranchNameback to summary
private String getBranchName()
getResultback to summary
public CheckoutResult getResult()

Get the result, never null

Returns:CheckoutResult

the result, never null

getShortBranchNameback to summary
private String getShortBranchName(Ref headRef)
getStartPointObjectIdback to summary
private ObjectId getStartPointObjectId() throws AmbiguousObjectException, RefNotFoundException, IOException
isCheckoutIndexback to summary
private boolean isCheckoutIndex()
processOptionsback to summary
private void processOptions() throws InvalidRefNameException, RefAlreadyExistsException, IOException
setAllPathsback to summary
public CheckoutCommand setAllPaths(boolean all)

Set whether to checkout all paths.

This options should be used when you want to do a path checkout on the entire repository and so calling addPath(String) is not possible since empty paths are not allowed.

If this option is set, neither the setCreateBranch(boolean) nor setName(String) option is considered. In other words, these options are exclusive.

Parameters
all:boolean

true to checkout all paths, false otherwise

Returns:CheckoutCommand

this

Since
2.0
setCreateBranchback to summary
public CheckoutCommand setCreateBranch(boolean createBranch)

Specify whether to create a new branch.

If true is used, the name of the new branch must be set using setName(String). The commit at which to start the new branch can be set using setStartPoint(String) or setStartPoint(RevCommit); if not specified, HEAD is used. Also see setUpstreamMode for setting up branch tracking.

Parameters
createBranch:boolean

if true a branch will be created as part of the checkout and set to the specified start point

Returns:CheckoutCommand

this instance

setForcedback to summary
public CheckoutCommand setForced(boolean forced)

Allow a checkout even if the workingtree or index differs from HEAD. This matches native git's '--force' option. JGit releases before 5.2 had a method setForce() offering semantics different from this new setForced(). This old semantic can now be found in setForceRefUpdate(boolean)

Parameters
forced:boolean

if set to true then allow the checkout even if workingtree or index doesn't match HEAD. Overwrite workingtree files and index content with the new content in this case.

Returns:CheckoutCommand

this instance

Since
5.3
setForceRefUpdateback to summary
public CheckoutCommand setForceRefUpdate(boolean forceRefUpdate)

Specify to force the ref update in case of a branch switch. In releases prior to 5.2 this method was called setForce() but this name was misunderstood to implement native git's --force option, which is not true.

Parameters
forceRefUpdate:boolean

if true and the branch with the given name already exists, the start-point of an existing branch will be set to a new start-point; if false, the existing branch will not be changed

Returns:CheckoutCommand

this instance

Since
5.3
setNameback to summary
public CheckoutCommand setName(String name)

Specify the name of the branch or commit to check out, or the new branch name.

When only checking out paths and not switching branches, use setStartPoint(String) or setStartPoint(RevCommit) to specify from which branch or commit to check out files.

When setCreateBranch(boolean) is set to true, use this method to set the name of the new branch to create and setStartPoint(String) or setStartPoint(RevCommit) to specify the start point of the branch.

Parameters
name:String

the name of the branch or commit

Returns:CheckoutCommand

this instance

setOrphanback to summary
public CheckoutCommand setOrphan(boolean orphan)

Specify whether to create a new orphan branch.

If true is used, the name of the new orphan branch must be set using setName(String). The commit at which to start the new orphan branch can be set using setStartPoint(String) or setStartPoint(RevCommit); if not specified, HEAD is used.

Parameters
orphan:boolean

if true a orphan branch will be created as part of the checkout to the specified start point

Returns:CheckoutCommand

this instance

Since
3.3
setProgressMonitorback to summary
public CheckoutCommand setProgressMonitor(ProgressMonitor monitor)

Set progress monitor

Parameters
monitor:ProgressMonitor

a progress monitor

Returns:CheckoutCommand

this instance

Since
4.11
setStageback to summary
public CheckoutCommand setStage(CheckoutCommand.Stage stage)

When checking out the index, check out the specified stage (ours or theirs) for unmerged paths.

This can not be used when checking out a branch, only when checking out the index.

Parameters
stage:CheckoutCommand.Stage

the stage to check out

Returns:CheckoutCommand

this

setStartPointback to summary
public CheckoutCommand setStartPoint(String startPoint)

Set the name of the commit that should be checked out.

When checking out files and this is not specified or null, the index is used.

When creating a new branch, this will be used as the start point. If not specified or null, the current HEAD is used.

Parameters
startPoint:String

commit name to check out

Returns:CheckoutCommand

this instance

setStartPointback to summary
public CheckoutCommand setStartPoint(RevCommit startCommit)

Set the commit that should be checked out.

When creating a new branch, this will be used as the start point. If not specified or null, the current HEAD is used.

When checking out files and this is not specified or null, the index is used.

Parameters
startCommit:RevCommit

commit to check out

Returns:CheckoutCommand

this instance

setUpstreamModeback to summary
public CheckoutCommand setUpstreamMode(CreateBranchCommand.SetupUpstreamMode mode)

When creating a branch with setCreateBranch(boolean), this can be used to configure branch tracking.

Parameters
mode:CreateBranchCommand.SetupUpstreamMode

corresponds to the --track/--no-track options; may be null

Returns:CheckoutCommand

this instance

org.eclipse.jgit.api back to summary

public final Enum CheckoutCommand.Stage

extends Enum<CheckoutCommand.Stage>
Class Inheritance

Stage to check out, see CheckoutCommand#setStage(Stage).

Field Summary

Modifier and TypeField and Description
public static final CheckoutCommand.Stage
BASE

Base stage (#1)

private final int
public static final CheckoutCommand.Stage
OURS

Ours stage (#2)

public static final CheckoutCommand.Stage
THEIRS

Theirs stage (#3)

Constructor Summary

AccessConstructor and Description
private
Stage(int number)

Method Summary

Modifier and TypeMethod and Description
public static CheckoutCommand.Stage
public static CheckoutCommand.Stage[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

BASEback to summary
public static final CheckoutCommand.Stage BASE

Base stage (#1)

numberback to summary
private final int number
OURSback to summary
public static final CheckoutCommand.Stage OURS

Ours stage (#2)

THEIRSback to summary
public static final CheckoutCommand.Stage THEIRS

Theirs stage (#3)

Constructor Detail

Stageback to summary
private Stage(int number)

Method Detail

valueOfback to summary
public static CheckoutCommand.Stage valueOf(String name)
valuesback to summary
public static CheckoutCommand.Stage[] values()