如何按时间顺序重新基于分叉的 Git 分支?

huangapple go评论111阅读模式
英文:

How to rebase diverged Git branches in chronological order?

问题

我正在为您翻译以下内容:

我之前在我的dev分支上工作,然后进行了一些提交(ABC)。在某个时刻,由于某种原因,我切换到了main分支,对之前我所工作的同一文件的不同部分进行了编辑(这就是为什么我没有注意到我在不同的分支上),然后提交了D...

* (4th) D (HEAD -> main)
|
|  * (3rd) C (dev)
|  * (2nd) B
|  * (1st) A
| /
|/
* x
* y
* z

最直接的方法是以它们被创建的顺序将ABCD放在一个分支上(无论是main还是dev)上。

英文:

I was working on my dev branch, then made some commits (A, B, C). I switched to the main branch for some reason at this point, did edits on different parts of the same files I worked on before (the reason why I didn't notice that I'm on a different branch), then commited D...

* (4th) D (HEAD -> main)
|
|  * (3rd) C (dev)
|  * (2nd) B
|  * (1st) A
| /
|/
* x
* y
* z

What is the most straightforward way to have A, B, C, and D on one branch (doesn't matter if it's main or 'dev`) in the same order they have been created?

答案1

得分: 1

像这样:

git switch main # 从 D 开始
git branch temp # 给 D 一个名字
git reset --hard @~1 # 将 main 指向 x
git rebase --onto dev main temp # 将 D 复制到 C 之后
git branch -M dev # 将 D 重命名为 dev

结果:


   * (第四个) D (HEAD -> dev)
   * (第三个) C
   * (第二个) B
   * (第一个) A
  /
 /
* x -> main
* y
* z

<details>
<summary>英文:</summary>

Like this:

    git switch main # start at D
    git branch temp # give D a name
    git reset --hard @~1 # point main to x
    git rebase --onto dev main temp # copy D to after C
    git branch -M dev # rename D dev

Result:

  • (4th) D (HEAD -> dev)
  • (3rd) C
  • (2nd) B
  • (1st) A
    /
    /
  • x -> main
  • y
  • z

答案2

得分: 1

main 分支中,你只需要运行 git rebase dev 命令。这将会将 main 分支中所有不在 dev 分支中的提交(根据你的图示,只有 D)重新应用到 dev 分支的最新提交上。

这样,你的 main 分支就会处于你想要的状态,而 dev 分支则不会有任何改变。

英文:

From main you can just run git rebase dev. That will replay everything you have in main which isn't also in dev (just D according to your diagram) onto the tip of dev.

Your main branch will then be in the state you want and your dev branch will be unchanged.

答案3

得分: 0

使用git cherry-pick命令将提交Dmain分支移动到dev分支,按照以下步骤进行操作:

  1. 找到并复制提交D的哈希值

  2. git checkout dev

  3. git cherry-pick <D_COMMIT_HASH>

  4. 解决冲突

  5. git add <AFFECTED_FILES>

  6. git cherry-pick --continue

    在这个阶段,提交历史看起来像下面的图形,

    * (4th) D (main)
    |
    |  * (new) C_MERGED_WITH_D (HEAD -> dev)
    |  * (3rd) C 
    |  * (2nd) B
    |  * (1st) A
    | /
    |/
    * x
    * y
    * z
    

    因为“从技术上讲,你并没有移动提交,而是创建了一个具有相同内容的新提交,并且git在合并时记住了这个副本”,所以:

  7. git checkout main

  8. git reset --hard HEAD~1

英文:

Use git cherry-pick to move commit D from main to dev by doing the following:

  1. Find and copy the commit hash of D

  2. git checkout dev

  3. git cherry-pick &lt;D_COMMIT_HASH&gt;

  4. Resolve the conflicts

  5. git add &lt;AFFECTED_FILES&gt;

  6. git cherry-pick --continue

    > At this stage, the commit history looks like the graph below,
    >
    > text
    &gt; * (4th) D (main)
    &gt; |
    &gt; | * (new) C_MERGED_WITH_D (HEAD -&gt; dev)
    &gt; | * (3rd) C
    &gt; | * (2nd) B
    &gt; | * (1st) A
    &gt; | /
    &gt; |/
    &gt; * x
    &gt; * y
    &gt; * z
    &gt;

    >
    > because "technically you do not move the commit, but create a new one with the same content, and git remembers this copy when merging", so:

  7. git checkout main

  8. git reset --hard HEAD~1

huangapple
  • 本文由 发表于 2023年8月9日 08:03:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76863803.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定