我运行了git状态,它告诉我一切都是最新的,没有本地更改。
然后我做了几次连续的改变,意识到我想扔掉一切,回到原来的状态。这个命令能帮我吗?
git reset --hard HEAD
我运行了git状态,它告诉我一切都是最新的,没有本地更改。
然后我做了几次连续的改变,意识到我想扔掉一切,回到原来的状态。这个命令能帮我吗?
git reset --hard HEAD
当前回答
如果您只想删除所有更改,请执行git签出。这是一种更快更简单的方法。
其他回答
在此处添加另一个选项。
我指的是标题:还原本地更改。它还可以应用于未准备提交的更改。
在这种情况下,您可以使用:
git restore <file>
返回到以前的状态。
两个简单步骤
git fetch origin
git reset --hard origin/master
或者如果您的git使用main而不是master,请使用以下命令:
git reset --hard origin/main
如果您想恢复所有更改并更新当前远程主机(例如,您发现主机HEAD已向前移动,因为您已将其分支,并且您的推送被“拒绝”),您可以使用
git fetch # will fetch the latest changes on the remote
git reset --hard origin/master # will set your local branch to match the representation of the remote just pulled down.
简单地执行-
git stash
它将删除所有本地更改。您也可以稍后通过执行-
git stash apply
查看git-relog。它将列出它记住的所有状态(默认为30天),您只需签出您想要的状态即可。例如:
$ git init > /dev/null
$ touch a
$ git add .
$ git commit -m"Add file a" > /dev/null
$ echo 'foo' >> a
$ git commit -a -m"Append foo to a" > /dev/null
$ for i in b c d e; do echo $i >>a; git commit -a -m"Append $i to a" ;done > /dev/null
$ git reset --hard HEAD^^ > /dev/null
$ cat a
foo
b
c
$ git reflog
145c322 HEAD@{0}: HEAD^^: updating HEAD
ae7c2b3 HEAD@{1}: commit: Append e to a
fdf2c5e HEAD@{2}: commit: Append d to a
145c322 HEAD@{3}: commit: Append c to a
363e22a HEAD@{4}: commit: Append b to a
fa26c43 HEAD@{5}: commit: Append foo to a
0a392a5 HEAD@{6}: commit (initial): Add file a
$ git reset --hard HEAD@{2}
HEAD is now at fdf2c5e Append d to a
$ cat a
foo
b
c
d