git-move-commits-to-another-branch

Git: Move commits to another branch

2023-08-10

I've been make mistake when i working in some project before. I forgot to switch the branch into development and i working in staging branch where it protected branch. When i want to push to the remote origin i got error message “Protected branch”, and i want to move my commits that i worked in staging branch to the development branch. Here I will share it ho i do that.


Once i in staging branch and we need to know list of our commits id, to do that we can run this command:


git log

example looks of the result of above command like:

git-log-sample
git log sample

after that, you can switch to the branch where it your destination branch such as development branch in my case. We can run this command to switch to the development branch that existed before.

git checkout development

after we switched the active branch, we need to move our commits in staging to development branch, to do that we can run this command:

git cherry-pick commit_id

replace commit_id with your commit id actually, example:

git cherry-pick s4160ca9ffd95589990156cdf1df45103c805253

do it for each commit that you want to move to current active branch.


I hope this can help you, and sorry about my English. I hope you can get me.