Fetching an old deleted file of a project using Git
Recently in one of my projects, I had to bring the content of old-forgotten-deleted file back to the application code base using Git. The problem took a ‘fancy’ turn as the path to the file was no more a valid path in Git as there had been a major refactorings in our project esp. renaming of the packages as well as the folder structure since the file in question was deleted.
This is how I could see the deleted file (& its content) using the Git commands:
1. First of all, we will try to get the revision number of the git commit which committed our deleted file.
Type in, git log
on your console at the location where you have cloned/forked your Git project. This command will show a all the commits that has been done since the start of the project.
If you know at what period of time the deleted file was committed, you can use another flavor of git log command i.e.
git log –before=”Oct 01 2010″, or,
git log –after=”Oct 01 2010″
If you have an idea about who all could have or have committed the deleted file, then you can also use another option provided by git log command i.e
git log –author=”Author Name” or
git log –author=authorId
The commands above are self-explanatory.
NOTE: The step above would be helpful only if your team has followed good practices such as putting appropriate and self-explanatory comments while committing. So like if you have deleted a file & the comment while committing specifies that in some way or other it would be really easy for you to find out the probable revision number of that old commit by seeing the logs.
2. Now once we have the revision number, we can try using the
git show
command to make sure that the deleted file was actually present in the commit. Type the following:
git show --name-only <revisionNumber>
Once you see your sought-after file there, then you can see the contents of your file by typing
git show <revisionNumber>
This command will show you the change-set and the content of the file. Now you can fetch or copy the code out of the deleted file and place it in your new code or create an entirely new file out of it.
Hope this helps !!!
Reference: http://cheat.errtheblog.com/s/git
— Abhishek Tejpaul