首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >git:获取樱桃精选的`git rebase --interactive`的好处

git:获取樱桃精选的`git rebase --interactive`的好处
EN

Stack Overflow用户
提问于 2014-06-06 11:51:39
回答 3查看 5.9K关注 0票数 22

我希望能够做到这一点:

代码语言:javascript
复制
git cherry-pick --interactive hash-0..hash-n-1  # fantasy command

并获得与交互式rebase相同的工作流程:出现一个编辑器缓冲区,其中包含:

代码语言:javascript
复制
pick hash-0
pick hash-1
pick hash-2
...
pick hash-n-1

在那里我可以删除任何不需要的提交,将它们squash在一起,或者使用edit在选择之间暂停,以执行一些手动修复(如commit --amend)等等。

请注意交互式rebase的pick是如何与cherry-pick相似的。

现在可以先执行cherry-pick,然后执行交互式rebase,这是不方便的。这就是:

代码语言:javascript
复制
$ git tag old-head  # mark starting point for later rebase
$ git cherry-pick hash-0..hash-n-1    # get everything first
$ git rebase --interactive old-head   # okay now rebase "in-branch" to fix it up

这不仅因为这两个步骤而不方便,而且因为它可能需要解决提交中的冲突,您甚至不希望这些冲突在rebase阶段被丢弃。

EN

回答 3

Stack Overflow用户

发布于 2014-06-06 13:17:00

好的,想出了一个好办法。

在当前分支中的一次提交上启动一个简单的rebase --interactive HEAD^。你会得到类似这样的结果:

代码语言:javascript
复制
pick 1efd396b * Fixed a bug in frob function

现在,只需粘贴您想要挑选的其他散列:

代码语言:javascript
复制
pick 1efd396b * Fixed a bug in frob function
pick f01934db * Awesome feature added
pick 6fd109c1 * Refactored the widgets layer
squash 3900fd77 * Refactored the widgets layer s'more

保存并退出,然后wee:rebase mule很乐意接受您加载到它背上的额外cruft,并根据命令将其合并到当前分支中。

实际上,您可以使用以下命令执行一个空的rebase:

代码语言:javascript
复制
git rebase --interactive HEAD

你会得到一个缓冲区,其中包含

代码语言:javascript
复制
noop

你不必删除它;只需在它之后添加你的选择即可。

附录:要生成此方法的选择列表,请使用git log --oneline --reverse from..to,然后修剪所需的输出,并将rebase命令预先添加到每一行:picksquash、...

票数 27
EN

Stack Overflow用户

发布于 2014-06-06 13:00:52

好的,根据卡尔·诺鲁姆的建议,我研究了git rebase--onto争论。

用例可以像这样得到满足,这是一个改进,尽管仍然涉及到一些过多的步骤。

主要的问题是rebase需要一个当前的分支来挑选,所以我们必须将我们的坐标转移到一个临时分支,然后在之后操作我们的原始分支头部并删除临时分支。

流程如下所示:

代码语言:javascript
复制
# We are originally on "mybranch"
# We create a temp-branch pointing to the last commit to be picked: hash-n-1

$ git checkout -b temp-branch hash-n-1   # create branch at last hash

# Then we rebase from the point before the first hash, onto our original branch

$ git rebase --interactive hash-0^ --onto mybranch

# The above rebase should make "temp-branch" into the very object that
# we want "mybranch" to be. If it looks that way, then all that is left is
# make it so:

$ git checkout mybranch          
$ git reset --hard temp-branch
$ git branch -D temp-branch

git rebase --interactive hash-0^ --onto mybranch使用hash-0之前的提交作为rebase的“上游”,从当前分支(基于hash-n-1)获取不在上游的所有提交。当然,这些提交是通过hash-n-1hash-0。它们被重新定位到mybranch头上,但跟踪结果的是当前的temp-branch,它是reset --hard。所以我们只需要将指针赋值给mybranch并删除temp-branch

它相当笨拙,但消除了重复的挑剔,并且只需使用git reset --hard mybranch就可以随时恢复。

(这还能改进吗?)

票数 4
EN

Stack Overflow用户

发布于 2014-06-06 12:07:01

也许有一个更好的答案,但这可能对你有效。

代码语言:javascript
复制
git cherry | awk '$0=$2' > cherry.txt
"$EDITOR" cherry.txt
git cherry-pick --stdin < cherry.txt

Example

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24073883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档