Main Content

Branch and Merge Files with Git

Create a Branch

  1. In a project using Git™ source control, click Branches on the Project tab. The Branches dialog box appears, where you can view, switch, create, and merge branches.

    Tip

    You can inspect information about each commit node. Select a node in the Branch Browser diagram to view the author, date, commit message, and changed files.

    The Branches pane in this figure shows an example branch history.

  2. Select a source for the new branch. Click a node in the Branch Browser diagram, or enter a unique identifier in the Source text box. You can enter a tag, branch name, or a unique prefix of the SHA1 hash (for example, 73c637 to identify a specific commit). Leave the default to create a branch from the head of the current branch.

  3. Enter a name in the Branch name text box and click Create.

  4. To work on the files on your new branch, switch your project to the branch.

    In the Branches drop-down list, select the branch you want to switch to and click Switch.

  5. Close the Branches dialog box to return to the project and work on the files on your branch.

For next steps, see Pull, Push, and Fetch Files with Git.

Switch Branch

  1. In a project, click Branches.

  2. In the Branches dialog box, select the branch you want to switch to in the Branches list and click Switch.

  3. Close the Branches dialog box to return to the project and work on the files on the selected branch.

Compare Branches and Save Copies

From within your Git repository folder, right-click the white space of the Current Folder browser and select Source Control > Branches.

  • To examine differences in a file between the current revision and its parent, right-click a file in the tree under Differences from parent and select Show Difference.

  • To examine differences in a file between any two revisions including revisions on two different development branches, hold the Ctrl key and select the two different revisions. Right-click a file in the tree under Differences from selection and select Show Difference.

MATLAB® opens a comparison report. You can save a copy of the selected file on either revision. Right-click a file and select Save As to save a copy of the file on the selected revision. Select Save Original As to save a copy of the file on the prior revision. This is useful if you want to test how the code ran in previous revisions or on other branches.

Merge Branches

Before you can merge branches, you must register model files as binary to prevent Git from inserting conflict markers. See Register Model Files with Source Control Tools.

Tip

After you use Fetch, you must merge. See Pull, Fetch, and Merge.

To merge any branches:

  1. In a project, click Branches.

  2. In the Branches dialog box, from the Branches drop-down list, select a branch you want to merge into the current branch, and click Merge.

  3. Close the Branches dialog box to return to the project and work on the files on the current branch.

If the branch merge causes a conflict that Git cannot resolve automatically, an error dialog box reports that automatic merge failed. The Branch status in the Git pane displays MERGING. Resolve the conflicts before proceeding.

Caution

Do not move or delete files outside of MATLAB because this can cause errors on merge.

Keep Your Version

  1. To keep your version of the file, right-click the file and select Mark Conflict Resolved. The Branch status in Git pane displays MERGE_RESOLVED. The Modified Files list is empty, because you have not changed any file contents. The local repository index version and your branch version are identical.

  2. Click Commit to commit your change that marks the conflict resolved.

View Conflicts in Branch Versions

If you merge a branch and there is a conflict in a model file, Git marks the file as conflicted and does not modify the contents. Right-click the file and select View Conflicts. The project opens a comparison report showing the differences between the file on your branch and the branch you want to merge into. Decide how to resolve the conflict. See Resolve Conflicts.

Revert to Head

To remove all local changes, in the Branches dialog box, click Revert to Head.

Delete Branches

  1. In the Branches dialog box, from the Branches drop-down list, select a branch you want to delete. You cannot delete the current branch.

  2. On the far right, click the down arrow and select Delete Branch.

    Caution

    You cannot undo deleting a branch.

See Also

Functions

Related Examples

More About