git cherry-pick的注意事项
对于多分支的代码库,将代码从一个分支转移到另一个分支是常见需求。
这时分两种情况。一种情况是,你需要另一个分支的所有代码变动,那么就采用合并(git merge)。另一种情况是,你只需要部分代码变动(某几个提交),这时可以采用 Cherry pick。
git cherry-pick命令的作用,就是将指定的提交(commit)应用于其他分支。
git cherry-pick <commitHash>
commitHash为你在另外一个分支提交的分支id,具体的分支id可以在远程仓库中查看点击,在仓库的左侧导航栏点击commits即可看见该分支下的提交信息。
然后使用 git checkout切换到被转移的分支下,本地使用git fetch 和git pull 获取一下最新的代码。然后切换到转移的分支下,使用git cherry-pick 加提交id。
运行后可能出现
error: could not apply 3e86ed8… Added Copy Patron button to circ-toolbar
hint: after resolving the conflicts, mark the corrected paths
hint: with ‘git add ’ or ‘git rm ’
hint: and commit the result with ‘git commit -c 3e86ed8’
时。 我们可以
git status
git add .
git commit -c 3e86ed8
注意3e86ed8 为commit的id