简体   繁体   English

Git在pull上删除了未跟踪的文件

[英]Git deleted untracked files on pull

I created a git repository on computer and pushed it to my server. 我在计算机上创建了一个git存储库并将其推送到我的服务器。 Then I went into a folder on another computer that should be merged with the contents of the repository. 然后我进入另一台应该与存储库内容合并的计算机上的文件夹。

Here are the exact steps I executed (I reproduced it): On the first repository: 以下是我执行的确切步骤(我将其复制):在第一个存储库中:

git init
git remote add origin *repo adress*
git remote update
echo "abc" > a
git add a
git commit -a -m "Intial commit"
git push --set-upstream origin master

On the second one (the one where files get deleted): 在第二个(文件被删除的那个):

git init
echo "def" > b
git add b
git remote add origin *repo adress*
git remote update
git pull origin master

What I expected to happen was that git would pull those files and then I could commit my local files and push it back up. 我期望发生的是git将拉出这些文件然后我可以提交我的本地文件并将其推回。 But instead now my local files are gone. 但现在我的本地文件已经消失了。 Did git really just delete local files without a warning (I didn't use any force option or similar)? git真的只是在没有警告的情况下删除本地文件(我没有使用任何强制选项或类似)?

Is there a possibility to get them back or is this intended and expected behavior to just remove untracked files? 有没有可能让他们回来,或者这是预期和预期的行为,只是删除未跟踪的文件?

Output of just git status says: 只有git status输出说:

# On branch master
nothing to commit, working directory clean

I just reporduced these steps with a test repository and it happens as described: File "a" gets pulled into repository number two, but file "b" is no gone (only a is displayed by 'ls'). 我只是用测试存储库重新定义了这些步骤,它按照描述进行:文件“a”被拉入第二个存储库,但是文件“b”没有消失(只有一个被'ls'显示)。

Well, I find another strange thing. 好吧,我发现了另一件奇怪的事情。

In the help of git pull , there is such a sentence as the following: git pull的帮助下,有如下句子:

"In its default mode, git pull is shorthand for git fetch followed by git merge FETCH_HEAD" “在默认模式下,git pull是git fetch的简写,后跟git merge FETCH_HEAD”

So, I use $git fetch and then $git merge FETCH_HEAD instead of git pull origin master above. 所以,我使用$git fetch然后$git merge FETCH_HEAD而不是上面的git pull origin master Then what amazing, file b is still there. 然后有什么了不起的, 文件b仍然存在。

So, I really don't know what git pull origin master exactly does. 所以,我真的不知道git pull origin master到底做了什么。

Also, I saw an explain in 另外,我看到了一个解释

http://git.661346.n2.nabble.com/pulling-the-root-commit-overwrites-untracked-files-without-warning-1-7-2-3-td5658622.html http://git.661346.n2.nabble.com/pulling-the-root-commit-overwrites-untracked-files-without-warning-1-7-2-3-td5658622.html

that is "git merge finds that it has no valid HEAD and therefore does a hard reset, which obviously overwrites any files already there. " 那就是“git merge发现它没有有效的HEAD,因此会进行硬重置,这显然会覆盖已存在的所有文件。”

But I really doubt this, because I can use $git merge FETCH_HEAD successfully. 但我真的很怀疑这一点,因为我可以成功使用$git merge FETCH_HEAD

If you want to find the lost files, you can goto see Git pull deleted uncommitted changes provided by @ellotheth 如果要查找丢失的文件,可以转到@itotheth提供的Git pull删除未提交的更改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM