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
git branch new-branch
git checkout new-branch
git cherry-pick commitSHA
git checkout master
git reset --hard HEAD~1
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