Git issue - move specific commits from your current branch to a different branch
There are multiple ways to move branches from one branch to another.
Cherry-pick only copies the commit from current branch to target branch rather than moving the commit i.e., the branch is still there in source branch.
# Switch to the target branch git checkout target-branch # Cherry-pick specific commit(s) git cherry-pick <commit-hash>
This is the first way, but as mentioned earlier - this would only copy the commit but not move it.
In order to move the commit, we need to use rebase:
# Move to the source branch git checkout source-branch # Start an interactive rebase git rebase -i HEAD~<N>
Change
pick
todrop
for commits you want to remove from the source branch.Save and exit.
git checkout target-branch git cherry-pick <commit-hash>
This removes the commit from the original branch and moves it to the correct branch.