Git pull 删除未提交的更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10268940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git pull deleted uncommitted changes
提问by Eric
I just made a new repository on github. Starting with a folder full of files, the steps I did were:
我刚刚在 github 上创建了一个新的存储库。从一个装满文件的文件夹开始,我做的步骤是:
git init
git add -A
git remote add origin ...
#Now pull in the first commit that github made
git pull origin master
#Check everything is OK
ls
Eek! All my files have disappeared! What happened? Can I get them back?
哎呀!我所有的文件都不见了!发生了什么?我可以让他们回来吗?
回答by jthill
You can get them back. Even though the only thing pointing to it was the index, git add
still put the added content in the repo. I'd start with a git fsck
to find "dangling" (git's slightly quirky spelling of "unreferenced") blobs and git cat-file -p
those blobs, if there's too many I'd do something like find .git/objects -type f | xargs ls -lt
.
你可以让他们回来。尽管唯一指向它的是索引,git add
但仍将添加的内容放在 repo 中。我会从 agit fsck
开始找到“悬空”(git 的“未引用”拼写有点古怪)blob 和git cat-file -p
那些 blob,如果有太多我会做类似find .git/objects -type f | xargs ls -lt
.
回答by Xavier
I agree with the accepted answer, however in my case there were too many results for git fsck
. This solution is what helped me locate the lost files:
我同意接受的答案,但是就我而言,git fsck
. 这个解决方案帮助我找到丢失的文件:
Search for a string in the missing file(s):
在丢失的文件中搜索字符串:
grep -rin <string_in_missing_file> .git/
For example:
例如:
grep -rin MyClassName .git/
Search results:
搜索结果:
.git//lost-found/other/3cfaa36226f52a5c1b38c2d2da3912656c998826:5:class MyClassName extends ParentClass
.git//lost-found/other/e7b8923de4afb230ad523b5b515f50cb9c624248:5:class MyClassName extends ParentClass
Where search results are:
搜索结果在哪里:
.git/<path_to_file>:<line_number_of_found_string>:<found_string_and_context>
Then to restore the file:
然后恢复文件:
git cat-file -p 3cfaa36226f52a5c1b38c2d2da3912656c998826 > ../<desired_file_path>/MyClassName.php
回答by qwerty001
I did the similar thing When I was trying to push my commit and was getting hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again.
我做了类似的事情当我试图推送我的提交并收到提示时:更新被拒绝,因为您当前分支的提示在提示后面:它的远程副本。在再次推送之前集成远程更改(例如提示:'git pull ...')。
I tried to revert my commits by
我试图通过以下方式恢复我的提交
git reset HEAD~3 .
git stash
git pull
git stash pop
git push
and accidentally pulled again and push the changes. All my work for 7 days was lost.
并不小心再次拉动并推动更改。我 7 天的所有工作都丢失了。
This worked for me to get all of my work back in local branch:
这对我有用,可以让我在本地分支中完成所有工作:
git reset --hard HEAD@{"15 minutes ago"}
git reset --hard HEAD@{"15 minutes ago"}
回答by user3518317
For me the following worked:
对我来说,以下工作有效:
- Go to the affected file.
- Right click and choose Local Historyand select Show History. From there you can see the history of changes made to the file, and then choose the time you want to bring back the file to.
- You will get two windows with the left one having >>. Click on all the >>and this will send your changes to the right window.
- Close the window and here you go with your file restored to where you wanted it to be.
- 转到受影响的文件。
- 右键单击并选择Local History并选择Show History。从那里您可以看到对文件所做更改的历史记录,然后选择要恢复文件的时间。
- 你会得到两个窗口,左边一个有>>。单击所有>>,这会将您的更改发送到右侧窗口。
- 关闭窗口,然后您将文件恢复到您想要的位置。
I hope it helped!
我希望它有帮助!
回答by ralphtheninja
Since you never committed the files, no sorry. The steps you need to take are:
由于您从未提交过这些文件,因此很抱歉。您需要采取的步骤是:
git init
git add .
git commit -m 'Initial commit'
git remote add origin ...
git push origin master
Remember, when in doubt, always commit. As long as you do that, you can always undo stuff with git.
请记住,当有疑问时,请始终承诺。只要你这样做,你总是可以用 git 撤消东西。