Use Git Hooks in MATLAB - MATLAB & Simulink (original) (raw)

Main Content

This example shows how to use Git™ hooks in MATLAB® to standardize your commit workflows. Git hooks are custom scripts that can be triggered by operations such as committing, merging, and pushing commits.

To use Git hooks in MATLAB on a Windows® system, make sure you enable .sh files to run with Bash when you install command-line Git. For more information, see Additional Setup.

Open the example to download the supporting files. This repository includes three example hooks. The .git/hooks folder contains the commit-msg, the pre-commit, and the prepare-commit-msg hooks. Explore and edit the content of these example hooks or use them as templates to create your own hooks. If you do not see the .git folder, adjust your settings to show hidden files and folders. For more information, see Current Folder Settings.

Repository folder hierarchy showing three hooks in the .git/hooks folder

Customize Commit Message

Use the prepare-commit-msg hook to modify the default commit message and customize it to include information such as branch names and issue tracker IDs. In this example, prepare-commit-msg appends the project name -PROJ123- to your commit message.

In the Files panel, right-click and select Source Control > Commit. In the Commit Changes dialog box, the default commit message includes -PROJ123- at the end.

Alternatively, click Commit in the Source Control Source Control panel icon panel. If the Source Control icon is not in a sidebar, click the Open more panels button Open more panels icon and select the Source Control panel.

Files browser showing a context menu with the Source Control > Commit option selectedCommit Changes dialog box that includes the list of modified files, a comment field that includes "-PROJ123-", and the Commit and Cancel buttons

Inspect Files Before Committing

Use the pre-commit hook to inspect the files you are about to commit. You can customize pre-commit to check for code quality, formatting issues, and typos. pre-commit cancels the commit if the checks you specify in the hook fail. In this example, pre-commit checks for the text "TODO" in your modified files.

In this example, the saveUnsavedMFiles.m file contains "TODO" comments. When you attempt to commit your changes, the commit operation throws the error you specify in the pre-commit hook.

Error dialog forwarding the error from the pre-commit hook

Open the saveUnsavedMFiles.m file and delete occurrences of "TODO". Then, commit again.

Validate Final Commit Message

Use the commit-msg hook to inspect and validate the final commit message. commit-msg cancels the commit if the final commit message does not adhere to the guidelines you enforce. In this example, commit-msg requires every commit message to start with "fix:", "feat:", "doc:", or "test:" to specify the type of the commit.

If you attempt to commit without including the commit type at the beginning of the commit message, the commit operation throws the error you specify in the commit-msg hook. In the Commit Changes dialog box, enter the commit message "Add saveUnsavedFiles utility" and click Commit.

Error dialog forwarding the error from the commit-msg hook

Alternatively, you can commit your changes programmatically using the commit function.

localrepo = gitrepo(path/to/repo/folder) commit(localrepo, Message="Add saveUnsavedFiles utility"); Error using matlab.git.GitRepository/commit>i_doCommit The 'commit-msg' hook aborted the operation.

Caused by: ERROR: Commit message does not start with 'fix:', 'feat:', 'doc:', or 'test:'

To use Git hooks to improve other workflows, such as the merge and push workflows, add more hooks in the .git/hooks folder. Starting in R2024a, MATLAB Git integration supports these hooks: pre-commit, commit-msg, post-commit, prepare-commit-msg, pre-push, pre-merge-commit, post-checkout, and post-merge.

See Also

commit

Topics