簡體   English   中英

Git 使用樹哈希恢復到先前的提交作為新提交

[英]Git revert to previous commit as a new commit using tree hashes

有沒有辦法向 HEAD 添加與現有提交具有相同樹哈希的新提交? 基本上是什么回滾到公共存儲庫中的舊 Git 提交要求,但使用git commit-tree而不是git revertgit reset --hard

好的,經過一些嘗試,我想出了這個腳本:

#/bin/bash
COMMITID=$1
git reset --hard $(git commit-tree -m "Revert to commit $COMMITID" -p $(git rev-parse HEAD) $(git rev-parse $COMMITID^{tree}))

這將從我們想要恢復到的提交中獲取樹哈希,然后創建一個新的提交消息,指定當前 HEAD 作為父提交,然后將我們的分支重置為該新提交。

這將刪除任何未暫存或未提交的更改,因此也許我們可以使用git reset --soft代替。

編輯:也非常感謝@RomainValeri,他為命令提供了全局別名:

git config --global alias.reset-by-commit-tree '!f() { git reset --hard $(git commit-tree -m "Revert to commit $1" -p $(git rev-parse HEAD) $(git rev-parse $1^{tree})); }; f'

運行后,您可以使用git reset-by-commit-tree <sha>恢復到特定提交。

你可以使用git reset --soft

git checkout --detach revision-with-the-desired-tree
git reset --soft the-branch
# if you like the result
git branch -f the-branch
git checkout the-branch

暫無
暫無

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

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