我在使用时遇到一些问题--keep-empty
的选项git rebase
,而我是
不确定我是否误解了此选项的作用,或者存在错误。
这是一个最小的例子:
Setup
-
创建一个新的 Git 存储库和一个初始的、不相关的提交。
$ git init
$ echo something >base.txt
$ git add base.txt
$ git commit -m 'some base commit to not run into the root corner case'
-
创建一个新提交,添加两个新文件。
$ echo A >a.txt; echo B >b.txt
$ git add a.txt b.txt
$ git commit -m 'add A and B'
-
修改其中一个文件。
$ echo A1 >a.txt
$ git add a.txt
$ git commit -m 'change A'
-
修改另一个文件。
$ echo B1 >b.txt
$ git add b.txt
$ git commit -m 'change B'
Rebase
$ git checkout -b rebased master
$ git rebase --keep-empty -i :/base
……选择edit
提交在哪里A
and B
添加,并更改它以便仅B
添加(在实际场景中,原因可能是A
是保密的):
$ git rm a.txt
$ git commit --amend
$ git rebase --continue
当然,下一次提交是在哪里A
现在修改会产生冲突:
error: could not apply 182aaa1... change A
When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".
Could not apply 182aaa1701ad100fc02a5d5500cacebdd317a24b... change A
...选择不添加修改版本a.txt
:
$ git mergetool
Merging:
a.txt
Deleted merge conflict for 'a.txt':
{local}: deleted
{remote}: modified file
Use (m)odified or (d)eleted file, or (a)bort? d
提交在哪里A
修改后现在为空:
$ git diff --cached
# nothing
…并完成变基:
$ git rebase --continue
Successfully rebased and updated refs/heads/rebased.
Question
所以现在我的历史有两个版本,区别在于没有任何痕迹A
在其中之一。然而,因为我选择了--keep-empty
选项,我仍然希望存在一个空提交rebased
,这会告诉我A
如果它在那里的话,就会被修改。
但显然,情况并非如此:
$ git log --oneline master
f893569 change B
182aaa1 change A
3340b71 add A and B
38cb5da some base commit to not run into the root corner case
$ git log --oneline rebased
73a2c05 change B
55f502b add A and B
38cb5da some base commit to not run into the root corner case
这不是么--keep-empty
应该做,或者不起作用
正确吗?
有关的:在根上重新建立基础并保留空提交 https://stackoverflow.com/questions/34529723/rebase-on-the-root-and-keep-empty-commits是一个非常相似的问题,但它涉及--root
我在这里明确避免了极端情况。它没有答案,只有一些评论表明我在这里展示的内容应该有效。另一个区别是,在另一个问题中,提交首先是空的,而这里只有在解决冲突后才变成空。