Move some commits to another branch - git cherry pick

In some cases developers mistakenly do commits in wrong branch after several commits they realize that they were commiting  in wrong branch there are several ways to move commits into another branch please review the following article in detail. We are going to tell you all possible ways to move commits in to another branch 

Git Cherry Pick:

git cherry-pick is a powerful command that enables arbitrary Git commits to be picked by reference and appended to the current working HEAD. So you only need to create a new branch and cherry pick all your commits one by one using commit SHA id.


Note: You must keep order correct of your commits like first commit should be first cherry picked 

  1. Follow the following steps to move commit from one branch to another
  2. create a new branch using git branch new-branch
  3. checkout new branch using git checkout new-branch
  4. Pick whole commit code from another branch using cherry-pick git cherry-pick commitSHA
  5. checkout old branch from where you want to remove last commit using git checkout master
  6. remove last commit from a branch using git  git reset --hard HEAD~1
  7. Again checkout new branch and push your changes  using git push
     

Copy and past whole commands just replace with your branch names and commit HASH ID.

git branch new-branch
git checkout new-branch
git cherry-pick 3a3b3ed6
git checkout master
git reset --hard HEAD~1
git checkout new-branch
git push --set-upstream origin new-branch 

 

Moving to an existing branch:
If you want to move your commits to an existing branch, follow the following steps you can also follow above steps 


git checkout new-branch
git merge master
git checkout master
git reset --hard HEAD~1 # delete last commit.
git checkout new-branch

Tags:

git

Share:

Related posts