Main Content

createBranch

Create new Git branch

Since R2023b

    Description

    branchDetails = createBranch(repo,name) creates a new branch in the Git™ repository repo with the name name from the most recent commit.

    example

    branchDetails = createBranch(repo,name,StartPoint=commitID) creates a new branch in the Git repository repo with the name name from the specified commit.

    example

    Examples

    collapse all

    Navigate to your repository folder and create a repository object.

    repo = gitrepo;

    Create a new branch from the last commit of the currently checked-out branch.

    branchDetails = createBranch(repo,"NewFeature")
    
    branchDetails = 
    
      GitBranch with properties:
    
              Name: "NewFeature"
        LastCommit: [1×1 GitCommit]  (1376b77)

    Navigate to your repository folder and create a repository object.

    repo = gitrepo;

    Create a new branch from a commit and switch to the new branch.

    newBranch = createBranch(repo,"InvestigateBug",StartPoint="1376b77");
    switchBranch(repo,newBranch);
    

    When you finish the work, delete the branch.

    deleteBranch(repo,newBranch);

    Input Arguments

    collapse all

    Git repository, specified as a matlab.git.GitRepository object.

    Name of the branch, specified as a string scalar or a character vector.

    Example: "BugIssue1032", 'FeatureB'

    Data Types: char | string

    Revision specifiers (ID) of start point commit, specified as a string scalar or a character vector. If you do not specify this input, the function creates a branch from the most recent commit.

    If the start point is a remote branch, the createBranch function creates a local branch that tracks the remote branch automatically.

    Commit IDs support short, full, and relative commits.

    Example: "08a4c49", "08a4c49d249a4dc3d998b473cdda186f1c05dfd0", "origin/Dev"

    Data Types: char | string

    Output Arguments

    collapse all

    Details of the branch, returned as a matlab.git.GitBranch object.

    Version History

    Introduced in R2023b