Main Content

commit

Commit changes to Git repository

Since R2023b

    Description

    example

    commitDetails = commit(repo,Message=commitMessage) commits all modified files to the Git™ repository repo with the specified message Message and returns the commit details.

    example

    commitDetails = commit(repo,Message=commitMessage,Files=listOfFiles) commits only the modified files specified by Files to the Git repository.

    Examples

    collapse all

    Open the Times Table App project and use the gitrepo function to create a Git repository object from the currently active repository.

    openExample("matlab/TimesTableProjectExample")
    repo = gitrepo
    repo = 
    
      GitRepository with properties:
    
        WorkingFolder: "C:\myWorkSpace\examples\TimesTableProjectExample"
            GitFolder: "C:\myWorkSpace\examples\TimesTableProjectExample\.git"
        CurrentBranch: [1×1 GitBranch]  (main)
           LastCommit: [1×1 GitCommit]  (566d916)
        ModifiedFiles: [0×1 string]
       UntrackedFiles: [0×1 string]
               IsBare: 0
            IsShallow: 0
           IsDetached: 0
           IsWorktree: 0 

    Create a new file and add it to the repository.

    edit newScript.m;
    add(repo,"newScript.m");

    Modify the timesTableGame file in your repository.

    writelines("% Add simple comment","source/timesTableGame.m",WriteMode="append");

    List the modified files in the repository.

    modifiedFilesList = repo.ModifiedFiles
    
    modifiedFilesList =
    
    2×1 string array
    
        "C:\myWorkSpace\examples\TimesTableProjectExample\newfile.m"
        "C:\myWorkSpace\examples\TimesTableProjectExample\source\timesTableGame.m"
    

    Commit all modified files to the local repository.

    commitDetails = commit(repo,Message="Commit all")
    commitDetails = 
    
      GitCommit with properties:
    
               Message: "Commit all"
                    ID: "a774b7daed41a6efb683d9405fe80a1e0ed82b5e"
            AuthorName: "username"
           AuthorEmail: "username@mathworks.com"
            AuthorDate: 17-Apr-2023 12:17:03 +0000
         CommitterName: "username"
        CommitterEmail: "username@mathworks.com"
         CommitterDate: 17-Apr-2023 12:17:03 +0000
         ParentCommits: "566d9161131ad17ead7bc4842c2d0124c435c77e"

    Open the Times Table App project and use the gitrepo function to create a Git repository object from the currently active repository.

    openExample("matlab/TimesTableProjectExample")
    repo = gitrepo
    repo = 
    
      GitRepository with properties:
    
        WorkingFolder: "C:\myWorkSpace\examples\TimesTableProjectExample"
            GitFolder: "C:\myWorkSpace\examples\TimesTableProjectExample\.git"
        CurrentBranch: [1×1 GitBranch]  (main)
           LastCommit: [1×1 GitCommit]  (566d916)
        ModifiedFiles: [0×1 string]
       UntrackedFiles: [0×1 string]
               IsBare: 0
            IsShallow: 0
           IsDetached: 0
           IsWorktree: 0 

    Create a new file and add it to the repository.

    edit newScript.m;
    add(repo,"newScript.m");

    Modify the timesTableGame file in your repository.

    writelines("% Add simple comment","source/timesTableGame.m",WriteMode="append");

    List the modified files in the repository.

    modifiedFilesList = repo.ModifiedFiles
    
    modifiedFilesList =
    
    2×1 string array
    
        "C:\myWorkSpace\examples\TimesTableProjectExample\newScript.m"
        "C:\myWorkSpace\examples\TimesTableProjectExample\source\timesTableGame.m"
    

    Commit only the newScript file to the local repository.

    commitDetails = commit(repo,Files="newScript.m",Message="Commit new file")
    commitDetails =  = 
    
      GitCommit with properties:
    
               Message: "Commit new file"
                    ID: "a774b7daed41a6efb683d9405fe80a1e0ed82b5e"
            AuthorName: "username"
           AuthorEmail: "username@mathworks.com"
            AuthorDate: 17-Apr-2023 12:17:03 +0000
         CommitterName: "username"
        CommitterEmail: "username@mathworks.com"
         CommitterDate: 17-Apr-2023 12:17:03 +0000
         ParentCommits: "566d9161131ad17ead7bc4842c2d0124c435c77e"

    Input Arguments

    collapse all

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

    Commit message, specified as a string scalar or a character vector.

    Example: "Fix bug"

    Data Types: char | string

    Files to commit to the local repository, specified as a string array, character vector, or cell array of character vectors. If you do not specify this input, the function commits all modified files.

    Example: "newfile.m"

    Data Types: char | string | cell

    Output Arguments

    collapse all

    Details of the commit, returned as a matlab.git.GitCommit object.

    Version History

    Introduced in R2023b