有的时候不小心在错误的分支A上开发了一点代码,也已经提交了,这个时候如果想把这部分代码移到正确的分支B上可以如下操作
- 使用git log命令找到错误提交的那次commitid
git log
commit 8b19200eed045d5bef5b304c30eb76d74f3943ea (HEAD -> A, origin/A)
Author: alioo <xxx@yyy.com>
Date: 2021-03-08T21:45:40+08:00
取消没有必要的RecommendException
- checkout到分支B之后,使用cherry-pick命令进行merge
git checkout B
Switched to branch 'B'
Your branch is up to date with 'origin/B'.
git cherry-pick 8b19200eed045d5bef5b304c30eb76d74f3943ea
[A b2da47b48] 取消没有必要的RecommendException
Date: Mon Mar 8 21:45:40 2021 +0800
1 file changed, 4 insertions(+), 2 deletions(-)
git status
On branch B
Your branch is ahead of 'B' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
git push
Enumerating objects: 27, done.
Counting objects: 100% (27/27), done.
Delta compression using up to 8 threads
Compressing objects: 100% (9/9), done.
Writing objects: 100% (14/14), 1.04 KiB | 353.00 KiB/s, done.
Total 14 (delta 6), reused 0 (delta 0)