1

I created a repository on github. My initial repository content are as follows:

D:\github\Learn-PyQt>dir
Calculate.py
Connections.py
CurrencyCalculator.py
ImprovedSignals.py

All the above files are up-to-date in respect to my remote git repository. What I mean is I have successfully executed git add, git commit, git push on my repository.

Now after the above procedure by mistake I deleted the file Connections.py from my system (localmachine). After deletion I have not yet pushed anything.

Now, as the file is present on my github repository is there any way I can execute a command and update the contents of my local directory mentioned above.

I know that one can delete the current repository and do a git clone but I don't want to download all the files from my repository. Just the one that is deleted. So, something like git sync currentdirectory.

Bart
  • 19,692
  • 7
  • 68
  • 77
RanRag
  • 48,359
  • 38
  • 114
  • 167

1 Answers1

2

Simply

git checkout -- Connections.py

You will get back the content of that file as last pushed or last added to the index (minus any local modification you might have done since said last push).

In short, git checkout can be used for a file or a path, not just a full tree like a commit or a tag.

If you had made several commits before realizing that your file was deleted, then you would have had to apply one of the solutions of "Restore a deleted file in a Git repo".

I particularly like, for a directory where some files are missing:

 git ls-files -d | xargs git checkout --

(git ls-files -d would show deleted files)

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • @VonC - What about to sync entire local folder with the git repository ? Suppose I've local folder which out of sync with the git repository (which is the latest source code). How can I make that work ? Please help – Ramakishna Balla Jan 20 '15 at 05:42
  • @Ram simply git checkout -- yourFolder – VonC Jan 20 '15 at 05:44
  • Acutally I'm on NitrousIO and when I do 'git checkout -- BRKsJobs_Board' it says like 'error: pathspec 'RKsJobs_Board' did not match any file(s) known to git.' But actually I've this repo setup on github and also the folder setup on NitrousIO. :( – Ramakishna Balla Jan 20 '15 at 06:18
  • @RamakishnaBalla then ask a separate question with all the details you can provide on your current setup. That will be more visible than in a comment thread. – VonC Jan 20 '15 at 06:24
  • this is the question I've posted - http://stackoverflow.com/questions/28039173/how-to-sync-the-local-folder-with-the-git-repository-on-nitrousio. See, if you can help me out. – Ramakishna Balla Jan 20 '15 at 06:40