我正在尝试重现我尝试回答所导致的问题这个问题.
In short
github 用户尝试这样做git pull --rebase
并且该用户的本地文件已被删除。我尝试在 github 上重新创建这个场景,但在我的例子中没有删除任何内容。
那么如何重现 git pull 具有破坏性的场景呢?
细节
Part 1:
- 用户询问如何将本地更改同步到新创建的远程存储库。
-
git push
失败,因为用户没有远程更改(如预期)。
- 我建议使用
git pull --rebase
,然后求解,然后git push
again.
- 但我的建议导致用户的本地文件被删除。
(看原问题)
- 请注意,文件已被跟踪(在本地提交),因此用户能够恢复它们。
Part 2:
- 我创建了一个名为 https://github.com/user/myrepo 的存储库,使用 README 和 .gitignore 进行初始化
-
在本地,我做了:
本地初始化存储库
> mkdir myrepo
> cd myrepo
> echo AAAAA > fileA.txt
> echo BBBBB > fileB.txt
> echo ignoreme > .gitignore
> git init
本地提交文件
> git add .
> git commit -m"initial commit"
尝试拉取远程更改
> git remote add origin https://github.com/user/myrepo.git
> git branch --set-upstream-to=origin/master master
> git pull --rebase
-
结果是(如预期):
First, rewinding head to replay your work on top of it...
Applying: initial commit
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
Auto-merging .gitignore
CONFLICT (add/add): Merge conflict in .gitignore
error: Failed to merge in the changes.
Patch failed at 0001 initial commit
The copy of the patch that failed is found in: .git/rebase-apply/patch
When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".
-
此时,如果我这样做ls
,我的本地文件仍在我的文件系统上。
> ls -a
. .. .git .gitignore README.md fileA.txt fileB.txt
Update:
这是遇到问题的原始用户的评论。
来自用户7342807:
...我今天早上安装了 wordpress 并做到了这一点。
git init
git add .
git commit -m "initial commit"
此时,我意识到我已经创建了 github 页面
它,里面有一个默认的自述文件。
所以,不知道这会是一个问题,我试图推动
git remote add origin http://github.com/user/repo.git
git push -u origin master
这是我收到消息的时候:
$ git push -u origin master
To https://github.com/user/project.com
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/user/project.com'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
因此,我没有强制推送并删除 github 上的 README 文件,而是
做过git pull --rebase
git 向我展示了这条消息
First, rewinding head to replay your work on top of it...
我等了大约5分钟,才ctrl+C
过程之外,并且
意识到大约有 8-9 个文件从 WordPress 站点中删除。git status
还向我展示了删除的文件。
我能够使用恢复这些文件git reflog
这向我展示了
我在 HEAD@1 上的第一次提交,并使用以下命令恢复了我的文件git reset
--hard "HEAD@{5}"
是什么导致文件从其他用户的本地文件系统中删除?
请注意,有一个类似的问题询问发生这种情况时如何取消删除文件。所以这种情况发生了,人们正在丢失他们的文件。
git pull
将删除另一个路径上曾经跟踪的文件。
参考为什么 git rm --cached 不删除本地曾经跟踪的文件而是其他文件
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)