commit - Commit changes to Git repository - MATLAB (original) (raw)

Main Content

Commit changes to Git repository

Since R2023b

Syntax

Description

[commitDetails](#mw%5F6e1e7113-0af2-4eb7-ac10-0f9dd48a7b28) = commit([repo](#mw%5Fc1b0e73e-144c-4dd0-90a4-703ee70da2bd),Message=[commitMessage](#mw%5Fe032819a-9eb3-4622-853f-33673ce72d96)) commits all modified files to the Git™ repository repo with the specified messageMessage and returns the commit details.

example

[commitDetails](#mw%5F6e1e7113-0af2-4eb7-ac10-0f9dd48a7b28) = commit([repo](#mw%5Fc1b0e73e-144c-4dd0-90a4-703ee70da2bd),Message=[commitMessage](#mw%5Fe032819a-9eb3-4622-853f-33673ce72d96),Files=[listOfFiles](#mw%5Faf4d2668-0f6b-46c7-85cf-f262af313c50)) commits only the modified files specified by Files to the Git repository.

example

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)
      Remotes: [1×1 GitRemote]
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)
      Remotes: [1×1 GitRemote]
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

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