英文:
How to rebase diverged Git branches in chronological order?
问题
我正在为您翻译以下内容:
我之前在我的dev
分支上工作,然后进行了一些提交(A
,B
,C
)。在某个时刻,由于某种原因,我切换到了main
分支,对之前我所工作的同一文件的不同部分进行了编辑(这就是为什么我没有注意到我在不同的分支上),然后提交了D
...
* (4th) D (HEAD -> main)
|
| * (3rd) C (dev)
| * (2nd) B
| * (1st) A
| /
|/
* x
* y
* z
最直接的方法是以它们被创建的顺序将A
,B
,C
和D
放在一个分支上(无论是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
命令将提交D
从main
分支移动到dev
分支,按照以下步骤进行操作:
-
找到并复制提交
D
的哈希值 -
git checkout dev
-
git cherry-pick <D_COMMIT_HASH>
-
解决冲突
-
git add <AFFECTED_FILES>
-
git cherry-pick --continue
在这个阶段,提交历史看起来像下面的图形,
* (4th) D (main) | | * (new) C_MERGED_WITH_D (HEAD -> dev) | * (3rd) C | * (2nd) B | * (1st) A | / |/ * x * y * z
-
git checkout main
-
git reset --hard HEAD~1
英文:
Use git cherry-pick
to move commit D
from main
to dev
by doing the following:
-
Find and copy the commit hash of
D
-
git checkout dev
-
git cherry-pick <D_COMMIT_HASH>
-
Resolve the conflicts
-
git add <AFFECTED_FILES>
-
git cherry-pick --continue
> At this stage, the commit history looks like the graph below,
>
>text
> * (4th) D (main)
> |
> | * (new) C_MERGED_WITH_D (HEAD -> dev)
> | * (3rd) C
> | * (2nd) B
> | * (1st) A
> | /
> |/
> * x
> * y
> * z
>
>
> because "technically you do not move the commit, but create a new one with the same content, andgit
remembers this copy when merging", so: -
git checkout main
-
git reset --hard HEAD~1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论