Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I just made a simple script and created a new repository for it on GitHub. After initializing the local repo, moving the script to the repository and pulling the GitHub repository (containing only a README.md file), the script was gone. Is this normal behavior? And is there some way to restore the file?

Here are the commands, as executed:

$ mkgithub ~/dev/cr2meta2jpg
Initialized empty Git repository in /home/username/dev/cr2meta2jpg/.git/
$ mv test.sh ~/dev/cr2meta2jpg/cr2meta2jpg.sh
$ cd ~/dev/cr2meta2jpg/
$ ls
cr2meta2jpg.sh
$ git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From github.com:l0b0/cr2meta2jpg
 * [new branch]      master     -> origin/master
$ ls
README.md
share|improve this question
add comment

2 Answers 2

up vote 4 down vote accepted

A git pull will not overwrite local changes unless you use git add before. Even in this case, you can still recover your data.

When you issued git add before the pull:

mkgithub blub
cp test.sh blub/
cd blub/
git add test.sh
git pull

The pull will delete test.sh. It will only do this if you have no commit in the local repo. After adding a commit this won't remove test.sh (git either ignores it or reports a merge conflict)

The file is not lost. It's still in the Git repository as a dangling blob.

To recover the file:

$ git fsck
Checking object directories: 100% (256/256), done.
Checking objects: 100% (401/401), done.
dangling blob 541060d8292ff808c9715265d063f1bae220da7c
$ git show 541060d8292ff808c9715265d063f1bae220da7 > test.sh

This works as long as you did not issue git gc --prune=now afterwards.

share|improve this answer
2  
You can also issue git fsck --lost-found. This will save the dangling blobs to .git/lost-found/other. Either way, the file names are lost. The filenames will be the hashes of the objects. –  JonnyJD Dec 18 '12 at 13:43
add comment

I tried making a new repository with a github remote:

$ mkgithub ~/dev/cr2meta2jpg
Initialized empty Git repository in /home/username/dev/cr2meta2jpg/.git/

I stuck a file in the folder without tracking it:

$ mv test.sh ~/dev/cr2meta2jpg/cr2meta2jpg.sh
$ cd ~/dev/cr2meta2jpg/
$ ls
cr2meta2jpg.sh

And then tried to pull from github:

$ git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From github.com:l0b0/cr2meta2jpg
 * [new branch]      master     -> origin/master
$ ls
README.md

cr2meta2jpg.sh was deleted, so it looks like the answer is yes.

share|improve this answer
    
Only a copy of code from your shell is not really an answer. Can you maybe at some more info? –  Bernhard Mar 28 '13 at 9:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.