Git issue - move specific commits from your current branch to a different branch

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.

  1. 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.

  2. 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 to drop 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.