Git – move accidental commits to a different branch

If you’ve been working on a common staging branch when you thought you were working on a personal feature branch, you can move your work by doing the following:

git checkout feature_branch
git cherry-pick SHA (Do this for each commit on the mistaken branch)
git checkout staging
git reset –hard HEAD~n (where n = the number of mistakenly committed commits)

This assumes you haven’t already pushed your changes to a common repository.

If you have modified files uncommitted on the staging branch, commit them, cherry pick them into your feature branch, then reset staging as above.

Leave a Reply

Your email address will not be published. Required fields are marked *