Class CheckoutCommand

  • All Implemented Interfaces:
    Callable<Ref>

    public class CheckoutCommand
    extends GitCommand<Ref>
    Checkout a branch to the working tree.

    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:
    Git documentation about Checkout
    • Constructor Detail

      • CheckoutCommand

        protected CheckoutCommand​(Repository repo)
        Constructor for CheckoutCommand
        Parameters:
        repo - the Repository
    • Method Detail

      • setProgressMonitor

        public CheckoutCommand setProgressMonitor​(ProgressMonitor monitor)
        Parameters:
        monitor - a progress monitor
        Returns:
        this instance
        Since:
        4.11
      • addPath

        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 - path to update in the working tree and index (with / as separator)
        Returns:
        this
      • addPaths

        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 - paths to update in the working tree and index (with / as separator)
        Returns:
        this
        Since:
        4.6
      • setAllPaths

        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 - true to checkout all paths, false otherwise
        Returns:
        this
        Since:
        2.0
      • setOrphan

        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 - if true a orphan branch will be created as part of the checkout to the specified start point
        Returns:
        this instance
        Since:
        3.3
      • setForce

        @Deprecated
        public CheckoutCommand setForce​(boolean force)
        Deprecated.
        this method was badly named comparing its semantics to native git's checkout --force option, use setForceRefUpdate(boolean) instead
        Specify to force the ref update in case of a branch switch.
        Parameters:
        force - 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:
        this instance
      • setForceRefUpdate

        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 - 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:
        this instance
        Since:
        5.3
      • setForced

        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 - 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:
        this instance
        Since:
        5.3
      • setStartPoint

        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 - commit name to check out
        Returns:
        this instance
      • setStartPoint

        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 - commit to check out
        Returns:
        this instance
      • setStage

        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 - the stage to check out
        Returns:
        this
      • getResult

        public CheckoutResult getResult()
        Get the result, never null
        Returns:
        the result, never null