How to move git commits from master to a different existing branch
This question already has an answer here:
If I'm not mistaken, you had two synchronized branches, master
and dev
, and simply forgot to switch the branch, before your commits.
If that is the case, we have:
----------------
git log in dev
xxx
yyy
...
----------------
and:
----------------
git log in master
ccc
bbb
aaa
<---- here you forgot to switch branch
xxx
yyy
...
----------------
The solution is:
First, make sure, that:
git status -s
returns empty results.
Next, get all your new commits from master
to dev
with:
git checkout dev
git merge master
Now return to you master
:
git checkout master
Remove unnecessary commits:
git reset --hard HEAD~3
The number ~3
is the number of commits you want to remove.
Remember: git status -s
have to return empty results. Otherwise, git reset --hard
can cause data loss.
For coping into another branch you can use cherry picking:
git cherry-pick <commit>
Deleting is not that easy. You can use rebase and squash or edit the commit:
git rebase -i <commit>~1
But I am not sure when chosing edit during rebase if you can edit the files also rather than the commit message only.
You can cherry-pick
your commits over to the develop
and afterwards interactively rebase
your master
branch:
git checkout develop
git cherry-pick aabbcc
git cherry-pick ddeeff
git checkout master
git rebase 123456 -i
where 123456
is a commit before you made a mistake. This will open an editor that shows every commit that will be affected by the rebase. Delete the lines that correspond to the commits you want to discard and quit the editor.
上一篇: git:如何将一个克隆变成一个新的分支?
下一篇: 如何将主从git提交到不同的现有分支