13

I created a git repository on computer and pushed it to my server. 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. 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)?

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:

# 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').

javex
  • 7,198
  • 7
  • 41
  • 60
  • 1
    This seems rather unlikely, since a merge (git pull == git fetch & git merge) will not work if the working copy is not clean (changes have been committed); and untracked files should not be touched by a merge either. Are you sure you didn't do something else? – Nevik Rehnel Dec 06 '12 at 13:52
  • Did you do anything else / what is the output of `git status`? – Chronial Dec 06 '12 at 14:03
  • 1
    I am sorry, I omitted one command (probably the one causing this mess): I added all files before pulling but I did NOT commit them (maybe thats where git got confused?) – javex Dec 06 '12 at 14:03
  • If that’s true then you would have been asked to stash away those changes; `git pull` (or rather the merge) won’t do anything unless index and the working directory is clean. – poke Dec 06 '12 at 14:45
  • That's what I would have assumed as well. I don't know what happend, but I executed the commands above and now the files are gone. At least I was able to retreive the most important stuff via `extundelete`. – javex Dec 06 '12 at 15:03
  • I have just reproduced this issue and edited my commands above accordingly. The above command sequence with two repositories and a server removes the file `b` from the file system. – javex Dec 06 '12 at 15:13
  • While git probably shouldn't do this I think it's happening because you're doing `git init` in the second directory. That's a very strange use case because you have two totally different repositories and you're importing an entirely new history set into the second one. The content of your files may be in the .git/objects directory or in the .git/index file. I'm trying to figure out which git command to use to read them – asm Dec 06 '12 at 16:33
  • This sounds like a bug ([possibly related](http://git.661346.n2.nabble.com/pulling-the-root-commit-overwrites-untracked-files-without-warning-1-7-2-3-td5658622.html)). It's happening because you didn't commit b, so the repository is in a weird state because there is no HEAD. – ellotheth Dec 11 '12 at 05:29
  • Dupe: http://stackoverflow.com/questions/10268940/git-pull-deleted-uncommitted-changes?rq=1# – ellotheth Dec 11 '12 at 15:01

2 Answers2

4

Well, I find another strange thing.

In the help of git pull, there is such a sentence as the following:

"In its default mode, git pull is shorthand for git fetch followed by git merge FETCH_HEAD"

So, I use $git fetch and then $git merge FETCH_HEAD instead of git pull origin master above. Then what amazing, file b is still there.

So, I really don't know what git pull origin master exactly does.

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

that is "git merge finds that it has no valid HEAD and therefore does a hard reset, which obviously overwrites any files already there. "

But I really doubt this, because I can use $git merge FETCH_HEAD successfully.

If you want to find the lost files, you can goto see Git pull deleted uncommitted changes provided by @ellotheth

Community
  • 1
  • 1
pktangyue
  • 8,326
  • 9
  • 48
  • 71
0

I wasn't able to find it anyhow. However, I remembered the file name and just recreated this file with this same name and my VSCode helped me out with the rest.

enter image description here

testing_22
  • 2,340
  • 1
  • 12
  • 28