Dick Colt
Dick Colt

Reputation: 1555

Git - deleted some files locally, how do I get them from a remote repository

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

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

Upvotes: 150

Views: 189570

Answers (10)

Toru
Toru

Reputation: 905

If the removal was not yet committed: Identify removed files with

git status

and then restore them from local index by

git restore <file>

("To discard changes in working directory" as documentation says).

Upvotes: 0

Mr.7
Mr.7

Reputation: 2713

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>

Upvotes: 5

smcs
smcs

Reputation: 2004

In the code directory: git checkout .

Upvotes: 6

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.

Upvotes: 3

rzskhr
rzskhr

Reputation: 991

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.

Upvotes: 34

Mona Wade
Mona Wade

Reputation: 192

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!!

Upvotes: 1

Amit
Amit

Reputation: 670

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

$ git checkout -f HEAD

Upvotes: 28

Cascabel
Cascabel

Reputation: 496722

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.

Upvotes: 196

Šimon T&#243;th
Šimon T&#243;th

Reputation: 36423

git checkout filename

git reset --hard might do the trick as well

Upvotes: 51

user229044
user229044

Reputation: 239230

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

Upvotes: 3

Related Questions