Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git deleted untracked files on pull

Tags:

git

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

like image 546
javex Avatar asked Dec 06 '12 13:12

javex


People also ask

Does git pull remove untracked files?

Because the clean operation permanently deletes files, Git wants you to first run the command with the -n option to force a dry run. Remember that 'git clean' only removes untracked files. To find out if a file is tracked or not, developers can issue the 'git status' command from within the repository.

What happens to untracked files when git pull?

git pull doesn't delete uncommitted changes, it doesn't deal with your working directory, any changes or new files will not be touched unless it committed.

Is it still possible to restore deleted untracked files in git?

Summary. Once the untracked files are deleted, they cannot be restored. Before running the git clean command, perform dry run to know what the are files that will be deleted.

What command removes untracked?

You can use the git clean command to remove untracked files. The -fd command removes untracked directories and the git clean -fx command removes ignored and non-ignored files. You can remove untracked files using a . gitignore file.


1 Answers

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

like image 80
pktangyue Avatar answered Oct 11 '22 00:10

pktangyue