我不清楚git如何恢复工作。例如,我想在头之后提交6次,在中间提交的中间恢复所有更改。
假设其SHA哈希值为56e05fced214c44a37759efa2dfc25a65d8ae98d。那我为什么不能这样做
git revert 56e05fced214c44a37759efa2dfc25a65d8ae98d
我不清楚git如何恢复工作。例如,我想在头之后提交6次,在中间提交的中间恢复所有更改。
假设其SHA哈希值为56e05fced214c44a37759efa2dfc25a65d8ae98d。那我为什么不能这样做
git revert 56e05fced214c44a37759efa2dfc25a65d8ae98d
当前回答
更新:
如果中间没有合并提交,这个答案提供了一个更简单的方法: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——hard和git reset——soft这两个命令在这里很神奇。第一个改变了工作目录,但是它也改变了head(当前分支)。我们在第二次之前把头修好。
其他回答
它将还原所述的提交,即添加与之相反的提交。如果你想签出以前的版本,你可以:
git checkout 56e05fced214c44a37759efa2dfc25a65d8ae98d
回滚到特定提交的最好方法是:
git reset --hard <commit-id>
然后:
git push <reponame> -f
这比较容易理解:
git checkout 56e05fced -- .
git add .
git commit -m 'Revert to 56e05fced'
为了证明它有效:
git diff 56e05fced
如果您希望在不同的提交中使用准确的状态在当前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 reset --hard 56e05f
它会让你回到那个特定的时间点。