CLion 2017.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).

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 diverge from master diagram

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

merge result diagram
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.

To merge branches, do the following:

  1. Switch to the target branch that you want to integrate the changes to.
  2. Invoke the branches popup, and select the branch that you want to merge into the target branch.
  3. Choose Merge from the submenu.

If your working tree is clean (i.e. 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:

merge commit

If conflicts occur between your branch and the target branch, you will be prompted to resolve them (see Resolve conflicts for details).

If you have local changes that will be overwritten by merge, CLion will suggest performing Smart merge. If you select this option, CLion 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:

feature branch diagram

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

feature branch diverge from master diagram

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 result diagram

To rebase the current branch on top of another branch

To rebase the branch that is currently checked out on top of another branch, do the following:
  1. Invoke the branches popup and select the branch the you want to rebase the current branch onto.
  2. Choose Rebase onto from the list of available actions.

To rebase a branch on top of the current branch

To rebase a branch on top of the branch that is currently checked out, do the following:

  1. Invoke the branches popup and select the branch that you want to rebase on top of the current branch.
  2. Choose Checkout with Rebase from the list of available actions.

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 view:


Apply changes from a specific commit to another branch (cherry-pick)

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, etc.

CLion allows you to do so by using the Cherry-pick action:

  1. Switch to the target branch that you want to integrate the changes to.
  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.

    Note that the log lists all commits. To reduce the number of items in the list, you can filter commits by branch, user or date. You can also click the 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 view, or click find on the toolbar).

  4. Select the required commit. Use the information in the Commit Details area if necessary.
  5. Click the Cherry-pick button cherryPickButton.png on the toolbar. CLion 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. You can review these changes and commit them later if necessary.

Last modified: 15 December 2017