matlab.git.GitRepository
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");
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");
gitinit
function initializes a
repository in the current folder.Properties
WorkingFolder
— Location of currently checked-out files
string scalar
This property is read-only.
Location of currently checked-out files, returned as a string scalar.
Example: "C:\myWorkSpace\myproject"
GitFolder
— Git folder path
string scalar
This property is read-only.
Path of Git folder .git
, returned as a string
scalar.
Example: "C:\myWorkSpace\myproject\.git"
CurrentBranch
— Current branch
matlab.git.GitBranch
object
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)
LastCommit
— Last commit
matlab.git.GitCommit
object
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)
ModifiedFiles
— List of modified files
string array
This property is read-only.
List of modified files in the current repository, returned as a string array.
Example: [2×1 string]
UntrackedFiles
— List of untracked files
string array
This property is read-only.
List of untracked files in the current working folder, returned as a string array.
Example: [5×1 string]
IsBare
— Bare repository indicator
logical
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.
IsShallow
— Shallow repository indicator
logical
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.
IsDetached
— Detached state indicator
logical
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.
IsWorkTree
— Work tree indicator
logical
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
Create Repository
Track Files
Manage Branches
gitrepo | Create Git repository object |
createBranch | Create new Git branch |
switchBranch | Switch Git branch |
deleteBranch | Delete local Git branch |
merge | Merge Git branch, revision, or tag into current branch |
Update Repository
Revert Changes
gitrepo | Create Git repository object |
discardChanges | Discard changes in Git repository |
Examples
Create Git Repository Object
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
Find Git Repository Commands
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
Examine Git Repository Properties
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
MATLAB 명령
다음 MATLAB 명령에 해당하는 링크를 클릭했습니다.
명령을 실행하려면 MATLAB 명령 창에 입력하십시오. 웹 브라우저는 MATLAB 명령을 지원하지 않습니다.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)