Main Content

matlab.git.GitRepository

Git repository object

Since R2023b

Description

The matlab.git.GitRepository object represents the currently active local Git™ repository. Use the Git repository object to programmatically interact with your local repository.

Creation

To create a matlab.git.GitRepository object for a specified repository, use the gitrepo function. For example:

repo = gitrepo("C:\workSpace\myrepofolder");
If you do not specify a path, the gitrepo function creates an object for the repository in the current folder.

To clone a remote Git repository, use the gitclone function. For example:

repo = gitclone("https://github.com/mathworks/examplerepo.git");

To initialize a Git repository in a folder, use the gitinit function. For example:

repo = gitinit("C:\workSpace\myrepofolder");
If you do not specify a path, the gitinit function initializes a repository in the current folder.

Properties

expand all

This property is read-only.

Location of currently checked-out files, returned as a string scalar.

Example: "C:\myWorkSpace\myproject"

This property is read-only.

Path of Git folder .git, returned as a string scalar.

Example: "C:\myWorkSpace\myproject\.git"

This property is read-only.

Current branch, returned as a matlab.git.GitBranch object. Use the matlab.project.GitBranch object to query the currently checked-out branch.

Example: [1×1 GitBranch] (main)

This property is read-only.

Last commit, returned as a matlab.git.GitCommit object. Use the matlab.git.GitCommit object to query the most recent commit.

Example: [1×1 GitCommit] (566d916)

This property is read-only.

List of modified files in the current repository, returned as a string array.

Example: [2×1 string]

This property is read-only.

List of untracked files in the current working folder, returned as a string array.

Example: [5×1 string]

This property is read-only.

Bare repository indicator, returned as 1 or 0. A value of 1 indicates that the repository is a bare repository.

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.

This property is read-only.

Shallow repository indicator, returned as 1 or 0. A value of 1 indicates that the repository is a shallow repository.

This property is read-only.

Detached state indicator, returned as 1 or 0. A value of 1 indicates that the repository is in a detached state.

This property is read-only.

Work tree indicator, returned as a 1 or 0. A value of 1 indicates that the repository is a work tree.

Object Functions

expand all

gitcloneClone Git repository
gitrepoCreate Git repository object
gitinitInitialize empty Git repository
gitrepoCreate Git repository object
addMark new file for addition to Git repository
rmMark file for deletion from Git repository
status Display status of files in working folder and staging area in Git repository
logDisplay record of commits in Git repository
gitrepoCreate Git repository object
createBranchCreate new Git branch
switchBranchSwitch Git branch
deleteBranchDelete local Git branch
mergeMerge Git branch, revision, or tag into current branch
gitrepoCreate Git repository object
fetchDownload new data from remote Git repository
mergeMerge Git branch, revision, or tag into current branch
pullDownload and merge new data from remote Git repository into local repository
gitrepoCreate Git repository object
discardChangesDiscard changes in Git repository
gitrepoCreate Git repository object
commitCommit changes to Git repository
pushPublish local changes to remote Git repository

Examples

collapse all

Open the Times Table App project and use the gitrepo function to create a Git repository object that you can manipulate programmatically.

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: [2×1 string]
   UntrackedFiles: [5×1 string]
           IsBare: 0
        IsShallow: 0
       IsDetached: 0
       IsWorktree: 0 

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;

Find the functions that you can execute on the project object.

methods(repo)
Methods for class matlab.git.GitRepository:

GitRepository  commit         deleteBranch    fetch       merge      push      status      
add            createBranch   discardChanges  log         pull       rm        switchBranch
                                      

Create a Git repository object and examine its properties.

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 

Examine the currently checked-out branch.

checkedoutbranch = repo.CurrentBranch
checkedoutbranch = 

  GitBranch with properties:

          Name: "main"
    LastCommit: [1×1 GitCommit]  (566d916)

Examine the last commit on the current branch.

latestcommit = checkedoutbranch.LastCommit
latestcommit = 

  GitCommit with properties:

           Message: "Initial check-in"
                ID: "566d9161131ad17ead7bc4842c2d0124c435c77e"
        AuthorName: "username"
       AuthorEmail: "username@mathworks.com"
        AuthorDate: 29-Mar-2023 11:45:23 UTC
     CommitterName: "username"
    CommitterEmail: "username@mathworks.com"
     CommitterDate: 29-Mar-2023 11:45:23 UTC
     ParentCommits: [0×1 string]

List the modified files in your repository.

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

modifiedfileslist = repo.ModifiedFiles
modifiedfileslist =

1×1 string array

    "C:\myWorkSpace\examples\TimesTableProjectExample\source\timesTableGame.m"

Commit modified files to the local repository.

commit(repo,Files=modifiedfileslist,Message="Adding new file")
ans = 

  GitCommit with properties:

           Message: "Adding 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"

Version History

Introduced in R2023b