Main Content

merge

Merge Git branch, revision, or tag into current branch

Since R2023b

    Description

    example

    merge(repo,commitIdentifier) merges the branch, commit, or tag specified by commitIdentifier into the current branch of the Git™ repository repo.

    When you attempt to merge a branch that results in file conflicts, the merge function reports that the merge is incomplete and lists the conflicted files. To complete the merge, resolve and commit the conflicted files.

    Examples

    collapse all

    Navigate to your repository folder and create a repository object.

    repo = gitrepo;

    Query the currently checked-out branch.

    branchToMergeObj = repo.CurrentBranch
    branchToMergeObj = 
    
      GitBranch with properties:
    
              Name: "newFeature"
        LastCommit: [1×1 GitCommit]  (4a00f8a)

    Switch to the branch into which you want to merge the newFeature branch.

    switchBranch(repo,"main");

    Merge the newFeature branch into the main branch.

    merge(repo,"newFeature")

    Alternatively, use the branch object to perform the merge.

    merge(repo,branchToMergeObj)

    Navigate to your repository folder and create a repository object.

    repo = gitrepo;

    Query the last commit on the current checked-out branch.

    commitToMergeObj = repo.LastCommit
    commitToMergeObj = 
    
      GitCommit with properties:
    
               Message: "Add test for new feature"
                    ID: "4a00f8abca08f3f6feea94e3fb6a2f65ba48a232"
            AuthorName: "username"
           AuthorEmail: "username@mathworks.com"
            AuthorDate: 15-Mar-2023 19:54:48 +0000
         CommitterName: "username"
        CommitterEmail: "username@mathworks.com"
         CommitterDate: 15-Mar-2023 19:54:48 +0000
         ParentCommits: [1×1 string]

    Switch to the branch into which you want to merge the newFeature branch.

    switchBranch(repo,"main");

    Use the commit revision specifier (ID) to merge the newFeature branch into the main branch.

    commitID = repo.commitToMergeObj.ID;
    merge(repo,commitID)

    Alternatively, use the commit object to merge.

    merge(repo,commitToMergeObj)

    Input Arguments

    collapse all

    Git repository, specified as a matlab.git.GitRepository object.

    Revision specifiers, tag, or branch to merge into the current branch, specified as a string scalar, character vector, matlab.git.GitBranch object, or matlab.git.GitCommit object.

    Commit IDs support short, full, and relative commits.

    Example: "08a4c49", "08a4c49d249a4dc3d998b473cdda186f1c05dfd0", "myBranch"

    Data Types: char | string

    Tips

    When you perform a merge, you can use the add or rm functions to mark the conflict on a file as resolved.

    Version History

    Introduced in R2023b