58

I have cloned a Git project into a local Git repository. Then I have done something nasty to one of the files and in that panic I deleted file physically from the drive (rm style.css) and also removed it from Git (git rm style.css).

I want to get the original style.css file back from origin to my development branch. Unfortunately my Git thinks it is up-to-date and won't do anything.

cd ~/project.me
git status

# On branch dev
nothing to commit (working directory clean)

git pull origin dev

Password for 'https://someone@github.com':
From https://github.com/somewhere/project.me
* branch            dev        -> FETCH_HEAD
Already up-to-date.

What do I need to do to tell git that I want to download original style.css file back into my dev branch?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
feronovak
  • 2,687
  • 6
  • 35
  • 54
  • if you want to restore but you are on a specific commit or branch check out this answer: https://stackoverflow.com/questions/16803904/git-pull-doesnt-actually-restore-my-missing-files-from-remote – Charlie Parker Sep 16 '22 at 19:40
  • fyi sometimes the files are not fully updated for some mysterious reason. I found out that going to the root of the git project and running `git checkout .` fixed that issues. – Charlie Parker Sep 16 '22 at 20:30

4 Answers4

84

If you want to restore all the missing files from the local repository

git checkout .

Warning: This method also restores all changed files and drops all the changes

aleskva
  • 1,644
  • 2
  • 21
  • 40
neoneye
  • 50,398
  • 25
  • 166
  • 151
83

Use git checkout. In your case:

git checkout origin/master style.css

This command will update the requested file from the given branch (here the remote branch origin/master).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CJlano
  • 1,532
  • 1
  • 12
  • 18
  • 1
    Thanks - this helped me too. If you have lots of files to restore (as I did) you can use: `git diff --name-status | sed -n '/^D/ s/^D\s*//gp' | xargs git checkout origin/master` which will re-checkout all the deleted files. – spikyjt Jan 31 '14 at 10:37
  • your answer is not general enough to help anyone. Perhaps make it more useful to everyone besides the OP? – Charlie Parker Jan 25 '22 at 19:22
13

Go to the location of file style.css (may be app/css/) using the console. Otherwise there will be pathspec error.

Then execute:

git checkout origin/master style.css
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
prime
  • 14,464
  • 14
  • 99
  • 131
1

I intentionally deleted some files from a clone --depth 1.

This brought the main files, and submodule ones, back:

git checkout . -f && git submodule update --checkout -f 
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
noabody
  • 179
  • 4