Main Content

Use Git Hooks in MATLAB

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 Install Command-Line Git Client.

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.

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 Current Folder browser, right-click and select Source Control > View and Commit Changes. In the View and Commit Changes dialog box, the default commit message includes -PROJ123- at the end.

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

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 View and Commit Changes dialog box, in the Comment field, enter "Add saveUnsavedFiles utility" and click Commit.

Error dialog forwarding the error from the commit-msg hook

Alternatively, commit your changes programmatically using the commit function.

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:'

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.

View and Commit Changes dialog box that includes the list of modified files, the comment field showing the commit message that starts with "feat:", and Commit and Cancel buttonsError dialog forwarding the error from the pre-commit hook

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

To use Git hooks to improve other workflows, such as the merge and push workflows, add more hooks in the .git/hooks folder. 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

Related Topics