git cherry-pick 把其他commit合并到当前分支
参考博客:https://ruanyifeng.com/blog/2020/04/git-cherry-pick.html
就是站到你想要合并到的分支,然后去cherry-pick其他commit
例如现在有两个分支,master和feature。
feature有1,2,3,4,5,一共5个commit。
feature是从master过来的。
现在master想要2-5所有的改动,那么可以
git cherry-pick 1 5
如果想要1-5所有的改动,那么可以
git cherry-pick 1^..5
如果只想要 5 这个commit,那么可以
git cherry-pick 5
测试样例:
1 创建一个test_cherry_pick的文件夹,并初始化,添加README,并commit
mkdir test_cherry_pick
cd test_cherry_pick
git init .
echo "master" > README
git add .
git commit -m "master add README"
2 创建feature分支,添加5次commit,每次添加一个文件(不想解决冲突,2333)
git checkout -b feature
echo "1.cpp" > 1.cpp
git add .
git commit -m "1.cpp"
echo "2.cpp" > 2.cpp
git add .
git commit -m "2.cpp"
echo "3.cpp" > 3.cpp
git add .
git commit -m "3.cpp"
echo "4.cpp" > 4.cpp
git add .
git commit -m "4.cpp"
echo "5.cpp" > 5.cpp
git add .
git commit -m "5.cpp"
git log
3 将2-5 合并到master
git checkout master
git cherry-pick 9784ac530d2a9bff7f5cb0fd1a069e838f973003^..bc449a6ef7602dd8a129349708d63d324ad37e2c
结果:
[user@server test_cherry_pick]$ git cherry-pick 9784ac530d2a9bff7f5cb0fd1a069e838f973003^..bc449a6ef7602dd8a129349708d63d324ad37e2c
[master 1966347] 1.cpp
Date: Fri Sep 1 09:57:45 2023 +0800
1 file changed, 1 insertion(+)
create mode 100644 1.cpp
[master 6a4b5fa] 2.cpp
Date: Fri Sep 1 09:57:45 2023 +0800
1 file changed, 1 insertion(+)
create mode 100644 2.cpp
[master ce7d9c6] 3.cpp
Date: Fri Sep 1 09:57:45 2023 +0800
1 file changed, 1 insertion(+)
create mode 100644 3.cpp
[master cc078a2] 4.cpp
Date: Fri Sep 1 09:57:45 2023 +0800
1 file changed, 1 insertion(+)
create mode 100644 4.cpp
[master 196b1fa] 5.cpp
Date: Fri Sep 1 09:57:47 2023 +0800
1 file changed, 1 insertion(+)
create mode 100644 5.cpp
[user@server test_cherry_pick]$ cat README
master
[user@server test_cherry_pick]$ git br
feature
* master
[user@server test_cherry_pick]$ ls
1.cpp 2.cpp 3.cpp 4.cpp 5.cpp README
[user@server test_cherry_pick]$