DEV Community

Cover image for Moving a Specific Commit to Another Branch in Git
Emiedonmokumo Dick-Boro
Emiedonmokumo Dick-Boro

Posted on

Moving a Specific Commit to Another Branch in Git

Sometimes you might make a commit on the wrong branch in Git, maybe you were supposed to be on a feature branch but you were still on main. You don’t need to delete or redo your work, Git provides a simple way to move that commit to another branch using a command called git cherry-pick.

Git Cherry Pick

The git cherry-pick command allows you to copy a specific commit from one branch and apply it to another. It doesn’t bring along the entire branch history, just that single commit. For example, if your commit hash is 0e983810d6de98c8710cf2cda2df17d9b05875e7, and you want it to appear in another branch, you first switch to the branch you want the commit to go to using git checkout <branch-name>. If the branch doesn’t exist yet, you can create it using git checkout -b <new-branch>.

After switching, you run git cherry-pick 0e983810d6de98c8710cf2cda2df17d9b05875e7. This command copies the changes from that commit and applies them to your current branch. If there are no conflicts, the commit will appear in the new branch just like it was originally made there. If Git detects any conflicts, it will pause and ask you to fix them before continuing. Once resolved, you can run git add . followed by git cherry-pick --continue to finish applying the commit.

In simple terms, git cherry-pick lets you move specific commits between branches without merging or redoing your work. It’s one of the most useful Git commands when you make a mistake or need to copy a fix to another branch quickly.

Top comments (0)