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

标签 git

我正在尝试重现因尝试回答 this question 而导致的问题.

简而言之

一个 github 用户尝试执行 git pull --rebase 并且该用户的本地文件被删除。我试图在 github 上重新创建这个场景,但在我的例子中没有任何内容被删除。

那么如何重现git pull破坏性的场景呢?

详情

第 1 部分:

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

第 2 部分:

  • 我创建了一个名为 https://github.com/user/myrepo 的 repo,使用 README 和 .gitignore 进行了初始化
  • 在本地,我做了:

    本地初始化repo

    > mkdir myrepo
    > cd myrepo
    
    > echo AAAAA > fileA.txt
    > echo BBBBB > fileB.txt
    > echo ignoreme > .gitignore
    
    > git init
    

    本地提交文件

    > git add .
    > git commit -m"initial commit"
    

    试图 pull 远程更改

    > 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
    

更新:

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

来自用户 7342807:

... I installed wordpress this morning and did exactly this.

git init
git add .
git commit -m "initial commit"

at this point, I had realized I already created the github page for it, WITH a default readme in it.

So, not knowing this would be an issue, I tried to push as

git remote add origin http://github.com/user/repo.git
git push -u origin master

This is when I received the message:

$ 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.

So, instead of force pushing and erasing that README file on github, I did git pull --rebase and git showed me this message

First, rewinding head to replay your work on top of it...

I waited about 5 minutes, before I ctrl+C out of the process, and realized there were about 8-9 files removed from the wordpress site. git status also showed me the removed files.

I was able to recover those files using git reflog which showed me my first commit on HEAD@1 and I recovered my files with git reset --hard "HEAD@{5}"


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

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

最佳答案

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

引用Why git rm --cached not remove local ever tracked file but others

关于git - 什么会让 git 在 git pull --rebase 期间删除本地文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42625566/

相关文章:

git - rebase 结果与 merge 结果有何不同?

git - 卡在 git commit 窗口

git - 当目录已经移动到不同目录时如何在git中 merge 文件

git - 如何在保留子目录的同时拆分 git 存储库?

git - 我们可以跳过关于 "Adding your SSH key to the ssh-agent"的部分,只生成 key 并添加到 GitHub 吗?

git - GitHub 中的 fork 与分支

git - grep 用于多个 git 存储库中的内容

mysql - Spring Petclinic (Spring Boot) 中的数据库连接问题

git - 创建 git repo 并将本地目录推送到新初始化的 Azure Repos

git - 如何配置 visual studio 2017 以使用外部 difftool 和 mergetool