重写本地先前的提交历史记录,而无需创建新的提交

时间:2018-08-22 07:57:32

标签: git

我有一个本地git存储库,其中包含6个不会被远程推送的提交

af8b643 (tag: v2.0) HEAD@{11}: commit: standard version can work
96a57f9 HEAD@{12}: commit: multiple tries succeed
8b781e5 HEAD@{13}: reset: moving to 8b781e5
51a5f96 HEAD@{14}: commit: stupid version can work
abcdc39 HEAD@{15}: reset: moving to abcdc39
8b781e5 HEAD@{16}: commit: Emply zip to handle multiple try-except
abcdc39 HEAD@{17}: commit (initial): Parse the web front jobs

我想将51a5f96 HEAD@{14}: commit: stupid version can work重写为Original Version to Keep the Naive Tech

我尝试使用git rebase -i HEAD~2来关注https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History和多个答案,

现在,提交历史完全被弄乱了。

5d97cfa (HEAD) HEAD@{0}: commit (amend): Original Version to Keep the Naive Tech
abcdc39 HEAD@{1}: rebase -i (start): checkout HEAD~2
c340589 HEAD@{2}: rebase -i (pick): add tag v2.0
1d23365 HEAD@{3}: rebase -i (reword): Keep the original versiont to keep the naive techs
51a5f96 HEAD@{4}: rebase -i: fast-forward
abcdc39 HEAD@{5}: rebase -i (start): checkout HEAD~2
e392cd4 HEAD@{6}: commit: add tag v2.0
51a5f96 HEAD@{7}: rebase -i (start): checkout 51a5f96
141c0f2 (master) HEAD@{8}: rebase -i (finish): returning to refs/heads/master
141c0f2 (master) HEAD@{9}: rebase -i (start): checkout master
141c0f2 (master) HEAD@{10}: commit (amend): Encapulate multiple tries with function.:
af8b643 (tag: v2.0) HEAD@{11}: commit: standard version can work
96a57f9 HEAD@{12}: commit: multiple tries succeed
8b781e5 HEAD@{13}: reset: moving to 8b781e5
51a5f96 HEAD@{14}: commit: stupid version can work
abcdc39 HEAD@{15}: reset: moving to abcdc39
8b781e5 HEAD@{16}: commit: Emply zip to handle multiple try-except
abcdc39 HEAD@{17}: commit (initial): Parse the web front jobs

又产生了八次提交。

我只是打算更改一个先前的提交,可以直接实现吗?

1 个答案:

答案 0 :(得分:2)

由于git如何存储数据(作为Directed Acyclic Graph,其中提交通过其哈希指向其父级),因此如果不更新所有子提交,就无法更新提交。

  

现在,提交历史完全被弄乱了。

您显示的用途是reflog,而不是提交历史记录。它的目标是跟踪所有已创建的提交(即HEAD连续指向的所有提交),因此您在重新定位期间创建的所有提交都显示在这里...

reflog用于在您弄乱时检索“丢失的”提交。

使用git log显示历史记录。

PS:但是也许您有一个重复的历史记录,因为旧的历史记录(在进行变基之前)由标记ref v2.0处理。您应该决定是否删除该标签,然后在新的历史记录上重新创建它。