Apply changes from one branch to another

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:

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:

When you run merge, the changes from your feature branch are integrated into the HEAD of the target branch: 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.

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:

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, PyCharm will suggest performing Smart merge. If you select this option, PyCharm 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:

While you develop in your branch, your teammates continue to commit their work to 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:

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:

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.

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

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 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 on the toolbar).

Select the required commit. Use the information in the Commit Details area if necessary.

Click the Cherry-pick button on the toolbar. PyCharm 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.

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.