问题
I normally use the following git command to cherryppick a range of gerrits..no how do I exclude a couple gerrits in between.. can the below command be modified or is there one where we can pick a range of gerrits and exclude the ones we want..
git cherrypick fromgerritSHA1..togerritSHA1
回答1:
You can specify multiple ranges:
git cherry-pick A..B C..D E..F
or even specific commits:
git cherry-pick A B C D E F
If you have lots of commits you want to exclude, it might be easier to do something like this (sort of like a poor man's git rebase -i
for git cherry-pick
):
git log --pretty=oneline A..F | tac > tempfile.txt
< edit tempfile.txt to remove the commits you don't want >
git cherry-pick $(awk '{print $1}' tempfile.txt)
Edit: Added recommendation to tac
the log, since git cherry-pick
wants to see the commits in the opposite order from what git log
produces (could also use git log --reverse ...
).
回答2:
Not sure if you can pull individual commits out of a range abcdef..123456
. Git range syntax is explained in the docs for gitrevisions, and it doesn't look like it works that way. Still, there's another way to get what you want using only the range hashes and the ones to exclude.
Assuming the two hashes in range fromgerritSHA1..togerritSHA1
that you don't want are skiphash1
and skiphash2
, try:
$ git rev-list --reverse fromgerritSHA1..togerritSHA1 | grep -vE 'skiphash1|skiphash2' | git cherry-pick --stdin
git rev-list --reverse fromgerritSHA1..togerritSHA1
prints out commit hashes in range fromgerritSHA1..togerritSHA1
, one line at a time. --reverse
is needed to list the hashes in the correct order for the cherry-pick.
grep -vE 'skiphash1|skiphash2'
removes the two hashes you don't want from the list. You can add more hashes to skip, just separate them with |
.
Finally, the list of only the commit hashes you want is passed to git cherry-pick --stdin
.
回答3:
If you got problem during cherry-pick like below, try to choose starting hash one before than your original attempt was.
git cherry-pick a005efa..1ece685
[temp_4454kjerer3233 3520dd4] 3. xxx.
1 file changed, 9 insertions(+)
[temp_4454kjerer3233 791cec5] 4. xxx.
3 files changed, 19 insertions(+)
[temp_4454kjerer3233 2e95364] 5. xxx.
2 files changed, 21 insertions(+)
[temp_4454kjerer3233 59e38b9] 6. xxx.
3 files changed, 61 insertions(+)
error: could not apply a3b0c6b... 7. xxx.
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
来源:https://stackoverflow.com/questions/15687346/git-cherry-pick-range-of-commits-and-exclude-some-in-between