Git 重基在终止时创建新的提交

Git 重基在终止时创建新的提交,git,git-rebase,Git,Git Rebase,当我这样做时: git checkout -b branch1 git commit -m "A" git checkout -b branch2 git commit -m "B" -- oups did errors when committed in branch1 git checkout branch1 git commit --amend --no-edit git checkout branch2 git rebase branch1 -- resolve conflicts gi

当我这样做时:

git checkout -b branch1
git commit -m "A"
git checkout -b branch2
git commit -m "B"
-- oups did errors when committed in branch1
git checkout branch1
git commit --amend --no-edit
git checkout branch2
git rebase branch1
-- resolve conflicts
git checkout --theirs file
git rebase --continue
现在我仍然看到第一次提交(A->A->B)


什么是正确的命令使用,所以我没有提交

你的问题的答案会很长,因为你给了我们很多命令,而这些命令下面有很多东西

首先,您开始了第三个分支,由于没有更好的名称,我将其称为
start

git checkout start
git checkout -b branch1
git commit -m "A"
git checkout -b branch2
git commit -m "B"
这些命令留给您的
branch1
branch2
如下所示:

branch1: M <- A
branch2: M <- A <- B
branch1: M <- A'
branch2: M <- A' <- B
剩下的是:

branch1: M <- A'
branch2: M <- A <- B
这里,
A'
commit实际上是原始的
A
commit(在您修改它之前),而
A'
commit是您修改它之后的
A
commit

git checkout branch2
git rebase branch1

branch1: M <- A'
branch2: M <- A' <- A'' <- B
你需要决定你想在这里做什么。您可以将两个
A
提交压缩为一个,您可以只保留
A'
,您可以只保留
A'
,也可以同时保留两者。在任何情况下,我在这里推荐的工具都是交互式模式下的
git-rebase

因此,您可以这样做,而不是执行正常的重基:

git checkout branch2
git rebase -i branch1
这将显示提交列表,您可以选择要对每个提交执行的操作:

pick 9361ckv Some earlier commit M from the start branch
pick 62eed47 Amended commit A
pick 39dne92 Original commit A
pick k3nos9s Commit B
关键字
pick
表示每个提交都将被保留。如果仔细检查提交列表,您将看到它从最早到最新列出了在原始重新基址之后当前出现在
branch2
中的提交。这不是你想要的。假设您要删除原始的
A
commit(上面标记为
A'
),那么您将删除包含此commit的行,留下以下列表:

pick 9361ckv Some earlier commit M from the start branch
pick 62eed47 Amended commit A
pick k3nos9s Commit B
现在,当您完成重基时,您的分支将如下所示:

branch1: M <- A
branch2: M <- A <- B
branch1: M <- A'
branch2: M <- A' <- B

branch1:M将所有提交添加到单个提交中。