以前压缩的提交记录的还原

3
我使用了git rebase -i HEAD~5来打包提交。 然后我注意到其中一个提交必须分开处理。 我尝试使用此手册中的交互模式进行操作: https://git-scm.com/docs/git-rebase#_splitting_commits 命令为git rebase --interactive,但是只有这些内容。
noop

# Rebase 0349ada..0349ada onto 0349ada (1 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#

所以我不明白如何指示拆分的提交


1
我认为这实际上只是 https://dev59.com/72025IYBdhLWcg3wGB0n 的重复。你在 git rebase --interactive 命令中提供任何参数吗? - larsks
git rebase --interactive <要合并的提交 hash> - Mike
2个回答

3

你有两个选择。

第一种是将要拆分的提交设置为 edit,然后在该点进行更改。通常情况下,您需要回退到提交之前,然后再做两个所需提交:

$ git reset HEAD^
$ git add file1 file2 file3
$ git commit
$ git add file4 file5 file6
$ git commit
$ git rebase --continue

当然,真实的例子不太可能是针对独立文件进行编辑提交的,但是在执行了 git reset 后,你回到了被拆分的提交之前的状态,但是 这个提交所带来的修改却在你的工作目录中。你需要根据自己的情况来完成所需的两个提交。

另外一个选择是回到第一次变基之前的状态,并再次进行操作,注意不要犯同样的错误。在变基之前的提交不会立即被垃圾回收,因此你可以简单地重置为相关提交:

git reset --hard <hash of the HEAD commit before the rebase>

我说“简单”,但我意识到在你执行变基之前,你很可能没有记录提交哈希值。不用担心,你可以使用reflog来找到它:

git reflog

这里列出了每个操作时HEAD指向的提交。您应该会看到一堆标记为rebase -i的提交,从这些提交之前的提交中挑选提交哈希值即可。
然后您可以再次执行原始的变基操作,并进行不同的选择。

1
使用 git add -p - o11c
我想补充一点:在我看来,通常最明智的做法是返回到rebase之前的提交,因为否则你将试图重新创建你已经丢弃的信息。请注意,如果在原始rebase之后进行了新的更改,则这些更改需要重新基于新的rebase结果(或者,如果情况变得太复杂,可以重新父指向,因为两个rebase都将以具有相同TREE的提交结束)。 - Mark Adelsberger
如果您没有删除原始分支,则可以使用以下命令:that works in case。否则,您需要进入.git文件夹并查看日志文件夹中的HEADS数据。 - Mike
@Mike 我认为删除分支不会有任何影响 - 提交可能尚未被垃圾回收,应该仍然显示在 reflog 中。 - SpoonMeiser

1
你需要执行某些步骤。
  1. Check the Reflog

    git reflog
    
它将会展示如下输出。
some-sha HEAD@{4}: some message
some-sha HEAD@{5}: another message
some-sha HEAD@{6}: rebase -i (squash): Last commit message (after the temporary commit)
some-sha HEAD@{7}: rebase -i (squash): updating HEAD
some-sha HEAD@{8}: checkout: message some-sha
  1. Reset the rebase action. (Reset to the commit previous to the first rebase action)

    git reset --hard your-picked-sha-value
    

    or

    git reset --hard HEAD@{pickedValue}
    
  2. Rebase again

    git rebase -i HEAD~5
    

reflog返回https://pastebin.com/E13HK90B。对于这个SHA,使用硬重置没有任何变化。 - Mike
这很奇怪,对我来说它正确地显示了sha-id。 - Shubham Khatri
@Mike 这是完整的 reflog 吗?看起来你一直在尝试对单个提交进行多次变基(所有这些变基都没有产生变化)。如果你回退更远,是否能够找到你最初提交和初始变基的引用? - SpoonMeiser

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接