如何挑选多个提交

发布于 2024-08-10 02:35:11 字数 607 浏览 14 评论 0原文

我有两个分支机构。 Commit a 是其中一个的头部,而另一个则有 bcdef 位于 a 之上。我想将 cdef 移动到第一个分支而不提交 b.使用cherry pick很简单:签出第一个分支cherry-pick一个一个地从cf并将第二个分支重新设置为第一个分支。但是有没有办法在一个命令中挑选所有 c-f 呢?

以下是该场景的直观描述(感谢 JJD):

显示“之前”和“之后”状态的提交。在“之前”状态中,提交 a 到 f 以一个连续序列连接。在之后状态中,提交 c 到 f 已被重新定位以直接连接到 a无需重新排序,将 b 提交留在后面。

I have two branches. Commit a is the head of one, while the other has b, c, d, e and f on top of a. I want to move c, d, e and f to first branch without commit b. Using cherry pick it is easy: checkout first branch cherry-pick one by one c to f and rebase second branch onto first. But is there any way to cherry-pick all c-f in one command?

Here is a visual description of the scenario (thanks JJD):

A commit showing a 'before' and 'after' state. In the 'before' state, commits a through f are connected in one contiguous sequence. In the after state, commits c through f have been relocated to directly connect to a without reordering, leaving the b commit behind.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(18

万劫不复 2024-08-17 02:35:12
git format-patch --full-index --binary --stdout range... | git am -3
git format-patch --full-index --binary --stdout range... | git am -3
苦笑流年记忆 2024-08-17 02:35:11

Git 1.7.2 引入了挑选一系列提交的功能。来自发行说明

gitcherry-pick 学会了选择一系列提交
(例如cherry-pick A..Bcherry-pick --stdin),git revert也是如此;不过,它们不支持 rebase [-i] 所具有的更好的排序控制。

要挑选从提交 A 到提交 B 的所有提交(其中 A 早于 B), run:

git cherry-pick A^..B

如果您想忽略 A 本身,请运行:

git cherry-pick A..B

注释中的注释:

  • A 应该早于 BA< /code> 应该来自另一个分支。
  • 在 Windows 上,它应该是 A^^..B 因为插入符号需要转义,或者应该是 "A^..B" (双引号)。
  • zsh shell 中,它应该是 'A^..B' (单引号),因为插入符号是特殊字符。
  • 有关说明,请参阅Gabriel Staples 的回答

(感谢评论中的 damian、JB Rainsberger、sschaef、Neptilo、Pete 和 TMin。)

Git 1.7.2 introduced the ability to cherry pick a range of commits. From the release notes:

git cherry-pick learned to pick a range of commits
(e.g. cherry-pick A..B and cherry-pick --stdin), so did git revert; these do not support the nicer sequencing control rebase [-i] has, though.

To cherry-pick all the commits from commit A to commit B (where A is older than B), run:

git cherry-pick A^..B

If you want to ignore A itself, run:

git cherry-pick A..B

Notes from comments:

  • A should be older than B, or A should be from another branch.
  • On Windows, it should be A^^..B as the caret needs to be escaped, or it should be "A^..B" (double quotes).
  • In zsh shell, it should be 'A^..B' (single quotes) as the caret is a special character.
  • For an exposition, see the answer by Gabriel Staples.

(Credits to damian, J. B. Rainsberger, sschaef, Neptilo, Pete and TMin in the comments.)

喜你已久 2024-08-17 02:35:11

如果您有选择性的修订要合并,例如来自 A、B、C、D、E、F、G、H、I、J 提交的 A、C、F、J,只需使用以下命令:

git cherry-pick A C F J

If you have selective revisions to merge, say A, C, F, J from A,B,C,D,E,F,G,H,I,J commits, simply use the below command:

git cherry-pick A C F J
千仐 2024-08-17 02:35:11

如何挑选单个提交、多个提交或一系列提交

...到当前签出的分支:

1. 挑选单个 名为 commit 的分支或提交

git cherry-pick commit

示例:

git cherry-pick my_branch                                 # by branch name
git cherry-pick 1e038f108a130831f108329b1083a8139813fabc  # by full hash
git cherry-pick 1e038f10                                  # by partial hash

2. 挑选多个提交

请注意,您可以挑选任意数量的提交哈希一次,并且按照您想要的任何顺序。它们只会按照您指定的顺序一次应用一个。如果出现任何冲突,您必须一次解决一个问题,然后使用 git add my_file ,然后在完成后使用 gitcherry-pick --Continue 继续樱桃采摘过程。

git cherry-pick commit1 commit2 commit3 commit4 commit5

3. 挑选范围的提交

我最初是从最受好评的文章中学习这种风格的基础知识的@Eric Darchis 在这里回答

请注意,要挑选范围的提交,您必须指定开始和结束提交哈希,并在它们之间使用..。但是,在一系列提交中,不包括开始提交。因此,要包含它,您必须在开始提交之前指定提交。指定前面提交的语法是将~~1^放在之后 你的提交,如:beginning_commit~,这意味着:“在beginning_commit之前的提交”。

# A. INCLUDING the beginning_commit
git cherry-pick beginning_commit~..ending_commit
# OR (same as above)
git cherry-pick beginning_commit~1..ending_commit
# OR (same as above)
git cherry-pick beginning_commit^..ending_commit 

# B. NOT including the beginning_commit
git cherry-pick beginning_commit..ending_commit

注意: commit~commit~1commit^ 均表示“在 commit”,或者说:“commit 之前的提交”。

要在提交之前指定两次次提交,您可以使用如下语法:

commit~~
commit~2  # my preferred syntax
commit^^

要在提交之前指定三次次提交,你可以这样做:

commit~~~  
commit~3   # my preferred syntax
commit^^^

这不起作用:

commit^3   # INVALID syntax

确实有效,但这是一个非常特殊的情况。另请参阅我的问答:git merge 风格的工作流程中,仅显示某人在合并之前的 PR(拉取请求)功能分支

# valid on **merge commits** only
commit^2   # this is the immediate **right** parent of the two parents 
           # involved in the merge (which made commit `commit`)

# valid on any commits (gets the left parent of a merge commit, or 
# the only parent of a non-merge commit)
commit~    # and this is the immediate **left** parent of the two 
           # parents involved in the merge (which made commit `commit`)

要自己测试上述“先前提交语法”概念,最简单的方法是使用 git log 命令。例如:

git log commit
git log commit~
git log commit~1
git log commit^
git log commit~~
git log commit~5
# etc.

要挑选一系列同级的提交到您的分支上

4.当您的同级分支 peer_branch 是从您的分支 my_branch 的早期版本分叉出来时,

。快速总结

# you cherry-pick all of their extra commits from their `peer_branch` onto 
# your `my_branch` (note: the 3 dots below are very important!)

git fetch origin peer_branch  # get their latest changes from the remote
git checkout my_branch        # ensure you're on your branch
# cherry-pick their range of commits
git cherry-pick my_branch...origin/peer_branch  
git log                       # review the commits you just chery-picked
git push                      # push your changes to the remote

提交范围内的 2 点和 3 点之间的差异非常显着。 git diffbranch1...branch2 相当于 git diff $(git merge-basebranch1branch2)branch2。当您想要查看在 branch2 中所做的更改(因为它与 branch1 不同)而不是两个分支在当前状态下的差异时,这非常有用。请参阅我的在这里评论这里以及这里的答案: Git diff 提交范围中的双点“..”和三点“...”有什么区别?

完整详细信息和工作流程演练

假设您正在开发功能分支 my_branch,并且您的同事希望帮助您进行一些更改,以帮助您完成功能。您已经将 my_branch 推送到名为 origin 的远程。因此,他们会将名为 my_branch 的远程分支获取到本地计算机,从中分叉出自己名为 peer_brach 的分支,然后推送到自己名为 的分支>peer_branch。一旦他们这样做了,你就可以立即挑选他们添加的所有内容。这个过程的第一部分是这样的:

# **your peer** does this

# peer fetches your branch named `my_branch` and forks their `peer_branch`
# off of it

# they fetch your latest work from remote `my_branch` into their locally-stored
# remote-tracking "hidden" branch named `origin/my_branch`
# (note: you can see all locally-stored remote-tracking "hidden" branches
# with `git branch -r`)
git fetch origin my_branch
# create `peer_branch` as a fork off of `origin/my_branch`, and check it out
git checkout -b peer_branch origin/my_branch

# Now they can add their changes and commits and `git push` to remote `origin`
# as their own `peer_branch` when done.

现在他们已经将所有更改推送到远程 origin 作为他们自己的名为 peer_branch 的分支,您可以挑选他们在您的工作之上添加的所有提交如下所示:

# **you** do this to cherry-pick your peer's helpful changes they added to 
# your work

# you fetch their latest work from their branch named `peer_branch` on remote
# `origin` into your locally-stored remote-tracking "hidden" branch named 
# `origin/peer_branch` 
# (note: you can see all locally-stored remote-tracking "hidden" branches
# with `git branch -r`)
git fetch origin peer_branch
# ensure you are on `my_branch` (if you aren't already)
git checkout my_branch
# you cherry-pick all of their extra commits from their `peer_branch` onto 
# your `my_branch` (note: the 3 dots here are very important!)
git cherry-pick my_branch...origin/peer_branch

git log                       # review the commits you just chery-picked
git push                      # push your changes to the remote

为了您的理解,上面的 cherry-pick 命令(其中有 3 个点)是 与这个较长的命令完全相同:

git cherry-pick $(git merge-base my_branch origin/peer_branch)..origin/peer_branch

git merge-base my_branch origin/peer_branch 部分查找分支 my_branch 和分支 origin 之间的公共父提交哈希/peer_branch。这是他们从您的 my_branch 分叉出自己的 peer_branch 的提交。然后,您当然会挑选从该点到 (..) 在 origin/peer_branch 的最终提交的提交范围 >。

要了解有关三点语法的更多信息,请参阅此处:双点“..”和三点“之间有什么区别...”在 Git diff 提交范围中? [重复]。有关 git checkout -b new_branch from_branch 的帮助,请参阅我的答案:在 git 中创建分支的各种方法来自另一个分支

官方 Git 文档

  1. https://git-scm.com/docs/gitrevisions - 提到 git commit 3 点 (...) 与 2 点范围语法,^commit(“不是”commit),< code>commit^ (commit 的父级)等。

进一步

  1. 需要了解的其他内容:git rebase 只是一堆连续的 gitcherry-pick。请参阅我的其他答案(根据 Git,谁是“我们”,谁是“他们”?),我在其中展示,除此之外,我还绘制了一张 ASCII 绘图,展示了 git rebase 的工作原理及其作用。
  2. Git diff 提交范围中的双点“..”和三点“...”有什么区别? [重复]
  3. 我对从另一个分支在git中创建分支的各种方法的回答我的问答
  4. git merge 式工作流程中,仅显示某人在其 PR(拉取请求)功能中拥有的唯一提交合并前的分支

How to cherry-pick a single commit, multiple commits, or a range of commits

...onto your currently-checked-out branch:

1. to cherry-pick a single branch or commit named commit

git cherry-pick commit

Examples:

git cherry-pick my_branch                                 # by branch name
git cherry-pick 1e038f108a130831f108329b1083a8139813fabc  # by full hash
git cherry-pick 1e038f10                                  # by partial hash

2. to cherry-pick multiple commits

Note that you can cherry-pick any number of commit hashes at once, and in any order you want. They will simply be applied one-at-a-time, and in the order you specify. If any conflicts arise, you will have to resolve them one-at-a-time then use git add my_file then git cherry-pick --continue when done to continue the cherry-pick process.

git cherry-pick commit1 commit2 commit3 commit4 commit5

3. to cherry-pick a range of commits

I originally learned the basics of this style from the most-upvoted answer by @Eric Darchis here.

Notice that to cherry-pick a range of commits, you must specify a starting and ending commit hash, with .. between them. However, in a range of commits, the beginning commit is NOT included. Therefore, to include it, you must specify the commit before the beginning commit. The syntax to specify the preceding commit is to put ~, ~1, or ^ right after your commit, as in: beginning_commit~, which means: "the commit right before beginning_commit".

# A. INCLUDING the beginning_commit
git cherry-pick beginning_commit~..ending_commit
# OR (same as above)
git cherry-pick beginning_commit~1..ending_commit
# OR (same as above)
git cherry-pick beginning_commit^..ending_commit 

# B. NOT including the beginning_commit
git cherry-pick beginning_commit..ending_commit

Note: commit~, commit~1, and commit^ all mean "one commit prior to commit", or otherwise said: "the commit before commit".

To specify two commits prior to commit, you can use syntax like this:

commit~~
commit~2  # my preferred syntax
commit^^

To specify three commits prior to commit, you can do this:

commit~~~  
commit~3   # my preferred syntax
commit^^^

This does NOT work:

commit^3   # INVALID syntax

This does work, but is a very special case. See also my Q&A: In a git merge-style workflow, show only the unique commits someone had in their PR's (Pull Request's) feature branch before merging:

# valid on **merge commits** only
commit^2   # this is the immediate **right** parent of the two parents 
           # involved in the merge (which made commit `commit`)

# valid on any commits (gets the left parent of a merge commit, or 
# the only parent of a non-merge commit)
commit~    # and this is the immediate **left** parent of the two 
           # parents involved in the merge (which made commit `commit`)

To test the above "previous commit syntax" concepts yourself, the easiest way is with the git log command. Ex:

git log commit
git log commit~
git log commit~1
git log commit^
git log commit~~
git log commit~5
# etc.

4. To cherry-pick a range of your peer's commits onto your branch

...when their branch peer_branch is forked off of an earlier version of your branch my_branch.

Quick summary

# you cherry-pick all of their extra commits from their `peer_branch` onto 
# your `my_branch` (note: the 3 dots below are very important!)

git fetch origin peer_branch  # get their latest changes from the remote
git checkout my_branch        # ensure you're on your branch
# cherry-pick their range of commits
git cherry-pick my_branch...origin/peer_branch  
git log                       # review the commits you just chery-picked
git push                      # push your changes to the remote

The difference between 2 dots and 3 dots in a commit range is very significant. git diff branch1...branch2 is the equivalent of git diff $(git merge-base branch1 branch2) branch2. This is useful when you want to see the changes that have been made in branch2 since it diverged from branch1, rather than the differences between the two branches in their current states. See my comment here and here and the answers here: What are the differences between double-dot ".." and triple-dot "..." in Git diff commit ranges?

Full details and work-flow walk-through

Let's say you are working on your feature branch my_branch, and your peer wants to help make some changes for you to help you on your feature. You have already pushed my_branch to the remote named origin. So, they are going to fetch your remote branch named my_branch to their local computer, fork their own branch named peer_brach off of it, then push to their own branch named peer_branch. Once they do that, you will cherry-pick all of their additions at once. This is what the first part of this process looks like:

# **your peer** does this

# peer fetches your branch named `my_branch` and forks their `peer_branch`
# off of it

# they fetch your latest work from remote `my_branch` into their locally-stored
# remote-tracking "hidden" branch named `origin/my_branch`
# (note: you can see all locally-stored remote-tracking "hidden" branches
# with `git branch -r`)
git fetch origin my_branch
# create `peer_branch` as a fork off of `origin/my_branch`, and check it out
git checkout -b peer_branch origin/my_branch

# Now they can add their changes and commits and `git push` to remote `origin`
# as their own `peer_branch` when done.

Now that they have pushed all of their changes to remote origin as their own branch named peer_branch, you can cherry-pick all of their commits they added on top of your work like this:

# **you** do this to cherry-pick your peer's helpful changes they added to 
# your work

# you fetch their latest work from their branch named `peer_branch` on remote
# `origin` into your locally-stored remote-tracking "hidden" branch named 
# `origin/peer_branch` 
# (note: you can see all locally-stored remote-tracking "hidden" branches
# with `git branch -r`)
git fetch origin peer_branch
# ensure you are on `my_branch` (if you aren't already)
git checkout my_branch
# you cherry-pick all of their extra commits from their `peer_branch` onto 
# your `my_branch` (note: the 3 dots here are very important!)
git cherry-pick my_branch...origin/peer_branch

git log                       # review the commits you just chery-picked
git push                      # push your changes to the remote

For your understanding, that cherry-pick command just above, with 3 dots in it, is exactly equivalent to this longer command:

git cherry-pick $(git merge-base my_branch origin/peer_branch)..origin/peer_branch

The git merge-base my_branch origin/peer_branch part finds the common parent commit hash between branch my_branch and branch origin/peer_branch. This is the commit at which point they forked their peer_branch off your your my_branch. Then, you are, of course, cherry-picking the range of commits from that point to (..) their final commit at origin/peer_branch.

To read more about that 3-dot syntax, see here: What are the differences between double-dot ".." and triple-dot "..." in Git diff commit ranges? [duplicate]. For help on git checkout -b new_branch from_branch, see my answer here: Various ways to create a branch in git from another branch

Official Git documentation

  1. https://git-scm.com/docs/gitrevisions - mentions git commit 3 dot (...) vs 2 dot range syntax, ^commit ("not" commit), commit^ (the parent of commit), etc.

Going further

  1. Something else to know: a git rebase is just a bunch of sequential git cherry-picks. See my other answer here (Who is "us" and who is "them" according to Git?) where I show, among other things, an ASCII drawing I made of how a git rebase works and what it's doing.
  2. What are the differences between double-dot ".." and triple-dot "..." in Git diff commit ranges? [duplicate]
  3. My answer on Various ways to create a branch in git from another branch
  4. My Q&A: In a git merge-style workflow, show only the unique commits someone had in their PR's (Pull Request's) feature branch before merging
淡莣 2024-08-17 02:35:11

最简单的方法是使用 rebaseonto 选项。假设当前在 a 处结束的分支称为 mybranch,并且这是您要将 c-f 移动到的分支。

# checkout mybranch
git checkout mybranch

# reset it to f (currently includes a)
git reset --hard f

# rebase every commit after b and transplant it onto a
git rebase --onto a b

The simplest way to do this is with the onto option to rebase. Suppose that the branch which current finishes at a is called mybranch and this is the branch that you want to move c-f onto.

# checkout mybranch
git checkout mybranch

# reset it to f (currently includes a)
git reset --hard f

# rebase every commit after b and transplant it onto a
git rebase --onto a b
淡淡の花香 2024-08-17 02:35:11

或者所要求的一行:

git rebase --onto a b f

Or the requested one-liner:

git rebase --onto a b f
谜兔 2024-08-17 02:35:11

您可以使用 git rebasegitbranch 的串行组合将一组提交应用到另一个分支。正如 Wolfc 已经发布的那样,第一个命令实际上复制了提交。但是,在将分支名称添加到组的最顶层提交之前,更改不可见。

请在新选项卡中打开图片...

Workflow

以文本形式总结命令:

  1. 使用以下命令将 gitk 作为独立进程打开:gitk --all &
  2. 运行 git rebase --onto ab f 。
  3. 在 gitk 中按 F5。没有任何改变。但没有标记 HEAD
  4. 运行git分支选择
  5. gitk中按F5。新分支及其提交将会出现。

这应该可以澄清一些事情:

  • Commit a 是该组的新根目标。
  • 提交 b 是该组第一次提交之前的提交(不包括)。
  • 提交 f 是该组的最后一次提交(含)。

之后,您可以使用 git checkout 功能 && git reset --hard b 从 feature 分支中删除 cf 的提交。

除了这个答案之外,我还写了一个 博客文章 描述了另一个场景中的命令,应该有助于一般使用它。

You can use a serial combination of git rebase and git branch to apply a group of commits onto another branch. As already posted by wolfc the first command actually copies the commits. However, the change is not visible until you add a branch name to the top most commit of the group.

Please open the picture in a new tab ...

Workflow

To summarize the commands in text form:

  1. Open gitk as a independent process using the command: gitk --all &.
  2. Run git rebase --onto a b f.
  3. Press F5 in gitk. Nothing changes. But no HEAD is marked.
  4. Run git branch selection
  5. Press F5 in gitk. The new branch with its commits appears.

This should clarify things:

  • Commit a is the new root destination of the group.
  • Commit b is the commit before the first commit of the group (exclusive).
  • Commit f is the last commit of the group (inclusive).

Afterwards, you could use git checkout feature && git reset --hard b to delete the commits c till f from the feature branch.

In addition to this answer, I wrote a blog post which describes the commands in another scenario which should help to generally use it.

泛滥成性 2024-08-17 02:35:11

要应用 JB Rainsberger 和 sschaef 的评论来具体回答这个问题...要在此示例中使用樱桃选择范围:

git checkout a
git cherry-pick b..f

git checkout a
git cherry-pick c^..f

To apply J. B. Rainsberger and sschaef's comments to specifically answer the question... To use a cherry-pick range on this example:

git checkout a
git cherry-pick b..f

or

git checkout a
git cherry-pick c^..f
泪之魂 2024-08-17 02:35:11

要从提交 ID分支尖端进行挑选,您可以使用:

git cherry-pick commit_id^..branch_name

To cherry pick from a commit id up to the tip of the branch, you can use:

git cherry-pick commit_id^..branch_name
最笨的告白 2024-08-17 02:35:11
git rev-list --reverse b..f | xargs -n 1 git cherry-pick

git rev-list 打印从分支 bf (反转)的所有修订,以便当每一行(提交哈希)是按顺序传递,它会挑选每一个到当前的 git HEAD 上。即 gitcherry-pick {c 的哈希值}; gitcherry-pick {d 的哈希值}; ...

-- @coderatchet 评论

git rev-list --reverse b..f | xargs -n 1 git cherry-pick

git rev-list prints all revisions from branch b to f (reversed) so that when each line (the commit hash) is passed in order, it will cherry pick each one onto the current git HEAD. i.e. git cherry-pick {hash of c}; git cherry-pick {hash of d}; ...

-- @coderatchet comment

柳若烟 2024-08-17 02:35:11

另一个值得一提的变体是,如果您想要分支中的最后 n 提交,则 ~ 语法可能会很有用:

git cherry-pick some-branch~4..some-branch

在这种情况下,上述命令将选择最后 4 个提交从名为 some-branch 的分支提交(尽管您也可以使用提交哈希来代替分支名称)

Another variant worth mentioning is that if you want the last n commits from a branch, the ~ syntax can be useful:

git cherry-pick some-branch~4..some-branch

In this case, the above command would pick the last 4 commits from a branch called some-branch (though you could also use a commit hash in place of a branch name)

咆哮 2024-08-17 02:35:11

择优挑选多个提交:

签出到您想要择优挑选提交的分支

使用此命令:(通过部分哈希)

git cherry-pick 1e038f10 1e038f11 1e038f12 ...

Cherry-pick multiple commits:

Checkout to your branch where you want to cherry-pick the commits

Use this command: (by partial hash)

git cherry-pick 1e038f10 1e038f11 1e038f12 ...
多像笑话 2024-08-17 02:35:11

实际上,最简单的方法可能是:

  1. 记录两个分支之间的合并基数:MERGE_BASE=$(git merge-basebranch-abranch-b)
  2. 快进或重新基址较旧的分支到较新的分支
  3. 将生成的分支重新设置为自身,从步骤 1 的合并基础开始,并手动删除不需要的提交:

    git rebase ${SAVED_MERGE_BASE} -i
    

    或者,如果只有几个新提交,请跳过步骤 1,只需使用

    git rebase HEAD^^^^^^^ -i
    

    在第一步中,使用足够的 ^ 移过合并基础。

您将在交互式变基中看到类似的内容:

pick 3139276 commit a
pick c1b421d commit b
pick 7204ee5 commit c
pick 6ae9419 commit d
pick 0152077 commit e
pick 2656623 commit f

然后删除行 b (以及您想要的任何其他行)

Actually, the simplest way to do it could be to:

  1. record the merge-base between the two branches: MERGE_BASE=$(git merge-base branch-a branch-b)
  2. fast-forward or rebase the older branch onto the newer branch
  3. rebase the resulting branch onto itself, starting at the merge base from step 1, and manually remove commits that are not desired:

    git rebase ${SAVED_MERGE_BASE} -i
    

    Alternatively, if there are only a few new commits, skip step 1, and simply use

    git rebase HEAD^^^^^^^ -i
    

    in the first step, using enough ^ to move past the merge-base.

You will see something like this in the interactive rebase:

pick 3139276 commit a
pick c1b421d commit b
pick 7204ee5 commit c
pick 6ae9419 commit d
pick 0152077 commit e
pick 2656623 commit f

Then remove lines b (and any others you want)

━╋う一瞬間旳綻放 2024-08-17 02:35:11

我需要优先选择从一个分支到另一个分支的提交,但这里的提交很难理解,希望下面的帮助可以帮助您解决一个简单的问题:下面


要执行的步骤:

  1. 获取 1 个带有名称的提交(“删除姓氏字段”),来自"dev" 分支
  2. 在 "hotfix1" 分支 1 中提交

。从“dev”分支

// Go to "dev" branch
git checkout dev

// Get the commit id (1e2e3e4e1 here)
git log --oneline

    > ...
    > ...
    > 1e2e3e4e1     Remove Last Name field
    > ...
    > ...

2 获取提交详细信息。将提交推送到“hotfix1”分支

// Go to "hotfix1" branch
git checkout hotfix1

// Get the commit (1e2e3e4e1) from "dev" branch to "hotfix1" branch
git cherry-pick 1e2e3e4e1

// verify changes are correct
gitk

// push to "hotfix1" branch
git push

要一次执行多个操作,只需在上面进行 1 处更改,请按顺序给出所有提交 ID:

git cherry-pick 1e2e3e4e1 1e2e3e4e2 1e2e3e4e3


I need cherry pick a commit from one branch to another on priority, but commits here were difficult to understand, hope below helps with a easy one:


Procedure to do below:

  1. Get 1 commit with name ( "Remove Last Name field") , from "dev" branch
  2. Commit it in "hotfix1" branch

1 . Get commit details from "dev" branch

// Go to "dev" branch
git checkout dev

// Get the commit id (1e2e3e4e1 here)
git log --oneline

    > ...
    > ...
    > 1e2e3e4e1     Remove Last Name field
    > ...
    > ...

2 . Push the commit to "hotfix1" branch

// Go to "hotfix1" branch
git checkout hotfix1

// Get the commit (1e2e3e4e1) from "dev" branch to "hotfix1" branch
git cherry-pick 1e2e3e4e1

// verify changes are correct
gitk

// push to "hotfix1" branch
git push

To do multiple at once, only 1 change in the above, give all commit ids in sequence:

git cherry-pick 1e2e3e4e1 1e2e3e4e2 1e2e3e4e3

酷遇一生 2024-08-17 02:35:11

这是一个脚本,您只需告诉脚本樱桃选择的源分支和目标分支以及提交数量,即可连续选择多个提交:

https://gist.github.com/nickboldt/99ac1dc4eb4c9ff003a1effef2eb2d81

从分支中挑选到 master (使用当前分支作为源)

./gcpl.sh -m

:从 6.19.x 分支中选择最新的 5 个提交到 master:

./gcpl.sh -c 5 -s 6.19.x -t master

Here's a script that will allow you to cherry-pick multiple commits in a row simply by telling the script which source and target branches for the cherry picks and the number of commits:

https://gist.github.com/nickboldt/99ac1dc4eb4c9ff003a1effef2eb2d81

To cherry-pick from your branch to master (uses the current branch as source):

./gcpl.sh -m

To cherry-pick the latest 5 commits from your 6.19.x branch to master:

./gcpl.sh -c 5 -s 6.19.x -t master
还给你自由 2024-08-17 02:35:11

或者使用GitHub桌面应用程序

您可以在源分支的历史选项卡中多选提交,然后右键单击以获取选项“Cherry-Pick Selected Commits” 。

Alternatively using GitHub Desktop Application,

You can multi-select commits in the history tab of the source branch, then right-click to get the option "Cherry-Pick Selected Commits".

站稳脚跟 2024-08-17 02:35:11

除了提交之外, 您可以通过管道输入来自标准输入的 SHA 列表

git rev-list --reverse ..main -- path/ | git cherry-pick --stdin 

rev-list 基本上是 git-logplumbing 命令(“丑陋”但快速的表弟)
注意需要--reverse

您可以通过这种方式做更高级的事情,而不仅仅是提交范围。

In addition to commit-ish, you can pipe in a list of SHAs from stdin.

git rev-list --reverse ..main -- path/ | git cherry-pick --stdin 

rev-list is basically the plumbing command (the "ugly" but fast cousin) of git-log
NB that --reverse is needed.

You can do more advanced stuff this way rather than simply a commit range.

指尖上得阳光 2024-08-17 02:35:11

这并不是关于如何挑选多个提交的真正答案,而是一种避免过多合并的方法。我使用 IntelliJ 完成了此操作,但在命令行上也应该很容易完成。

您可以在要从中挑选的第一个提交之前从最后一次提交创建一个临时分支。然后将您需要的所有内容挑选到这个临时分支中。这通常应该很容易,没有很多冲突。

然后压缩所有精选的提交。这会将所有来回更改变成单个更改。

现在将该单个提交合并到您的实际目标分支中。您不必多次解决冲突。

Not really an answer on HOW to cherry pick multiple commits but a way to avoid too much merging. I did this using IntelliJ but it should be easy to do on the command line as well.

You can make a temporary branch from the last commit just before the first one you want to cherry pick from. Then cherry pick everything you need into this temporary branch. This should generally be easy without many conflicts.

Then squash all the cherry picked commits. This will turn all the back and forth changes into single changes.

Now merge that single commit into your actual target branch. You will not have to resolve conflicts more than once.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文