我不清楚git如何恢复工作。例如,我想在头之后提交6次,在中间提交的中间恢复所有更改。

假设其SHA哈希值为56e05fced214c44a37759efa2dfc25a65d8ae98d。那我为什么不能这样做

git revert 56e05fced214c44a37759efa2dfc25a65d8ae98d

当前回答

应该像这样简单:

git reset --hard 56e05f

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

其他回答

这比较容易理解:

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

为了证明它有效:

git diff 56e05fced

如果你的修改已经被推送到一个公共共享的远程,并且你想要还原HEAD和<sha-id>之间的所有提交,那么你可以传递一个提交范围给git revert,

git revert 56e05f..HEAD

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

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

git reset --hard <commit-id>

然后:

git push <reponame> -f

这可能有用:

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

如果您希望在不同的提交中使用准确的状态在当前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"