我不清楚git如何恢复工作。例如,我想在头之后提交6次,在中间提交的中间恢复所有更改。
假设其SHA哈希值为56e05fced214c44a37759efa2dfc25a65d8ae98d。那我为什么不能这样做
git revert 56e05fced214c44a37759efa2dfc25a65d8ae98d
我不清楚git如何恢复工作。例如,我想在头之后提交6次,在中间提交的中间恢复所有更改。
假设其SHA哈希值为56e05fced214c44a37759efa2dfc25a65d8ae98d。那我为什么不能这样做
git revert 56e05fced214c44a37759efa2dfc25a65d8ae98d
它将还原所述的提交,即添加与之相反的提交。如果你想签出以前的版本,你可以:
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-revert所做的是创建一个提交,撤销在给定提交中所做的更改,创建一个与给定提交相反(好吧,是互惠)的提交。因此
git revert <SHA-1>
应该做功,也确实做功。
如果你想回退到指定的提交,你可以这样做,因为这部分历史还没有发布,你需要使用git-reset,而不是git-revert:
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
并且它将恢复56e05f到HEAD之间的所有提交(不包括范围的起点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——hard和git reset——soft这两个命令在这里很神奇。第一个改变了工作目录,但是它也改变了head(当前分支)。我们在第二次之前把头修好。