Main Content

gitclone

Clone Git repository

Since R2023b

    Description

    example

    repo = gitclone(repositoryURL) clones the Git™ remote repository repositoryURL into the current folder and returns a matlab.git.GitRepository object.

    example

    repo = gitclone(repositoryURL,folder) clones the Git remote repository repositoryURL into the specified folder folder and returns a matlab.git.GitRepository object.

    example

    repo = gitclone(___,Name=Value) specifies additional options as one or more name-value arguments.

    Examples

    collapse all

    Clone a repository in the current folder.

    repo = gitclone("https://github.com/mathworks/Simulink-Model-Comparison-for-GitHub-Pull-Requests")
    
    repo = 
    
      GitRepository with properties:
    
         WorkingFolder: "C:\myWorkSpace\newrepo"
             GitFolder: "C:\myWorkSpace\newrepo\.git"
         CurrentBranch: [1×1 GitBranch]  (main)
            LastCommit: [1×1 GitCommit]  (3fa5e35)
         ModifiedFiles: [0×1 string]
        UntrackedFiles: [0×1 string]
                IsBare: 0
             IsShallow: 0
            IsDetached: 0
            IsWorktree: 0

    To clone a passphrase-protected repository, specify your login information for the Git repository account, for example GitHub®. For more information, see Clone Passphrase-Protected Repository.

    Clone a passphrase-protected repository hosted on GitHub.

    url = "https://github.com/mathworks/Simulink-Model-Comparison-for-GitHub-Pull-Requests";
    secrets = loadenv("github.env");
    repo = gitclone(url,Username=secrets("GITHUB_USER"),Token=secrets("GITHUB_TOKEN"))
    
    repo = 
    
      GitRepository with properties:
    
         WorkingFolder: "C:\myWorkSpace\newrepo"
             GitFolder: "C:\myWorkSpace\newrepo\.git"
         CurrentBranch: [1×1 GitBranch]  (main)
            LastCommit: [1×1 GitCommit]  (3fa5e35)
         ModifiedFiles: [0×1 string]
        UntrackedFiles: [0×1 string]
                IsBare: 0
             IsShallow: 0
            IsDetached: 0
            IsWorktree: 0

    To prevent frequent login prompts when you interact with your remote repository using HTTPS, configure a Git credential manager to remember credentials. For more information, see Install Git Credential Helper.

    Clone a repository in the specified folder.

    mkdir("newrepo");
    url = "https://github.com/mathworks/Simulink-Model-Comparison-for-GitHub-Pull-Requests";
    repo = gitclone(url,"newrepo\")
    
    repo = 
    
      GitRepository with properties:
    
         WorkingFolder: "C:\myWorkSpace\newrepo"
             GitFolder: "C:\myWorkSpace\newrepo\.git"
         CurrentBranch: [1×1 GitBranch]  (main)
            LastCommit: [1×1 GitCommit]  (3fa5e35)
         ModifiedFiles: [0×1 string]
        UntrackedFiles: [0×1 string]
                IsBare: 0
             IsShallow: 0
            IsDetached: 0
            IsWorktree: 0

    Clone only the most recent commit.

    url = "https://github.com/mathworks/Simulink-Model-Comparison-for-GitHub-Pull-Requests";
    repo = gitclone(url,Depth=1);
    

    Verify that the clone is shallow.

    repo.IsShallow
    ans =
    
      logical
    
       1

    Input Arguments

    collapse all

    URL of the remote repository, specified as a string scalar.

    Path of the folder in which the function clones the repository, specified as a character vector or string scalar.

    Name-Value Arguments

    Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Example: gitclone(url,Username="myusername",Token="mypersonaltoken",Depth=10)

    Username for the Git repository account, specified as a character vector or string scalar.

    Data Types: char | string

    Personal access token for the Git repository account, specified as a character vector or string scalar.

    Data Types: char | string

    Depth of the shallow clone, specified as a non-negative integer.

    If you do not specify the depth, gitclone performs a full clone. Otherwise, gitclone clones only the number of commits specified by Depth.

    Data Types: single

    Option to clone Git submodules recursively, specified as a numeric or logical 1 (true) or 0 (false).

    Data Types: logical

    Output Arguments

    collapse all

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

    Version History

    Introduced in R2023b