145

I've deleted some files on my PC, how do I download them again?

Pull says: "Already up-to-date".

NAND
  • 663
  • 8
  • 22
Dick Colt
  • 1,505
  • 2
  • 10
  • 6

9 Answers9

191

Since git is a distributed VCS, your local repository contains all of the information. No downloading is necessary; you just need to extract the content you want from the repo at your fingertips.

If you haven't committed the deletion, just check out the files from your current commit:

git checkout HEAD <path>

If you have committed the deletion, you need to check out the files from a commit that has them. Presumably it would be the previous commit:

git checkout HEAD^ <path>

but if it's n commits ago, use HEAD~n, or simply fire up gitk, find the SHA1 of the appropriate commit, and paste it in.

Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • 2
    just as a side comment, this also works for a full directory, its not special or specific to a single file (it worked when I tried it at least). Thnx btw. – Charlie Parker Feb 29 '16 at 16:53
  • 6
    N.B. the `` is mandatory, even if just `.` dot for current dir. But the "branch" (`HEAD` in the above example) is optional. As with so many Git commands, neither the output nor help is helpful. – MarkHu Nov 17 '17 at 08:27
49

git checkout filename

git reset --hard might do the trick as well

Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
29

If you deleted multiple files locally and did not commit the changes, go to your local repository path, open the git shell and type.

$ git checkout HEAD .

All the deleted files before the last commit will be recovered.

Adding "." will recover all the deleted the files in the current repository, to their respective paths.

For more details checkout the documentation.

rzskhr
  • 931
  • 11
  • 10
27

If you have deleted multiple files locally but not committed, you can force checkout

$ git checkout -f HEAD
Amit
  • 660
  • 6
  • 8
  • 1
    As we see from the output "Already up-to-date", the person asking this question has not committed anything lately. Should you maybe specify that git checkout -f HEAD is DANGEROUS? As it might undo local changes that were not commited (restoring previous files being the positive outcome, losing changes on edited files the negative one). – Lionel Trebuchon Oct 23 '17 at 12:42
  • Keep in mind that this command will overwrite all your local changes. – lazy.lizard May 21 '21 at 13:45
3

You need to check out a previous version from before you deleted the files. Try git checkout HEAD^ to checkout the last revision.

user229044
  • 232,980
  • 40
  • 330
  • 338
3

This is a rather niche use-case (although funnily enough answers the question as stated exactly), but just in case anyone ever needs this: if you do git filter-branch removing a file along with its history and then you want to recover just the latest versions of the removed files, git checkout filename won't be enough (because the file is no longer in the local history of the repository) and you need to specify that you want to reset to the remote version using git checkout origin/main -- filename.

Tomáš Hübelbauer
  • 9,179
  • 14
  • 63
  • 125
3

If your deleted file(s) is already staged, git checkout <file> doesn't work.

You have to unstage first and then do checkout

To unstage

git restore --staged <file>

and then do checkout

git checkout <file>
Mr.7
  • 2,292
  • 1
  • 20
  • 33
2

In the code directory: git checkout .

smcs
  • 1,772
  • 3
  • 18
  • 48
1

Also, I add to do the following steps so that the git repo would be correctly linked with the IDE:

 $ git reset <commit #>

 $ git checkout <file/path>

I hope this was helpful!!

Alexan
  • 8,165
  • 14
  • 74
  • 101
Mona Wade
  • 192
  • 1
  • 16