在Git中恢复到SHA哈希的提交?

我不清楚git revert是如何工作的。例如,我想恢复到头部后面的六个提交,恢复中间提交中的所有更改。

假设它的SHA哈希是56e05fced214c44a37759efa2dfc25a65d8ae98d。那为什么我不能这样做:

git revert 56e05fced214c44a37759efa2dfc25a65d8ae98d
657355 次浏览

它恢复上述提交,即添加与之相反的提交。如果您想签出早期版本,请执行:

git checkout 56e05fced214c44a37759efa2dfc25a65d8ae98d

如果您想在当前HEAD之上以不同提交的确切状态提交,撤消所有中间提交,那么您可以使用reset创建索引的正确状态以进行提交。

# Reset the index and working tree to the desired tree
# Ensure you have no uncommitted changes that you want to keep
git reset --hard 56e05fced


# Move the branch pointer back to the previous HEAD
git reset --soft "HEAD@{1}"


git commit -m "Revert to 56e05fced"

git恢复所做的是创建一个撤销给定提交中所做更改的提交,创建一个与给定提交相反(嗯,互惠)的提交。因此

git revert <SHA-1>

应该并且正在工作。

如果您想回退到指定的提交,并且您可以这样做,因为这部分历史记录尚未发布,您需要使用git重置,而不是git-恢复:

git reset --hard <SHA-1>

(请注意,--hard会使您丢失工作目录中任何未提交的更改)。

附加说明

顺便说一句,也许这并不明显,但无论留档说<commit><commit-ish>(或<object>),您都可以将SHA-1标识符(完整或缩短)提交。

这更容易理解:

git checkout 56e05fced -- .
git add .
git commit -m 'Revert to 56e05fced'

为了证明它是有效的:

git diff 56e05fced

如果您的更改已经推送到公共、共享远程,并且您想恢复HEAD<sha-id>之间的所有提交,那么您可以将提交范围传递给git revert

git revert 56e05f..HEAD

它将恢复56e05fHEAD之间的所有提交(不包括范围的起点56e05f)。

这可能会奏效:

git checkout 56e05f
echo ref: refs/heads/master > .git/HEAD
git commit

回滚到特定提交的最佳方法是:

git reset --hard <commit-id>

然后:

git push <reponame> -f

应该像这样简单:

git reset --hard 56e05f

这会让你回到那个特定的时间点。

更新:

如果中间没有合并提交,这个答案提供了一个更简单的方法:https://stackoverflow.com/a/21718540/541862

但是,如果有一个或多个合并提交,则该答案将不起作用,因此请坚持这个(在所有情况下都有效)。

原答复:

# Create a backup of master branch
git branch backup_master


# Point master to '56e05fce' and
# make working directory the same with '56e05fce'
git reset --hard 56e05fce


# Point master back to 'backup_master' and
# leave working directory the same with '56e05fce'.
git reset --soft backup_master


# Now working directory is the same '56e05fce' and
# master points to the original revision. Then we create a commit.
git commit -a -m "Revert to 56e05fce"


# Delete unused branch
git branch -d backup_master

git reset --hardgit reset --soft这两个命令在这里很神奇。第一个更改了工作目录,但它也更改了head(当前分支)。我们用第二个固定头。