【问题标题】:git history screwed upgit历史搞砸了
【发布时间】:2021-01-25 01:01:53
【问题描述】:

我试图做 rebase 以将提交合并为一个提交,但我最终编写了更多提交,现在这一切都搞砸了。

这是树: 在运行git status 时,我得到了

On branch master
Your branch is up to date with 'origin/master'.

Last commands done (5 commands done):
   pick 49147e5 squash
   pick 2c48387 Commit 
  (see more in file .git/rebase-merge/done)
Next commands to do (13 remaining commands):
   pick fddd4c7 commit 
   pick 5f01f80 commit 
  (use "git rebase --edit-todo" to view and edit)
You are currently editing a commit while rebasing branch 'master' on '1550461'.
  (use "git commit --amend" to amend the current commit)
  (use "git rebase --continue" once you are satisfied with your changes)

nothing to commit, working tree clean

在运行git log --oneline 时,我得到:

Merge branch 'master' of .....
49147e5 squash
0310431 commit 4 which was PR
d4be53f commit 6
ea24a2d commit 5
1550461 commit 3
fc8115a commit 2
212138a commit 1
72d8fff Merge pull request #64 from branch 1
c5ad199 commit 14
2a6ceaf commit 13
6660dbf commit 12
b6c863e commit 11
88788e6 commit 10
5bbe8a7 commit 9
9731810 commit 8
1e4ac6b commit 7
ce3817c Merge branch 'master' of .... into branch 1
ec4c3e9 commit 6
5b8900b commit 5
d10eec9 commit 4 which was PR
5f01f80 commit 3
fddd4c7 commit 2
02ebbc8 Merge branch 'master' of ....into branch 1
3995cf2 pr changes v2
2c48387 commit 1

我是怎么搞砸的: 从72d8fff,我做了git rebase -i HEAD~7 or 8git pull,然后是git push。我想将squashd10eec9 之后的所有提交合并为一个提交,但我想我最终写在 HEAD 之上。

我想先做git reset --hard 72d8fff 然后git push origin HEAD --force 这至少会让我回到我开始的地方。
请保存。

【问题讨论】:

    标签: git rebase squash git-squash


    【解决方案1】:

    带我回到我开始的地方。

    在本地,你可以看看git reflog,它会列出分支的提示和其他参考在本地存储库中的更新。

    您会在此处找到 HEAD 在您的变基之前 引用的内容。
    如果你 reset --hard 提交,你将回到 rebase 之前。
    您还可以将该提交与(如果您尚未进行任何其他变基)ORIG_HEAD and reset on that commit 进行比较。

    这个想法仍然存在:首先在本地恢复您的历史记录,然后再次压缩您的最后一次提交(这次是without rebase, but using git reset --soft),并在本地存储库的状态令人满意时强制推送。

    【讨论】:

    • 我在不同的分支上尝试了rebase 进行试用,这就是为什么看到git reflog 很烦人的原因。因此,我想在git reset --soft HEAD~9之后简单地做git reset --hard 72d8fff [这里我很困惑是否应该做HEAD~8]然后git commit然后git push -f。注意这是主分支所以我很害怕。
    • @amyJ 先本地试试:结果正确,就可以push了。如果您不确定,请不要推动。
    • 好的,所以当我执行git reset --hard 72d8fff 时,我会从我开始rebase 的地方进入上一个提交。现在,当我执行git reset --soft HEAD~1 时,来自[5b8900b, 72d8fff] 的所有提交都消失了。这可能是因为我在功能分支上进行了这些更改,然后我合并到 master 并且 git 指向之前的 HEAD。当我做git commit -v 时,变化看起来很好。我唯一担心的是在我做任何事情之前我应该​​先中止rebase吗?问题中发布的消息仍然存在
    • 是的,在任何重置之前,我会首先中止当前的 rebase,或者作为最后的手段退出它 (stackoverflow.com/a/41363262/6309)
    猜你喜欢
    • 2020-02-15
    • 2013-10-28
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多