我运行了git状态,它告诉我一切都是最新的,没有本地更改。
然后我做了几次连续的改变,意识到我想扔掉一切,回到原来的状态。这个命令能帮我吗?
git reset --hard HEAD
我运行了git状态,它告诉我一切都是最新的,没有本地更改。
然后我做了几次连续的改变,意识到我想扔掉一切,回到原来的状态。这个命令能帮我吗?
git reset --hard HEAD
当前回答
查看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
其他回答
如果您在顶级项目目录中,请尝试以下操作:
git restore .
如果没有,则使用:
git restore :/
如果要还原子集的本地更改:
使用top pathspec magic还原所有工作树文件:git restore:/还原当前目录中的所有文件:gitrestore。文件类型(例如所有C源文件):git restore“*.C”
有关详细信息,请参阅git restore文档。
要删除未跟踪的文件:gitclean-f
如果您想恢复所有更改并更新当前远程主机(例如,您发现主机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.
其他答案似乎太复杂了,所以这里有一个简单的解决方案对我有效:
如何放弃所有本地更改并将文件重置为当前分支上的最后一次提交?
# Execute on the root of the working tree...
# Discard all changes to tracked files
git checkout .
# Remove all untracked files
git clean -fd
注意:忽略的文件将不受影响
注意:您可能还想运行
git clean -fd
as
git reset --hard
不会删除未跟踪的文件,因为gitclean将从跟踪的根目录中删除不受git跟踪的任何文件。警告-小心!首先使用git clean运行一次试运行,看看它会删除什么。
当您收到错误消息时,这也特别有用
~"performing this command will cause an un-tracked file to be overwritten"
当你和你的朋友都添加了一个同名的新文件,但他先将其提交到源代码管理中,而你不在乎删除未跟踪的副本时,这可能会发生在做几件事时,其中一件事就是更新工作副本。
在这种情况下,干运行也将帮助您显示将被覆盖的文件列表。
尝试此操作以还原本地分支中未提交的所有更改
$ git reset --hard HEAD
但如果您看到这样的错误:
fatal: Unable to create '/directory/for/your/project/.git/index.lock': File exists.
您可以导航到“.git”文件夹,然后删除index.lock文件:
$ cd /directory/for/your/project/.git/
$ rm index.lock
最后,再次运行命令:
$ git reset --hard HEAD