GoLand 2019.3 Help

Apply changes from one branch to another

In Git, there are several ways to integrate changes from one branch into another: Merge branches, Rebase branches, or Apply separate commits from one branch to another (cherry-pick).

In GoLand, all operations with branches are performed in the Git Branches popup. To invoke it, click the Git widget in the Status bar or press Ctrl+Shift+`.

Merge branches

Suppose you have created a feature branch to work on a specific task, and want to integrate the results of your work into the main code base after you have completed and tested your feature:

feature branch diagram

Merging your branch into master is the most common way to do this.

It is very common that while you are working in your feature branch, your teammates continue to commit their work to master:

feature branch diverged from master

When you run merge, the changes from your feature branch are integrated into the HEAD of the target branch:

merge result
Git creates a new commit (M) that is referred to as a merge commit that results from combining the changes from your feature branch and master from the point where the two branches diverged.

Merge branches

  1. In the Branches popup select the target branch that you want to integrate the changes to and choose Checkout from the popup menu to switch to that branch.

  2. Click the Branches popup at the bottom of the GoLand window, select the branch that you want to merge into the target branch and choose Merge into Current from the submenu.

If your working tree is clean (which means you have no uncommitted changes), and no conflicts occur between your feature branch and the target branch, Git will merge the two branches, and the merge commit will appear in the Log tab of the Version Control tool window Alt+9:

merge commit

If conflicts occur between your branch and the target branch, you will be prompted to resolve them (see Resolve conflicts). If there are unresolved conflicts left after a merge, the Merge Conflicts node will appear in the corresponding changelist in the Local Changes tab of the Version Control tool window Alt+9 with a link to resolve them.

If you have local changes that will be overwritten by merge, GoLand will suggest performing Smart merge. If you select this option, GoLand will stash uncommitted changes, perform merge, and then unstash the changes.

Rebase branches

When you rebase a branch onto another branch, you apply the commits from the first branch on top of the HEAD commit in the second branch instead of merging them into the target branch.

Suppose you have created a feature branch to work on a specific task and make several commits to that branch:

featuer branch

While you develop in your branch, your teammates continue to commit their work to master:

feature branch diverged from master

When you perform the rebase operation you integrate changes you have done in your feature branch to the master branch by applying your commits on top of the current HEAD commit in master:

rebase operation result

Rebase the current branch on top of another branch

  1. In the Branches popup, select the branch that you want to rebase the current branch onto.

  2. Choose Rebase Current onto Selected from the popup menu.

Rebase a branch on top of the current branch

  1. In the Branches popup, select the branch that you want to rebase on top of the current branch.

  2. Choose Checkout and Rebase onto Current from the popup menu.

For details on how to skip or squash commit during a rebase, refer to Edit project history by performing interactive rebase.

Watch this video to see how a merge or a rebase operation are reflected in the Log tab of the Version Control tool window Alt+9:


Cherry-pick separate commits

Sometimes you only need to apply a single commit to a different branch instead of rebasing or merging an entire branch. This may be useful, for example, if you are working in a feature branch and want to integrate a hotfix from master that was committed after the two branches have diverged. Or you may want to backport a fix to a previous release branch. You can do so by using the Cherry-pick action.

The status of a cherry pick operation is displayed in the status bar. You can always abort an ongoing cherry-pick by selecting Abort Cherry-Pick in the Git Branches popup.

Cherry pick operation status

Apply a commit to another branch

  1. In the Branches popup select the target branch that you want to integrate the changes to and choose Checkout from the popup menu to switch to that branch.

  2. Open the Version Control tool window Alt+9 and switch to the Log tab.

  3. Locate the commit containing the changes you want to cherry pick.

    You can filter commits by branch, user or date. You can also click eye icon on the toolbar and select Highlight | Non-Picked Commits button Non-Picked Commits to grey out the commits that have already been applied to the current branch. If you know the commit hash, or are looking for a tagged commit, you can also use the Go to Hash / Branch / Tag action (press Ctrl+F in the Log tab of the Version Control tool window Alt+9, or click Search on the toolbar).

  4. Select the required commit. Use the information in the Commit Details area to make sure these are the changes you want to transfer to another branch.

  5. Click Cherry-pick the Cherry-Pick button on the toolbar. GoLand will display the Commit Changes dialog with the automatically generated commit message. If you want to review the changes or even modify the code before committing it to the target branch, you can do so in the difference viewer available from this dialog.

  6. When done, click Commit to cherry-pick the selected changes.

    Note that if you click Cancel, a separate changelist will be created with the selected changes that you can see in the Local Changes tab of the Version Control tool window Alt+9. You can review these changes and commit them later if necessary.

  7. Push the changes to the target branch.

Apply separate changes

Imagine you've made some changes to a file that you want to apply to a different branch, but these changes were committed together with other modified files. GoLand lets you apply separate changes instead of cherry-picking an entire commit.

  1. In the Branches popup select the target branch that you want to integrate the changes to and choose Checkout from the popup menu to switch to that branch.

  2. Open the Version Control tool window Alt+9 and switch to the Log tab.

  3. Locate the commit that contains the changes that you want to apply.

    You can filter commits by branch, user or date. You can also click eye icon on the toolbar and select Highlight | Non-Picked Commits button Non-Picked Commits to grey out the commits that have already been applied to the current branch. If you know the commit hash, or are looking for a tagged commit, you can also use the Go to Hash / Branch / Tag action (press Ctrl+F in the Log tab of the Version Control tool window Alt+9, or click Search on the toolbar).

  4. In the Commit Details pane on the right, select the files containing the changes you want to apply to the target branch and select Apply Selected Changes from the context menu.

  5. In the dialog that opens, select an existing changelist or enter the name for a new changelist and click OK.

  6. Commit the changes and then push them to the target branch.

Apply separate files

In addition to applying separate changes to a single file, you can copy an entire file's contents to a different branch. This may be useful, for example, if the file you want to apply doesn't exist in the target branch, or if changes to it were made within several commits.

  1. Check out the branch to which the changes will be applied.

  2. In the Branches popup, click the branch that contains the file you want to apply and select Compare with Current.

  3. Switch to the Files tab, select the file that you want to apply to the current branch, and select Get from Branch from the context menu.

  4. Commit and push the changes. GoLand will copy the entire contents of the file to the current branch.

Last modified: 23 March 2020