1

This is what happened:

1)I forked a repository

2)I cloned the forked repo to my laptop

3)I added some files of my own

4)I accidentally deleted a file (which was among the files from the original repo)

5)Commited and pushed the changes (I think I ran into some issues here, somehow fixed it). So at this point neither the online repo nor the offline one in my laptop had the file I accidentally deleted. Only the original repo I forked has it.

Now I want to get that file back. Doing a git pull on the original repo just tells me mine is up to date. How can I do this?

I saw some answers that use git checkout and git rebase but I just got confused (I'm fairly new to using git). Also after spending a few hours debugging code I really don't want to "debug" git commands. -_-

So please provide a solution and a simple explanation to it.

bahrep
  • 29,961
  • 12
  • 103
  • 150
AZG
  • 107
  • 1
  • 7
  • Show the exact commands that you did. – Basile Starynkevitch Sep 05 '17 at 10:31
  • The answer depends on what you want to achieve. You could simply revert your commit or checkout another commit where the file exists. Do you have gitweb or something similar on your host? This would at least show you the file in an easy manner. I highly recommend you read [some git documentation](https://git-scm.com/book/en/v2) because it seems like you don't know what git is about at all and you should know the tools you are working with (no offense :) ) – getjackx Sep 05 '17 at 10:37
  • 3
    Possible duplicate of [Find and restore a deleted file in a Git repository](https://stackoverflow.com/questions/953481/find-and-restore-a-deleted-file-in-a-git-repository) – getjackx Sep 05 '17 at 10:40

1 Answers1

1

You have to find the id of the last commit that did include the file by looking at the files history via git log -n2 -- path/to/file. This will show you the commit the file was deleted in and the last one before. Note the commit id of the latter.

Then use git checkout <commit_id_you_noted> -- path/to/file to restore the file.

This will restore the file as it was in the commit with id <commit_id_you_noted>. It will be marked as added and you will have to commit it.

kowsky
  • 12,647
  • 2
  • 28
  • 41