我有两个分支。提交a是一个的头,而另一个有b, c, d, e和f在a的顶部。我想移动c, d, e和f到第一个分支,而不提交b。使用樱桃采摘很容易:签出第一个分支樱桃采摘一个接一个c到f,并重新建立第二个分支到第一个。但是有没有办法在一个命令中选择所有的c-f ?

下面是场景的可视化描述(感谢JJD):


当前回答


我需要樱桃选择从一个分支到另一个优先级的提交,但这里的提交很难理解,希望下面的帮助与一个简单的:


步骤如下:

从“dev”分支获取1个带名字的提交(“移除姓氏字段”) 在"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

如果要一次做多个,只需要在上面修改一个,按顺序给出所有的提交id:

git cherry-pick 1e2e3e4e1 1e2e3e4e2 1e2e3e4e3

其他回答

另一个值得一提的变体是,如果你想要一个分支的最后n次提交,~语法可以很有用:

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

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

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

记录两个分支之间的merge-base: MERGE_BASE=$(git merge-base branch-a branch-b) 快进或将旧的分支重置到新的分支上 从步骤1的merge base开始,将生成的分支重新基于自身,并手动删除不需要的提交: ${SAVED_MERGE_BASE} -i 或者,如果只有几个新的提交,则跳过第1步,直接使用 git rebase HEAD^^^^^^^ -i 在第一步中,使用足够的^来移动合并基础。

你会在交互的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

然后删除行b(和任何其他你想要的)

除了提交之外,还可以通过管道从stdin输入sha列表。

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

Rev-list基本上是git-log的管道命令(“丑陋”但快速的表亲) 注意——反向是需要的。

您可以通过这种方式执行更高级的操作,而不仅仅是提交范围。


我需要樱桃选择从一个分支到另一个优先级的提交,但这里的提交很难理解,希望下面的帮助与一个简单的:


步骤如下:

从“dev”分支获取1个带名字的提交(“移除姓氏字段”) 在"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

如果要一次做多个,只需要在上面修改一个,按顺序给出所有的提交id:

git cherry-pick 1e2e3e4e1 1e2e3e4e2 1e2e3e4e3

最简单的方法是使用on选项来调整基数。假设电流在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