user429400
user429400

Reputation: 3325

Search for and view a file deleted from the repo

I'm working on a project in git, and I know that at some time in the past (long before I had the code) there existed a file called exec.js. I want to view the contents of this file. I know which repository used to contain this file, but I don't know the exact path.

Upvotes: 7

Views: 1461

Answers (3)

Go Dan
Go Dan

Reputation: 15502

There's no easy way to list a deleted file's full name and its contents in one or two commands. However, here's an alias to get you started:

$ git config --global alias.find-deleted-file '!f() { for c in `git rev-list HEAD`; do for fnm in `git diff-tree -r --diff-filter=D --name-only $c | grep -P ".*/$1"` ; do echo "$c $fnm" ; git show $c^:$fnm ; echo "" ; done ; done ; } ; f'

This will make available a Git find-deleted-file alias that will search for a deleted file (using diff-tree) with the provided file name pattern (pattern matching provided by grep -P), displaying the commit the file was deleted, the file's full name and its content before it was deleted.

Using the following example repository history:

$ git log --all --graph --decorate --oneline --name-only
* 3f3cba1 (HEAD, master) I
I
* 4c8b369 H
E/H
* a008343 rm F
E/F
* 525a127 FG
E/F
E/G
* 3a10f93 initial
A
B
C
D
E/F

You would search for the deleted F file using:

$ git find-deleted-file F
a008343eec91fe917078209d44ae93ee72fc5acb E/F
F

Upvotes: 0

eckes
eckes

Reputation: 67127

You could use wildcard characters in git log:

git log -- *exec.js

will give you all log messages when any file called exec.js was modified.

Once you find the deleting commit (say A), you could

git checkout A -- *exec.js

This will bring you the last version of exec.js.

If you want to view the full history of exec.js, you could use gitk:

gitk -- *exec.js

This will show you all the modifications again. If you're particularly interested in one commit, you could right click the file in the gitk dialog and then select external diff tool. This will open the external diff tool with the full files, not only the patches.

Upvotes: 3

Sandip Ransing
Sandip Ransing

Reputation: 7733

You can search for that particular file inside log history using -

If you don't have filepath then

git log --diff-filter=D --summary | grep filename

If you have filepath then you can use -

git log -- filepath
git log -n 1 -- filepath

and once you get the revision you can checkout that particular revision to get that file

Upvotes: 1

Related Questions