Rebase Git Branch in MATLAB - MATLAB & Simulink (original) (raw)

This example shows how to create a linear commit history using Git™ rebase in MATLAB®.

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

You have been collaborating with a team to add utility files to your shared repository. You created the Feature branch from main to add the cleanup utility. While working on the Feature branch, you discover a bug and create the BugFix branch from the Feature branch to investigate and fix the bug. To make the bug fix available to everyone, you want to merge BugFix with main. Since, your Feature branch is not ready to merge yet, you decide to make the bug fix available by rebasing BugFix onto main.

Investigate Commit History

To see the commits history, open the Branch Manager. In the Source Control panel Source Control side panel icon, click the Branch Manager button Branch Manager icon. If the Source Control icon is not in the sidebar, click the Open more panels button Open more panels icon and select the Source Control panel.

The Files and Source Control panels are grouped together.The mouse cursor points to the Branch Manager button in the Source Control panel.

Alternatively, in the Files panel, right-click and select Source Control > Branch Manager.

The Branch Manager shows the commit graph, branch information, and commit messages and details. This example includes three branches, main, Feature, and BugFix. The current branch is BugFix.

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

The Branch Manager tool shows a toolstrip on the top, a list of branches on the left, a commit graph and other details in the center, and commit details on the right.

Rebase Branch

To merge the BugFix branch with the main branch without merging the Feature branch, you can transfer the changes from the BugFix to the main branch using Git rebase.

Rebasing creates patches from the point BugFix branch diverged from the Feature branch, and places the patches in the BugFix branch as if they were based directly off the main branch.

This animation shows rebasing the BugFix branch onto the main branch.

An animated diagram shows the rebasing of the BugFix branch onto the main branch.

Tip: Before you rebase, you can combine commits into a single commit using Git squash. For an example, see Squash Git Commits in MATLAB.

To rebase the BugFix branch onto the main branch, follow these steps.

1. In the Branch Manager toolstrip, in the Changes section, click Rebase.

2. In the Rebase Current Branch dialog box, select the main branch and click Rebase.

The Rebase Current Branch dialog box shows the list of available branches and the Rebase button.

If a conflict occurs during the rebasing process, you see a dialog box to resolve the conflict. For information on how to resolve conflicts, see Resolve Git Conflicts.

You can also discard the rebasing process and restore to the state of your repository before the rebase. In the Branch Manager toolstrip, click Discard Rebase.

An error dialog box lists files that have conflicts.

After the rebasing process is complete, the commit graph shows that the main branch is behind the BugFix branch.

The commit graph shows that the main branch is behind the BugFix branch.

Fast-Forward Merge

To include the changes from the BugFix branch into the main branch, fast-forward merge the main branch. This animation shows a fast-forward merge.

Fast-forward merge illustration.

To fast-forward merge the main branch, follow these steps:

1. Switch to the main branch. In the Branch Manager toolstrip, in the Current Branch section, click the down arrow and select main.

2. In the Changes section, click Merge. In the Merge into Current Branch dialog box, select the Branch option. Then, choose BugFix and click Merge.

Merge into Current Branch dialog with the Branch option selected and the BugFix highlighted.

You see a confirmation that the fast-forward merge is successful.

Confirmation of succesful fast-forward merge

The commit graph in the Branch Manager reflects that the main branch moved forward.

The commit graph shows that the main branch moved forward.

You successfully rebase-merged the bug fix with the main branch. You can now delete the BugFix branch. In the left pane in the Branch Manager, in the Branches section, right-click BugFix and select Delete Branch.

The Branches pane shows the list of branches. The cursor points to the Delete Branch option in the context menu.

See Also

Tools

Topics