Class CheckoutCommand
Examples (git is a 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:
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanprivate CheckoutCommand.Stageprivate booleanprivate booleanprivate booleanprivate ProgressMonitorprivate Stringprivate booleanprivate RevCommitprivate Stringprivate CheckoutResultFields inherited from class org.eclipse.jgit.api.GitCommand
repo -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCheckoutCommand(Repository repo) Constructor for CheckoutCommand -
Method Summary
Modifier and TypeMethodDescriptionAdd a single slash-separated path to the list of paths to check out.Add multiple slash-separated paths to the list of paths to check out.call()private voidprivate voidcheckoutPath(DirCacheEntry entry, ObjectReader reader, DirCacheCheckout.CheckoutMetadata checkoutMetadata) protected CheckoutCommandCheckout paths into index and working directory, firing aWorkingTreeModifiedEventif the working tree was modified.private voidcheckoutPathsFromCommit(TreeWalk treeWalk, DirCache dc, RevCommit commit) private voidcheckoutPathsFromIndex(TreeWalk treeWalk, DirCache dc) private StringGet the result, nevernullprivate StringgetShortBranchName(Ref headRef) private ObjectIdprivate booleanprivate voidsetAllPaths(boolean all) Set whether to checkout all paths.setCreateBranch(boolean createBranch) Specify whether to create a new branch.setForce(boolean force) Deprecated.setForced(boolean forced) Allow a checkout even if the workingtree or index differs from HEAD.setForceRefUpdate(boolean forceRefUpdate) Specify to force the ref update in case of a branch switch.Specify the name of the branch or commit to check out, or the new branch name.setOrphan(boolean orphan) Specify whether to create a new orphan branch.setProgressMonitor(ProgressMonitor monitor) setStage(CheckoutCommand.Stage stage) When checking out the index, check out the specified stage (ours or theirs) for unmerged paths.setStartPoint(String startPoint) Set the name of the commit that should be checked out.setStartPoint(RevCommit startCommit) Set the commit that should be checked out.When creating a branch withsetCreateBranch(boolean), this can be used to configure branch tracking.Methods inherited from class org.eclipse.jgit.api.GitCommand
checkCallable, getRepository, setCallable
-
Field Details
-
name
-
forceRefUpdate
private boolean forceRefUpdate -
forced
private boolean forced -
createBranch
private boolean createBranch -
orphan
private boolean orphan -
upstreamMode
-
startPoint
-
startCommit
-
checkoutStage
-
status
-
paths
-
checkoutAllPaths
private boolean checkoutAllPaths -
actuallyModifiedPaths
-
monitor
-
-
Constructor Details
-
CheckoutCommand
Constructor for CheckoutCommand- Parameters:
repo- theRepository
-
-
Method Details
-
call
public Ref call() throws GitAPIException, RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException, CheckoutConflictExceptionExecute the command
- Specified by:
callin interfaceCallable<Ref>- Specified by:
callin classGitCommand<Ref>- Throws:
GitAPIExceptionRefAlreadyExistsExceptionRefNotFoundExceptionInvalidRefNameExceptionCheckoutConflictException
-
getShortBranchName
-
setProgressMonitor
- Parameters:
monitor- a progress monitor- Returns:
- this instance
- Since:
- 4.11
-
addPath
Add a single slash-separated path to the list of paths to check out. To check out all paths, usesetAllPaths(boolean).If this option is set, neither the
setCreateBranch(boolean)norsetName(String)option is considered. In other words, these options are exclusive.- Parameters:
path- path to update in the working tree and index (with/as separator)- Returns:
this
-
addPaths
Add multiple slash-separated paths to the list of paths to check out. To check out all paths, usesetAllPaths(boolean).If this option is set, neither the
setCreateBranch(boolean)norsetName(String)option is considered. In other words, these options are exclusive.- Parameters:
p- paths to update in the working tree and index (with/as separator)- Returns:
this- Since:
- 4.6
-
setAllPaths
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)norsetName(String)option is considered. In other words, these options are exclusive.- Parameters:
all-trueto checkout all paths,falseotherwise- Returns:
this- Since:
- 2.0
-
checkoutPaths
Checkout paths into index and working directory, firing aWorkingTreeModifiedEventif the working tree was modified.- Returns:
- this instance
- Throws:
IOExceptionRefNotFoundException
-
checkoutPathsFromIndex
- Throws:
IOException
-
checkoutPathsFromCommit
private void checkoutPathsFromCommit(TreeWalk treeWalk, DirCache dc, RevCommit commit) throws IOException - Throws:
IOException
-
checkoutPath
private void checkoutPath(DirCacheEntry entry, ObjectReader reader, DirCacheCheckout.CheckoutMetadata checkoutMetadata) -
isCheckoutIndex
private boolean isCheckoutIndex() -
getStartPointObjectId
private ObjectId getStartPointObjectId() throws AmbiguousObjectException, RefNotFoundException, IOException -
processOptions
private void processOptions() throws InvalidRefNameException, RefAlreadyExistsException, IOException -
getBranchName
-
setName
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)orsetStartPoint(RevCommit)to specify from which branch or commit to check out files.When
setCreateBranch(boolean)is set totrue, use this method to set the name of the new branch to create andsetStartPoint(String)orsetStartPoint(RevCommit)to specify the start point of the branch.- Parameters:
name- the name of the branch or commit- Returns:
- this instance
-
setCreateBranch
Specify whether to create a new branch.If
trueis used, the name of the new branch must be set usingsetName(String). The commit at which to start the new branch can be set usingsetStartPoint(String)orsetStartPoint(RevCommit); if not specified, HEAD is used. Also seesetUpstreamMode(org.eclipse.jgit.api.CreateBranchCommand.SetupUpstreamMode)for setting up branch tracking.- Parameters:
createBranch- iftruea branch will be created as part of the checkout and set to the specified start point- Returns:
- this instance
-
setOrphan
Specify whether to create a new orphan branch.If
trueis used, the name of the new orphan branch must be set usingsetName(String). The commit at which to start the new orphan branch can be set usingsetStartPoint(String)orsetStartPoint(RevCommit); if not specified, HEAD is used.- Parameters:
orphan- iftruea orphan branch will be created as part of the checkout to the specified start point- Returns:
- this instance
- Since:
- 3.3
-
setForce
Deprecated.this method was badly named comparing its semantics to native git's checkout --force option, usesetForceRefUpdate(boolean)insteadSpecify to force the ref update in case of a branch switch.- Parameters:
force- iftrueand 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:
- this instance
-
setForceRefUpdate
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- iftrueand 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:
- this instance
- Since:
- 5.3
-
setForced
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 methodsetForce()offering semantics different from this newsetForced(). This old semantic can now be found insetForceRefUpdate(boolean)- Parameters:
forced- if set totruethen 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:
- this instance
- Since:
- 5.3
-
setStartPoint
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- commit name to check out- Returns:
- this instance
-
setStartPoint
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- commit to check out- Returns:
- this instance
-
setUpstreamMode
When creating a branch withsetCreateBranch(boolean), this can be used to configure branch tracking.- Parameters:
mode- corresponds to the --track/--no-track options; may benull- Returns:
- this instance
-
setStage
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- the stage to check out- Returns:
- this
-
getResult
Get the result, nevernull- Returns:
- the result, never
null
-
checkOptions
private void checkOptions()
-
setForceRefUpdate(boolean)instead