簡體   English   中英

如何在保存提交歷史記錄的同時還原git repo?

[英]How to revert git repo while preserving the commit history?

這是我所擁有的:

dbd7868 (HEAD -> master) commit 3
19ea7e8 commit 2
5b4baae commit 1

我希望所有文件都與commit 2處於完全相同的狀態。 這是我嘗試過的:

git checkout 19ea7e8
git commit -m "reverted to commit 2"

但這是結果:

HEAD detached at 19ea7e8
nothing to commit, working tree clean

所需結果:

xxxxxxx (HEAD -> master) reverted to commit 2
dbd7868 commit 3
19ea7e8 commit 2
5b4baae commit 1

我嘗試環顧四周,但是找到了命令將歷史記錄commit 3排除在外的答案,然后我發現了不清楚是否保留歷史記錄的答案。


編輯:嘗試了建議的解決方案之一后,我不得不再次設置測試項目。 這是我現在所擁有的:

9c4180f (HEAD -> master) jkl
38029d0 ghi
830efcd def
ae96f00 abc

這就是我想要的:

xxxxxxx (HEAD -> master) def again
9c4180f jkl
38029d0 ghi
830efcd def
ae96f00 abc

def again提交def again應該與def完全相同。 為了澄清,我希望能夠進行任何提交。

您可以使用命令git revert <hash1> <hash2> ..

嘗試以下命令:

git revert dbd7868

它將創建一個新的提交,該提交將是提交3的還原。

如果要還原任何其他提交,只需使用commits哈希運行revert命令。 例如:

git revert 5b4baae #to revert commit 1
git revert 19ea7e8 #to revert commit 2

在所有情況下,還原提交都將在提交3之上。

感謝John Ilacqua,我找到了解決方案。

這是我輸入的確切命令:

git checkout 830efcd .
git commit -am "revert def"

這是輸出:

684b0bb (HEAD -> master) revert def
9c4180f jkl
38029d0 ghi
830efcd def
ae96f00 abc

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM