Squash Git Commits in MATLAB - MATLAB & Simulink (original) (raw)

Main Content

This example shows how to combine multiple Git™ commits into a single commit in MATLAB®.

Open the example to download the supporting files. The example repository reflects the following scenario.

You have been working locally on a utility script saveUnsavedMFiles.m that saves all opened files that have unsaved changes in the MATLAB Editor. You made several commits to track your incremental work. Before you share the utility script, you want to combine the incremental commits into a single commit that represents the feature.

Investigate Commit History

To see the commit history, open the Branch Manager. In the Files panel, right-click and select Source Control > Branch Manager.

Files panel showing a Git repository. A context menu shows the Source Control > Branch Manager option.

Alternatively, you can open the Branch Manager Branch Manager icon from the Source Control panel Source Control side panel icon. in the sidebar. 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.

Source Control panel with mouse cursor pointing to Branch Manager button

The Branch Manager shows the commit graph, branch information, and commit messages and details. In this example, you have three consecutive commits that track incremental changes to the same saveUnsavedMFiles utility file.

You can investigate the difference between the commits in the pane on the right.

Branch Manager tool. The Branch Manager includes a toolstrip at the top, a list of branches and tags on the left, the commit graph and details in the center, and author details and commit differences on the right.

Squash Commits

To squash the commits that track the incremental changes into a single commit that represents the feature, in the commit graph, select the first and last incremental commits you want to combine. Then, right-click and select Squash Selected Commits. The Branch Manager takes all commits between the two commits you selected and combines their commit messages.

Commit graph showing two selected commits and a context menu with the Squash Selected Commits option

In the Squash Commits dialog box, replace the combined commit messages with a new message that describes your feature. Then, click Squash.

Squash Commits dialog box with a field to enter a commit message and Squash and Cancel buttons

The commit graph now shows one combined commit instead of the three separate commits. The pane on the right shows that the difference between the selected commit and the previous commit is the addition of saveUnsavedMFiles.m.

Commit graph showing the two remaining commits. The saveUnsavedMFiles.m file is marked as added in the Differences pane on the right.

See Also

Tools

Topics