admin管理员组

文章数量:1023815

I have cherry-picked a number of changes and would like to update and push only the top-level commit. However, all the cherry-picked commits are also getting updated. For example, change 444683 received a new patch set with the comment: Uploaded patch set 5: New patch set was added with same tree, parent tree, and commit message as Patch Set 4. I do not want a new patch-set to be created for the previously cherry-picked commits. Is there a way to prevent this?

$ git fetch ssh://review-project.myinc:29418/myprj/myprj-project refs/changes/83/444683/4 && git cherry-pick FETCH_HEAD
From ssh://git-project-lv.myinc:29418/myprj/myprj-project
 * branch                      refs/changes/83/444683/4 -> FETCH_HEAD
[main 2b6e920a0218] Test failure case 3
 Date: Fri Nov 8 06:17:37 2024 -0800
 1 file changed, 3 insertions(+)

$ git fetch ssh://review-project.myinc:29418/myprj/myprj-project refs/changes/40/449340/1 && git cherry-pick FETCH_HEAD
remote: Counting objects: 8958, done
remote: Finding sources: 100% (6371/6371)
remote: Total 6371 (delta 3429), reused 5218 (delta 3429)
Receiving objects: 100% (6371/6371), 18.74 MiB | 9.41 MiB/s, done.
Resolving deltas: 100% (3429/3429), completed with 750 local objects.
From ssh://git-project-lv.myinc:29418/myprj/myprj-project
 * branch                      refs/changes/40/449340/1 -> FETCH_HEAD
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

You are currently cherry-picking commit 74726ac5a732.


$ git commit --allow-empty #-----------> I updated the commit message
[main 451839fba790] cherry-pick test for CUI
 Date: Mon Nov 18 10:09:47 2024 -0800

$ git log -1
commit 451839fba790e57f8255c96f6b626ccde3c98ce6 (HEAD -> main)
Author: Neth S <[email protected]>
Date:   Mon Nov 18 10:09:47 2024 -0800

    cherry-pick test

    Testing purpose

    Change-Id: I9c0fc86423ada538ad12072f2e6bb530f6dde258

$ git push origin 451839fba790e57f8255c96f6b626ccde3c98ce6:refs/for/main
Counting objects: 5, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 641 bytes | 641.00 KiB/s, done.
Total 5 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3)
remote: Processing changes: refs: 1, updated: 2, done
remote: warning: no changes between prior commit 526f331 and new commit 2b6e920
remote:
remote: SUCCESS
remote:
remote:   /+/444683 Test failure case 3
remote:   /+/449340 cherry-pick test
remote:
To ssh://review-project.myinc:29418/myprj/myprj-project
 * [new branch]                451839fba790e57f8255c96f6b626ccde3c98ce6 -> refs/for/main

I have cherry-picked a number of changes and would like to update and push only the top-level commit. However, all the cherry-picked commits are also getting updated. For example, change 444683 received a new patch set with the comment: Uploaded patch set 5: New patch set was added with same tree, parent tree, and commit message as Patch Set 4. I do not want a new patch-set to be created for the previously cherry-picked commits. Is there a way to prevent this?

$ git fetch ssh://review-project.myinc:29418/myprj/myprj-project refs/changes/83/444683/4 && git cherry-pick FETCH_HEAD
From ssh://git-project-lv.myinc:29418/myprj/myprj-project
 * branch                      refs/changes/83/444683/4 -> FETCH_HEAD
[main 2b6e920a0218] Test failure case 3
 Date: Fri Nov 8 06:17:37 2024 -0800
 1 file changed, 3 insertions(+)

$ git fetch ssh://review-project.myinc:29418/myprj/myprj-project refs/changes/40/449340/1 && git cherry-pick FETCH_HEAD
remote: Counting objects: 8958, done
remote: Finding sources: 100% (6371/6371)
remote: Total 6371 (delta 3429), reused 5218 (delta 3429)
Receiving objects: 100% (6371/6371), 18.74 MiB | 9.41 MiB/s, done.
Resolving deltas: 100% (3429/3429), completed with 750 local objects.
From ssh://git-project-lv.myinc:29418/myprj/myprj-project
 * branch                      refs/changes/40/449340/1 -> FETCH_HEAD
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

You are currently cherry-picking commit 74726ac5a732.


$ git commit --allow-empty #-----------> I updated the commit message
[main 451839fba790] cherry-pick test for CUI
 Date: Mon Nov 18 10:09:47 2024 -0800

$ git log -1
commit 451839fba790e57f8255c96f6b626ccde3c98ce6 (HEAD -> main)
Author: Neth S <[email protected]>
Date:   Mon Nov 18 10:09:47 2024 -0800

    cherry-pick test

    Testing purpose

    Change-Id: I9c0fc86423ada538ad12072f2e6bb530f6dde258

$ git push origin 451839fba790e57f8255c96f6b626ccde3c98ce6:refs/for/main
Counting objects: 5, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 641 bytes | 641.00 KiB/s, done.
Total 5 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3)
remote: Processing changes: refs: 1, updated: 2, done
remote: warning: no changes between prior commit 526f331 and new commit 2b6e920
remote:
remote: SUCCESS
remote:
remote:   https://review-project.myinc/c/myprj/myprj-project/+/444683 Test failure case 3
remote:   https://review-project.myinc/c/myprj/myprj-project/+/449340 cherry-pick test
remote:
To ssh://review-project.myinc:29418/myprj/myprj-project
 * [new branch]                451839fba790e57f8255c96f6b626ccde3c98ce6 -> refs/for/main
Share Improve this question asked Nov 18, 2024 at 18:28 rodeerodee 3,1915 gold badges39 silver badges79 bronze badges 2
  • Any parent of HEAD will be updated if the parent is open and has been rewritten. If you want to update the HEAD commit only, you cannot rewrite any of its parents. In other words, you need to keep its parent unchanged and amend HEAD only. – ElpieKay Commented Nov 19, 2024 at 6:45
  • What are you trying to accomplish? If you want your patch to end up on the branch where you're cherry-picking from, you should work on a local branch that is fully sync'd with that branch. Then there is no cherry picking. If you're trying to make a 2nd branch with a subset of the changes, those need to get cherry-picked, and in doing so they get their own SHA1 and become different entities. And someone needs to code review that the cherry-pick is acceptable in that 2nd branch. – Brent K. Commented Nov 21, 2024 at 12:58
Add a comment  | 

1 Answer 1

Reset to default 0

Git can't just create stuff when pushing. You setup what you want and then push that.

This requires rewriting history. You either cherry-pick the top commit on top of the base branch or, the easier way, rebase getting rid of the other commits. 5 commits and you only want to keep the last one, right?

git rebase HEAD~ the-current-branch --onto HEAD~5

I have cherry-picked a number of changes and would like to update and push only the top-level commit. However, all the cherry-picked commits are also getting updated. For example, change 444683 received a new patch set with the comment: Uploaded patch set 5: New patch set was added with same tree, parent tree, and commit message as Patch Set 4. I do not want a new patch-set to be created for the previously cherry-picked commits. Is there a way to prevent this?

$ git fetch ssh://review-project.myinc:29418/myprj/myprj-project refs/changes/83/444683/4 && git cherry-pick FETCH_HEAD
From ssh://git-project-lv.myinc:29418/myprj/myprj-project
 * branch                      refs/changes/83/444683/4 -> FETCH_HEAD
[main 2b6e920a0218] Test failure case 3
 Date: Fri Nov 8 06:17:37 2024 -0800
 1 file changed, 3 insertions(+)

$ git fetch ssh://review-project.myinc:29418/myprj/myprj-project refs/changes/40/449340/1 && git cherry-pick FETCH_HEAD
remote: Counting objects: 8958, done
remote: Finding sources: 100% (6371/6371)
remote: Total 6371 (delta 3429), reused 5218 (delta 3429)
Receiving objects: 100% (6371/6371), 18.74 MiB | 9.41 MiB/s, done.
Resolving deltas: 100% (3429/3429), completed with 750 local objects.
From ssh://git-project-lv.myinc:29418/myprj/myprj-project
 * branch                      refs/changes/40/449340/1 -> FETCH_HEAD
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

You are currently cherry-picking commit 74726ac5a732.


$ git commit --allow-empty #-----------> I updated the commit message
[main 451839fba790] cherry-pick test for CUI
 Date: Mon Nov 18 10:09:47 2024 -0800

$ git log -1
commit 451839fba790e57f8255c96f6b626ccde3c98ce6 (HEAD -> main)
Author: Neth S <[email protected]>
Date:   Mon Nov 18 10:09:47 2024 -0800

    cherry-pick test

    Testing purpose

    Change-Id: I9c0fc86423ada538ad12072f2e6bb530f6dde258

$ git push origin 451839fba790e57f8255c96f6b626ccde3c98ce6:refs/for/main
Counting objects: 5, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 641 bytes | 641.00 KiB/s, done.
Total 5 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3)
remote: Processing changes: refs: 1, updated: 2, done
remote: warning: no changes between prior commit 526f331 and new commit 2b6e920
remote:
remote: SUCCESS
remote:
remote:   /+/444683 Test failure case 3
remote:   /+/449340 cherry-pick test
remote:
To ssh://review-project.myinc:29418/myprj/myprj-project
 * [new branch]                451839fba790e57f8255c96f6b626ccde3c98ce6 -> refs/for/main

I have cherry-picked a number of changes and would like to update and push only the top-level commit. However, all the cherry-picked commits are also getting updated. For example, change 444683 received a new patch set with the comment: Uploaded patch set 5: New patch set was added with same tree, parent tree, and commit message as Patch Set 4. I do not want a new patch-set to be created for the previously cherry-picked commits. Is there a way to prevent this?

$ git fetch ssh://review-project.myinc:29418/myprj/myprj-project refs/changes/83/444683/4 && git cherry-pick FETCH_HEAD
From ssh://git-project-lv.myinc:29418/myprj/myprj-project
 * branch                      refs/changes/83/444683/4 -> FETCH_HEAD
[main 2b6e920a0218] Test failure case 3
 Date: Fri Nov 8 06:17:37 2024 -0800
 1 file changed, 3 insertions(+)

$ git fetch ssh://review-project.myinc:29418/myprj/myprj-project refs/changes/40/449340/1 && git cherry-pick FETCH_HEAD
remote: Counting objects: 8958, done
remote: Finding sources: 100% (6371/6371)
remote: Total 6371 (delta 3429), reused 5218 (delta 3429)
Receiving objects: 100% (6371/6371), 18.74 MiB | 9.41 MiB/s, done.
Resolving deltas: 100% (3429/3429), completed with 750 local objects.
From ssh://git-project-lv.myinc:29418/myprj/myprj-project
 * branch                      refs/changes/40/449340/1 -> FETCH_HEAD
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

You are currently cherry-picking commit 74726ac5a732.


$ git commit --allow-empty #-----------> I updated the commit message
[main 451839fba790] cherry-pick test for CUI
 Date: Mon Nov 18 10:09:47 2024 -0800

$ git log -1
commit 451839fba790e57f8255c96f6b626ccde3c98ce6 (HEAD -> main)
Author: Neth S <[email protected]>
Date:   Mon Nov 18 10:09:47 2024 -0800

    cherry-pick test

    Testing purpose

    Change-Id: I9c0fc86423ada538ad12072f2e6bb530f6dde258

$ git push origin 451839fba790e57f8255c96f6b626ccde3c98ce6:refs/for/main
Counting objects: 5, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 641 bytes | 641.00 KiB/s, done.
Total 5 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3)
remote: Processing changes: refs: 1, updated: 2, done
remote: warning: no changes between prior commit 526f331 and new commit 2b6e920
remote:
remote: SUCCESS
remote:
remote:   https://review-project.myinc/c/myprj/myprj-project/+/444683 Test failure case 3
remote:   https://review-project.myinc/c/myprj/myprj-project/+/449340 cherry-pick test
remote:
To ssh://review-project.myinc:29418/myprj/myprj-project
 * [new branch]                451839fba790e57f8255c96f6b626ccde3c98ce6 -> refs/for/main
Share Improve this question asked Nov 18, 2024 at 18:28 rodeerodee 3,1915 gold badges39 silver badges79 bronze badges 2
  • Any parent of HEAD will be updated if the parent is open and has been rewritten. If you want to update the HEAD commit only, you cannot rewrite any of its parents. In other words, you need to keep its parent unchanged and amend HEAD only. – ElpieKay Commented Nov 19, 2024 at 6:45
  • What are you trying to accomplish? If you want your patch to end up on the branch where you're cherry-picking from, you should work on a local branch that is fully sync'd with that branch. Then there is no cherry picking. If you're trying to make a 2nd branch with a subset of the changes, those need to get cherry-picked, and in doing so they get their own SHA1 and become different entities. And someone needs to code review that the cherry-pick is acceptable in that 2nd branch. – Brent K. Commented Nov 21, 2024 at 12:58
Add a comment  | 

1 Answer 1

Reset to default 0

Git can't just create stuff when pushing. You setup what you want and then push that.

This requires rewriting history. You either cherry-pick the top commit on top of the base branch or, the easier way, rebase getting rid of the other commits. 5 commits and you only want to keep the last one, right?

git rebase HEAD~ the-current-branch --onto HEAD~5

本文标签: Git push to Gerrit only the HEAD commit excluding previously cherrypicked commitsStack Overflow