Git使用树形哈希将其恢复为先前的提交,作为新的提交 [英] Git revert to previous commit as a new commit using tree hashes
问题描述
是否可以将新的提交添加到与现有提交具有相同树哈希的HEAD?基本上回滚到公共回购中的旧Git提交正在询问,但是使用git commit-tree
而不是git revert
或git reset --hard
吗?
Is there a way to add a new commit to HEAD which has the same tree hash as an existing commit? Basically what Rollback to an old Git commit in a public repo is asking, but using git commit-tree
instead of git revert
or git reset --hard
?
推荐答案
好的,经过一番尝试,我想到了这个脚本:
OK, after some trying I came up with this script:
#/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指定为父提交,然后将我们的分支重置为该新提交.
This will get the tree hash from the commit we want to revert to, then create a new commit message specifying current HEAD as the parent commit, and then reset our branch to that new commit.
这将删除所有未暂存或未提交的更改,因此也许我们可以改用git reset --soft
.
This would delete any unstaged or uncommitted changes so maybe we could use git reset --soft
instead.
非常感谢@RomainValeri,他为命令提供了全局别名:
Also many thanks to @RomainValeri, who provided a global alias for the command:
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>
恢复为特定的提交.
After running it, you can use git reset-by-commit-tree <sha>
to revert to a particular commit.
这篇关于Git使用树形哈希将其恢复为先前的提交,作为新的提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!