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

mar*_*bik 5 git

我正在尝试重现由于我未回答问题而导致的问题

简而言之

github用户尝试执行此操作git pull --rebase,该用户的本地文件被删除。我试图在github上重新创建此方案,但就我而言,什么也没有删除。

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

细节

第1部分:

  • 一个用户询问如何将本地更改同步到新创建的远程存储库。
  • git push 失败,因为用户没有远程更改(按预期方式)。
  • 我建议使用git pull --rebase,然后解决,然后git push再次使用。
  • 但是我的建议导致删除了用户的本地文件。(请参阅原始问题
  • 请注意,已跟踪文件(在本地提交),因此用户能够还原它们。

第2部分:

  • 我创建了一个名为回购的https://github.com/user/myrepo,自述和初始化的.gitignore
  • 在本地,我做了:

    本地初始化回购

    > mkdir myrepo
    > cd myrepo
    
    > echo AAAAA > fileA.txt
    > echo BBBBB > fileB.txt
    > echo ignoreme > .gitignore
    
    > git init
    
    Run Code Online (Sandbox Code Playgroud)

    在本地提交文件

    > git add .
    > git commit -m"initial commit"
    
    Run Code Online (Sandbox Code Playgroud)

    试图拉远程更改

    > git remote add origin https://github.com/user/myrepo.git
    > git branch --set-upstream-to=origin/master master
    
    > git pull --rebase
    
    Run Code Online (Sandbox Code Playgroud)
  • 结果是(按预期):

    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".
    
    Run Code Online (Sandbox Code Playgroud)
  • 此时,如果我这样做了ls,我的本地文件仍在我的文件系统上。

    > ls -a
    .          ..         .git       .gitignore README.md  fileA.txt  fileB.txt
    
    Run Code Online (Sandbox Code Playgroud)

更新:

这是有问题的原始用户的评论。

来自user7342807:

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

git init
git add .
git commit -m "initial commit"
Run Code Online (Sandbox Code Playgroud)

在这一点上,我意识到我已经为它创建了github页面,其中包含默认自述文件。

因此,我不知道这会是一个问题,所以我试图推动

git remote add origin http://github.com/user/repo.git
git push -u origin master
Run Code Online (Sandbox Code Playgroud)

这是我收到消息的时间:

$ 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.
Run Code Online (Sandbox Code Playgroud)

因此,我没有强迫推送和擦除github上的README文件,而是git pull --rebasegit向我显示了此消息

First, rewinding head to replay your work on top of it...
Run Code Online (Sandbox Code Playgroud)

我等了大约5分钟,我之前ctrl+C出的过程中,实现了大约有8-9从WordPress站点删除的文件。 git status还向我展示了已删除的文件。

我能够恢复使用这些文件git reflog,这些文件显示了我在HEAD @ 1上的首次提交,并且使用git reset --hard "HEAD@{5}"


是什么原因导致文件从另一个用户的本地文件系统中删除?

请注意,有一个类似的问题,询问在这种情况下如何取消删除文件。因此,发生这种情况,并且人们正在丢失其文件。

Kri*_*ofe 0

git pull将删除另一个路径上曾经跟踪的文件。

请参阅为什么 git rm --cached 不删除本地曾经跟踪的文件,而是删除其他文件