cherry-pick可用于把其余分支的commit,移到当前分支。git
1 在master分支新建git.txtgithub
2 在git.txt输入 “master第一次提交”json
3 在master分支提交segmentfault
git add git.txt
git commit -m 'master第一次提交'复制代码
4 基于master新建test分支bash
git checkout -b test复制代码
5 在git.txt加一行 “test第一次提交”ui
6 在test分支提交url
git add git.txt
git commit -m 'test第一次提交'
写法2: 此时git.txt已经add过了,能够用偷懒的写法
git commit -am 'test第一次提交'复制代码
7 在git.txt加一行 “test第二次提交”spa
8 在test分支提交3d
git commit -am 'test第二次提交'复制代码
9 参照上面 4 ~ 8,基于master新建test2分支,在test2中也提交两遍。code
此时,在master分支有一次提交,在test和test2分支有两次提交,以下图所示。
若是咱们须要把test和test2分支的第一次提交移到master上,那么能够使用cherry-pick(注意:使用merge合并的方式会把全部提交合并过来,而这里咱们只须要test和test2第一次提交)
使用git log查看test和test2中第一次提交的commit id,在cherry-pick中使用。
而后在master分支中cherry-pick
// 4d5a7b1 为test第一次提交的commit id, 3d56b9a为test2第一次提交的commit id
git cherry-pick 4d5a7b1 3d56b9a复制代码
此时若是无冲突,那么test和test2分支第一次提交的内容将会移到master中
若是有冲突,那么解决后使用git add,添加后再执行git cherry-pick --continue
固然你也能够退出cherry-pick,使用git cherry-pick --quit 会退出cherry-pick,可是刚刚cherry-pick的结果会留下
若是你但愿回到cherry-pick以前,那么能够使用 git cherry-pick --abort
若是cherry-pick已经顺利执行完,而你又想回到cherry-pick以前,那么能够使用版本回退啦。
1 cherry-pick一个分支的多个commit时,请按顺序填写commit id,或者使用 "..." 限定范围,以下。
// 从start-commit-id 到 end-commit-id 不包含 start-commit-id
git cherry-pick <start-commit-id>…<end-commit-id>
// 从start-commit-id 到 end-commit-id 包含 start-commit-id
git cherry-pick <start-commit-id>^…<end-commit-id>复制代码
2 假若有一个commit,是从其余分支合并过来造成的,那么cherry-pick这个commit将会报错,是由于git不知道取哪一个分支的内容,使用-m选项便可。参考这里
error: commit fd30d42926885f792976a094b1aa0c96e8228240 is a merge but no -m option was given.复制代码
git log --all --full-history -- package-lock.json复制代码
--all 展现包括其余分支的全部历史
--full-history 查看某个文件历史时,git会自动简化历史(History Simplification),甚至会隐藏一些提交。--full-history能够关闭History Simplification。
git 2.9 后默认不容许合并两个不相关的没有共同祖先的分支,好比新建本地仓库,新建github仓库,本地仓库 git remote add origin <repository url> ,而后git pull origin master,会报错。
fatal: refusing to merge unrelated histories复制代码
能够使用--allow-unrelated-histories解决
// pull时
git pull origin <branch> --allow-unrelated-histories
// merge时
git merge <branch> --allow-unrelated-histories复制代码
git checkout --orphan <new-branch>复制代码
--orphan建立出来的分支没有任何提交历史
// 克隆仓库,且只克隆最近 1 次提交历史,若是想克隆最近 n 次的历史,把 1 改为 n 便可。
git clone --depth=1 <repository url>复制代码