我想使用交互式rebase来编辑先前的提交,但是当我进入该提交的编辑模式时,所有文件都已提交。
我知道我可以进行更改并修改提交但我希望最初未提交所有更改(暂存或其他方式),以便我可以编辑它,就像它最初提交之前的时刻一样。这可能吗?
答案 0 :(得分:2)
想象一下git rebase --interactive
会给您以下列表:
pick 3d15626 first
pick 7911b8b second
pick 60d94b4 third
,现在您要编辑第二个未提交状态的提交。为此,请更改其行,如下所示:
pick 3d15626 first
exec git cherry-pick --no-commit 7911b8b && false
pick 60d94b4 third
此行的作用:
git cherry-pick
而不是选择提交git cherry-pick
将对索引和工作树应用“第二”提交(注意使用相同的提交哈希),预加载提交消息,但不提交(--no-commit
)&& false
通过返回失败的退出代码来确保执行的命令“失败”。这会迫使git rebase
在此行之后中断,以使您“解决”问题,即您现在可以编辑提交git commit
。感谢git cherry-pick
git rebase --continue
答案 1 :(得分:1)
您至少可以重置为上一次提交,添加所有内容并提交:然后您可以再次设置提交消息。
git reset @~
git add .
git commit --interactive
或实际上只是git commit
,因为您已添加了所需的内容
那仍然会打开一个编辑器,让你再次输入提交信息。
答案 2 :(得分:1)
@VonC答案的扩展,并提供以下情况的示例:
mkdir git_example
cd git_example
git init
echo first > first
git add first
git commit -m "initial"
git tag initial
echo "How do I see this change while editing in interacive rebase" > second
git add second
git commit -m "second"
echo third > third
git add third
git commit -m "third"
git rebase -i initial
e 66127f1 second
pick 70c0b50 third
git reset HEAD~
git add .
git commit
# NOT commit --amend
# The commit message will be empty, that's ok
git rebase --continue
对一件让我有些困惑的事情露骨:
重置后执行commit
不执行commit --amend
,否则将破坏初始提交。