Main Content

gitinit

Initialize empty Git repository

Since R2023b

    Description

    repo = gitinit initializes an empty Git™ repository in the current folder and returns a matlab.git.GitRepository object. The gitinit function also creates .gitignore and .gitattributes files. The .gitattributes file contains the list of common binary files to register and protect from corruption. For more information, see Register Binary Files with Git.

    example

    repo = gitinit(folder) initializes an empty Git repository in the specified folder and returns a matlab.git.GitRepository object.

    example

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

    example

    Examples

    collapse all

    Create an empty repository in the current folder. After you create the repository, you can add and commit files to it.

    repo = gitinit
    repo = 
    
      GitRepository with properties:
    
         WorkingFolder: "C:\workSpace\newrepo"
             GitFolder: "C:\workSpace\newrepo\.git"
         CurrentBranch: [0×0 GitBranch]
            LastCommit: [0×0 GitCommit]
         ModifiedFiles: [0×1 string]
        UntrackedFiles: [2×1 string]
                IsBare: 0
             IsShallow: 0
            IsDetached: 0
            IsWorktree: 0

    Create an empty repository in the specified folder. After you create the repository, you can add and commit files to it.

    mkdir("newrepo");
    repo = gitinit("newrepo\")
    repo = 
    
      GitRepository with properties:
    
         WorkingFolder: "C:\workSpace\newrepo"
             GitFolder: "C:\workSpace\newrepo\.git"
         CurrentBranch: [0×0 GitBranch]
            LastCommit: [0×0 GitCommit]
         ModifiedFiles: [0×1 string]
        UntrackedFiles: [2×1 string]
                IsBare: 0
             IsShallow: 0
            IsDetached: 0
            IsWorktree: 0

    Create a bare repository in the current folder.

    repo = gitinit(pwd,Bare=true)
    repo = 
    
      GitRepository with properties:
    
         WorkingFolder: "C:\workSpace\newfolder2"
             GitFolder: "C:\workSpace\newfolder2\.git"
         CurrentBranch: [0×0 GitBranch]
            LastCommit: [0×0 GitCommit]
         ModifiedFiles: [0×1 string]
        UntrackedFiles: [5×1 string]
                IsBare: 1
             IsShallow: 0
            IsDetached: 0
            IsWorktree: 0

    Input Arguments

    collapse all

    Path of the folder in which the function initializes a 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: gitinit(pwd,Bare=true)

    Name of the initial branch, specified as a string scalar or a character vector. If you do not specify this input, the function uses the value of init.defaultbranch in your global .gitconfig file. If init.defaultbranch is empty, the gitinit function names the initial branch main.

    To check the content of the global .giticonfig file, in MATLAB®, enter:

    !git config --global --list
    init.defaultbranch=trunk
    merge.mlAutoMerge.driver="C:/Program Files/MATLAB/R2023b/bin/win64/mlAutoMerge.bat" %O %A %B %A
    mergetool.mlMerge.cmd="C:/Program Files/MATLAB/R2023b/bin/win64/mlMerge.exe" $BASE $LOCAL $REMOTE $MERGED
    difftool.mlDiff.cmd="C:/Program Files/MATLAB/R2023b/bin/win64/mlDiff.exe" $LOCAL $REMOTE
    credential.https://insidelabs-git.mathworks.com.provider=generic
    user.name=myname
    user.email=myname@mathworks.com
    core.longpaths=true

    Example: "trunk"

    Data Types: char | string

    Option to create a bare Git repository, specified as a numeric or logical 1 (true) or 0 (false).

    A bare Git repository is commonly used as a remote repository where code is shared between members of the team. Bare Git repositories are not used for local development. You cannot commit to bare repositories, but you can pull and push changes.

    Data Types: logical

    Option to reinitialize a Git repository, specified as a numeric or logical 1 (true) or 0 (false).

    Reinitializing a Git repository copies new templates to the .git folder and creates .gitignore and .gitattributes files if they do not exist.

    Data Types: logical

    Output Arguments

    collapse all

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

    Version History

    Introduced in R2023b