什么会让git在git pull--rebase期间删除本地文件?

什么会让git在git pull--rebase期间删除本地文件?,git,Git,我试图重现我试图回答的问题 简言之 一个github用户试图执行git pull--rebase,该用户的本地文件被删除。我曾尝试在github上重新创建此场景,但在我的情况下,没有删除任何内容 那么,我如何重现git pull具有破坏性的场景呢 详细信息 第1部分: 用户询问如何将本地更改同步到新创建的远程存储库 git push失败,因为用户没有远程更改(如预期的那样) 我建议使用git pull--rebase,然后解析,然后再次使用git push 但我的建议导致用户的本地文件被删除。

我试图重现我试图回答的问题

简言之

一个github用户试图执行
git pull--rebase
,该用户的本地文件被删除。我曾尝试在github上重新创建此场景,但在我的情况下,没有删除任何内容

那么,我如何重现git pull具有破坏性的场景呢

详细信息

第1部分:

  • 用户询问如何将本地更改同步到新创建的远程存储库
  • git push
    失败,因为用户没有远程更改(如预期的那样)
  • 我建议使用
    git pull--rebase
    ,然后解析,然后再次使用
    git push
  • 但我的建议导致用户的本地文件被删除。 (见附件)
  • 请注意,文件已被跟踪(在本地提交),因此用户可以恢复它们
第2部分:

  • 我创建了一个名为https://github.com/user/myrepo的repo,用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
    
  • 结果是(如预期的那样):

  • 此时,如果我执行了
    ls
    ,本地文件仍在文件系统中

    > ls -a
    .          ..         .git       .gitignore README.md  fileA.txt  fileB.txt
    

更新:

下面是有问题的原始用户的评论

从用户7342807:

。。。我今天早上安装了wordpress,并做到了这一点

git init
git add .
git commit -m "initial commit"
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 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.
$ 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上的自述文件,而是 git拉了--rebase,git向我显示了这条消息

First, rewinding head to replay your work on top of it...
First, rewinding head to replay your work on top of it...
我等了大约5分钟,才
ctrl+C
退出流程,然后 意识到从wordpress站点删除了大约8-9个文件。
git status
还向我显示了删除的文件

我能够使用git reflog恢复这些文件 我的第一次承诺是HEAD@1我用git reset恢复了我的文件 --硬“HEAD@{5}”


是什么导致文件从其他用户的本地文件系统中删除的


请注意,出现这种情况时,有一个问题询问如何取消删除文件。所以,这种情况发生了,人们正在丢失他们的文件。

我不能评论,因为回购率低,但我今天早上安装了wordpress,并做到了这一点

git init
git add .
git commit -m "initial commit"
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 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.
$ 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上强制推送和删除自述文件,而是执行了git pull--rebase,git向我展示了这条消息

First, rewinding head to replay your work on top of it...
First, rewinding head to replay your work on top of it...
我等了大约5分钟,然后才离开进程,意识到从wordpress站点中删除了大约8-9个文件
git status
还向我显示了删除的文件


我能够使用git reflog恢复这些文件,这显示了我在HEAD@1我用git reset恢复了我的文件——硬“HEAD@{5}”

git pull
将删除另一路径上的跟踪文件


请参阅

事件是否删除了未跟踪的文件?我问,因为Git有多种合并策略,既然您现在正处于合并冲突的中间,如果Git认为处理冲突的最佳方法是尝试合并另一种方式,那么它可能只是简单地签出了来自远程本地的变更集,并试图合并到这一点上。这样,文件就好像被删除了,但实际上你仍然处在合并冲突的中间。此外,rebase一次只回放一个变更集,这可能解释了这一点。这些是跟踪文件。他们已经犯了罪。我将更新问题。@LasseV.Karlsen创建一个可能导致git使用此反向合并策略的文件将很有趣。。。我也明白你关于rebase replay的观点,但在本例中,我相信只有一个提交是由github repo上带有README+.gitignore的git init生成的。如果git签出不同的变更集,它根本不会删除任何文件,它们仍然存在于提交历史中。是的,它可以并且将从您的工作目录中删除它们,但不会从提交中删除它们。但是,如果移动头引用或分支引用被中断,您可能会得到除通过reflog之外无法访问的本地提交。这不是真正的答案,而是:(1)通用标准建议:避免
git pull
(2)您正在合并不相关的历史记录,在最新的Git中,哪个需要
——在使用
Git merge
时允许不相关的历史记录
;(3) 我们需要确切地知道上游存储库是如何创建的(GitHub允许您创建空的或非空的)以及您的本地Git版本;(4) 这可能是一个Windows Git特有的bug。嗨,我不能为此悬赏。我把你的答案复制到我的问题中。。。您介意在此时删除您的答案吗?
git reflog
git reset--hard“HEAD@{…}”
解救了我的文件,谢谢!