解释 Git 中的 cherry picking
Cherry picking 是一种从一个分支中选择特定提交并将其应用到另一个分支的方法。在想要从拉取请求中选择特定更改时很有用。
git cherry-pick {commit_hash}
上面的命令将为当前分支 cherry pick 与指定提交哈希关联的提交。
在图表中显示的上述提交中,我们只需要将特征分支中的提交 F1 应用到主分支。
在这种情况下,在 cherry pick 之后的主分支如下所示。
示例
$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo $ git init Initialized empty Git repository in E:/tut_repo/.git/ $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ echo hello>hello.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git add . $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git commit -m 'hello' [master (root-commit) cac1d77] hello 1 file changed, 1 insertion(+) create mode 100644 hello.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ echo hello 2 >> hello.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git commit -am 'c2' [master 4f10a5d] c2 1 file changed, 1 insertion(+) $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git branch feature $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git switch feature Switched to branch 'feature' $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature) $ echo world>world.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature) $ git add . $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature) $ git commit -m 'F1' [feature 9b5ddf0] F1 1 file changed, 1 insertion(+) create mode 100644 world.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature) $ echo world again>>world.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature) $ git commit -am 'F2' [feature 629c15d] F2 1 file changed, 1 insertion(+) $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature) $ git checkout master Switched to branch 'master' $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git log --oneline --all --graph * 629c15d (feature) F2 * 9b5ddf0 F1 * 4f10a5d (HEAD -> master) c2 * cac1d77 hello $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ echo hello3>>hello.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git commit -am 'C3' [master 81cdced] C3 1 file changed, 1 insertion(+) $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git log --oneline --all --graph * 81cdced (HEAD -> master) C3 | * 629c15d (feature) F2 | * 9b5ddf0 F1 |/ * 4f10a5d c2 * cac1d77 hello $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git cherry-pick 9b5ddf0 [master 693bff8] F1 Date: Tue Mar 30 13:43:50 2021 +0530 1 file changed, 1 insertion(+) create mode 100644 world.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git log --oneline --all --graph * 693bff8 (HEAD -> master) F1 * 81cdced C3 | * 629c15d (feature) F2 | * 9b5ddf0 F1 |/ * 4f10a5d c2 * cac1d77 hello $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ ls hello.txt world.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ cat world.txt world
广告