There are several reasons why you might want to move a commit to a different branch. Let’s take a look at some of them.
Committed to the wrong branch
You’re working on a new feature, but an urgent bug came in. You fixed the bug and committed the fix, but oops… you forgot to create a new branch for the bugfix! Now this bugfix is on the wrong branch. How do we fix this?
![IntelliJ IDEA Git log window showing a bugfix commit on a new-feature branch](https://maritvandijk.com/wp-content/uploads/2023/03/CommitOnWrongBranch-1024x575.png)
Use cherry-pick to move the commit
I could redo the work, especially if it’s a small change, but … I don’t want to! Luckily, there is a better way.
We only want to move this one commit from the feature branch to a separate bugfix branch. We can do this using Git’s “cherry pick” option from IntelliJ IDEA.
First, let’s go back to main and create the bugfix branch that we should have created in the first place.
![IntelliJ IDEA Git log window showing that the main branch is checked out.](https://maritvandijk.com/wp-content/uploads/2023/03/GoBackToMain-1024x575.png)
Once we’re back on the main branch, we can create a new branch named “bugfix”.
![](https://maritvandijk.com/wp-content/uploads/2023/03/CreateBugfixBranch-1024x575.png)
On the newly created branch, we can select the bugfix commit from the other branch and select Cherry-Pick to apply that commit to our current branch.
![IntelliJ IDEA Git log with the bugfix commit selected and the context menu with option cherry-pick selected.](https://maritvandijk.com/wp-content/uploads/2023/03/CherryPickFromMenu-1024x575.png)
Cherry-pick from the command line
Yes, we can do this from the command line too, but there’s no cute cherry icon on the command line. To cherry-pick a commit from the command line, we can use the command git cherry-pick <commit hash>
. We would need to find the commit hash of the commit we want to cherry-pick, which we can find for example in the Commit Details pane in the Git log window (see below).
![IntelliJ IDEA with the terminal open and the command "git cherry-pick 51e33e5a".](https://maritvandijk.com/wp-content/uploads/2023/03/CommandLine-1024x574.png)
As we can see, the bugfix commit is now on the bugfix branch.
![IntelliJ IDEA Git log showing the Bugfix commit on the bugfix branch and a message "Cherry-pick successful".](https://maritvandijk.com/wp-content/uploads/2023/03/BugfixCommitOnBugfixBranch-1024x575.png)
Other use cases for cherry-picking
Cherry picking can be useful in other situations too. Let’s take a look at some other use cases for cherry-picking.
Backporting a fix
We can also use cherry-picking to backport a fix to a previous release branch. For example, let’s move our bugfix commit also to the v1-release.
To do so, first we need to look for the last release (v1). We can search for a specific commit hash, branch or tag name in the Git log (⌘ F on Mac or Ctrl+F on Windows/Linux).
![IntelliJ IDEA Git log showing a pop-up to Enter hash or branch/tag name.](https://maritvandijk.com/wp-content/uploads/2023/03/SearchForHashBranchTag-1024x574.png)
We can also filter commits in the commit log by branch, user, date or path.
![IntelliJ IDEA Git log showing the option to filter by branch, user, date or path](https://maritvandijk.com/wp-content/uploads/2023/03/FilterByBranchUserEtc-1024x574.png)
To see which commits have not yet been applied to this branch, we can click View Options and select Highlight | Not Cherry-Picked Commits. We’ll compare with the new-feature branch. Commits that have already been applied to the current branch are greyed out.
![IntelliJ IDEA Git log showing a context menu with Highlights | Not Cherry-Picked Commits selected](https://maritvandijk.com/wp-content/uploads/2023/03/NotCherryPickedCommits-1024x574.png)
When we select a commit, we can look at the information in the Commit Details area (at the bottom right) to make sure these are the changes we want to transfer to this branch. In the Commit Details area we can see which files were changed in a particular commit. We can right-click a file and select Show Diff from the context menu to open the changes that were made to that file.
![IntelliJ IDEA Git log showing the details of a commit on the right.](https://maritvandijk.com/wp-content/uploads/2023/03/CommitDetails-1024x574.png)
If we are sure these are the changes we want, we can cherry-pick them to the previous release branch.
Cherry pick part of a commit
In the Commit details pane on the right, select the files containing the changes you want to apply to the target branch, right-click and select Cherry-Pick Selected Changes from the context menu.
![IntelliJ IDEA Git log showing a context-menu with the option Cherry-Pick Selected Changes selected](https://maritvandijk.com/wp-content/uploads/2023/03/Cherry-PickSelectedChanges-1024x574.png)
The cherry picked changes are transferred to the change list and we can commit them from there.
![IntelliJ IDEA Commit window with Changes selected to be committed.](https://maritvandijk.com/wp-content/uploads/2023/03/ChangeList-1024x574.png)
Dealing with conflicts
So far, cherry picking went smoothly because there are no conflicting changes. What if there are conflicts?!
When we cherry-pick a commit that has conflicts with our current branch, the Merge Conflicts dialog opens.
![IntelliJ IDEA Merge Conflict dialog with the options to Accept Yours, Accept Theirs or Merge](https://maritvandijk.com/wp-content/uploads/2023/03/MergeConflict-1024x574.png)
We can resolve the merge conflicts here. We want to keep some changes, and reject others.
![IntelliJ IDEA Merge Conflict dialog with conflicts highlighted](https://maritvandijk.com/wp-content/uploads/2023/03/MergeConflictDialog-1024x574.png)
![IntelliJ IDEA Merge Conflict dialog with conflicts resolved and the option to Save changes and finish merging](https://maritvandijk.com/wp-content/uploads/2023/03/MergeConflictResolved-1024x574.png)
If you’re not able to resolve the merge conflicts, you can also abort the cherry pick.
![IntelliJ IDEA Git log showing a notification that the Cherry-pick was performed with conflicts and a popup to Abort the Cherry-Pick](https://maritvandijk.com/wp-content/uploads/2023/03/Abort-1024x574.png)
Continue after cherry-picking
Once we’re done cherry-picking, we can go back to the “feature” branch. Since we haven’t pushed these changes yet, we can remove the commit from the feature branch by selecting Drop commit.
![IntelliJ IDEA Git log with a commit selected and a context-menu with the option to Drop Commit](https://maritvandijk.com/wp-content/uploads/2023/03/DropCommit-1024x574.png)
What if you have pushed the changes already? Then you might want to revert it on this branch instead. Right-click the commit and from the context menu select Revert Commit.
![IntelliJ IDEA Git log with a commit selected and a context-menu with the option to Revert Commit](https://maritvandijk.com/wp-content/uploads/2023/03/RevertCommit-1024x574.png)
Now we can continue working on the new feature!
Conclusion
Moving a commit to a different branch. Not nearly as scary as it sounds! Let the IDE help to turn this into a quick, low-stress task.
Links
- (code) https://github.com/mlvandijk/git_tips
- (JetBrains – IntelliJ IDEA) Cherry-pick separate commits
- (Git documentation) Git cherry-pick
- (blog) Marco Behler: Git: Merge, Cherry-Pick & Rebase
- (blog) IntelliJ IDEA: Resolving Merge Conflicts in Git
- (blog) Foojai.io: Resolving Git Merge Conflicts in IntelliJ IDEA
One thought on “Cherry-pick: Move a commit to a different branch”
Comments are closed.