如何将某些提交移动到基于 git 中的另一个分支?

发布于 2024-08-24 04:32:21 字数 683 浏览 2 评论 0原文

情况:

  • master 在 X 处,
  • quickfix1 在 X + 2 次提交处

这样:

o-o-X (master HEAD)
     \
      q1a--q1b (quickfix1 HEAD)

然后我开始在quickfix2上工作,但不小心将quickfix1作为要复制的源分支,而不是master。现在,quickfix2 处于 X + 2 次提交 + 2 次相关提交。

o-o-X (master HEAD)
     \
      q1a--q1b (quickfix1 HEAD)
              \
               q2a--q2b (quickfix2 HEAD)

现在我想要一个带有quickfix2的分支,但没有属于quickfix1的2个提交。

      q2a'--q2b' (quickfix2 HEAD)
     /
o-o-X (master HEAD)
     \ 
      q1a--q1b (quickfix1 HEAD)

我尝试从 QuickFix2 中的某个修订版创建补丁,但该补丁不保留提交历史记录。有没有办法保存我的提交历史记录,但在quickfix1中有一个没有更改的分支?

The situation:

  • master is at X
  • quickfix1 is at X + 2 commits

Such that:

o-o-X (master HEAD)
     \
      q1a--q1b (quickfix1 HEAD)

Then I started working on quickfix2, but by accident took quickfix1 as the source branch to copy, not the master. Now quickfix2 is at X + 2 commits + 2 relevant commits.

o-o-X (master HEAD)
     \
      q1a--q1b (quickfix1 HEAD)
              \
               q2a--q2b (quickfix2 HEAD)

Now I want to have a branch with quickfix2, but without the 2 commits that belong to quickfix1.

      q2a'--q2b' (quickfix2 HEAD)
     /
o-o-X (master HEAD)
     \ 
      q1a--q1b (quickfix1 HEAD)

I tried to create a patch from a certain revision in quickfix2, but the patch doesn't preserve the commit history. Is there a way to save my commit history, but have a branch without changes in quickfix1?

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

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

发布评论

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

评论(5

握住我的手 2024-08-31 04:32:21

这是rebase --onto

 # let's go to current master (X, where quickfix2 should begin)
 git checkout master

 # replay every commit *after* quickfix1 up to quickfix2 HEAD.
 git rebase --onto master quickfix1 quickfix2 

所以你应该从到

o-o-X (master HEAD)
     \ 
      q1a--q1b (quickfix1 HEAD)
              \
               q2a--q2b (quickfix2 HEAD)

      q2a'--q2b' (new quickfix2 HEAD)
     /
o-o-X (master HEAD)
     \ 
      q1a--q1b (quickfix1 HEAD)

这最好在干净的工作树上完成。
请参阅git config --global rebase.autostash true,尤其是Git 2.10 之后

This is a classic case of rebase --onto:

 # let's go to current master (X, where quickfix2 should begin)
 git checkout master

 # replay every commit *after* quickfix1 up to quickfix2 HEAD.
 git rebase --onto master quickfix1 quickfix2 

So you should go from

o-o-X (master HEAD)
     \ 
      q1a--q1b (quickfix1 HEAD)
              \
               q2a--q2b (quickfix2 HEAD)

to:

      q2a'--q2b' (new quickfix2 HEAD)
     /
o-o-X (master HEAD)
     \ 
      q1a--q1b (quickfix1 HEAD)

This is best done on a clean working tree.
See git config --global rebase.autostash true, especially after Git 2.10.

为你拒绝所有暧昧 2024-08-31 04:32:21

您可以使用 gitcherry-pick 来选择要复制的提交。

也许最好的方法是从 master 中创建分支,然后在该分支中对您想要的来自 QuickFix2 的 2 次提交使用 gitcherry-pick 。

You can use git cherry-pick to just pick the commit that you want to copy over.

Probably the best way is to create the branch out of master, then in that branch use git cherry-pick on the 2 commits from quickfix2 that you want.

_蜘蛛 2024-08-31 04:32:21

您能做的最简单的事情就是挑选一个范围。它的作用与 rebase --onto 相同,但更容易观察:)

git cherry-pick quickfix1..quickfix2

The simplest thing you can do is cherry picking a range. It does the same as the rebase --onto but is easier for the eyes :)

git cherry-pick quickfix1..quickfix2
_失温 2024-08-31 04:32:21

我相信是:

git checkout master
git checkout -b good_quickfix2
git cherry-pick quickfix2^
git cherry-pick quickfix2

I believe it's:

git checkout master
git checkout -b good_quickfix2
git cherry-pick quickfix2^
git cherry-pick quickfix2
烟雨凡馨 2024-08-31 04:32:21
// on your branch that holds the commit you want to pass
$ git log
// copy the commit hash found
$ git checkout [branch that will copy the commit]
$ git reset --hard [hash of the commit you want to copy from the other branch]
// remove the [brackets]

其他更有用的命令和解释:Git 指南

// on your branch that holds the commit you want to pass
$ git log
// copy the commit hash found
$ git checkout [branch that will copy the commit]
$ git reset --hard [hash of the commit you want to copy from the other branch]
// remove the [brackets]

Other more useful commands here with explanation: Git Guide

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