Ghassan Karwchan
Ghassan Karwchan

Reputation: 3539

Filter git log to find deleted file by some user

Long time ago, there was a spike that I did to try something, and then I removed it.

I don't remember when I removed it (more than a year or maybe two years ago), but it was in our codebase for two or three months.

The spike was a code to read "pfx" files, and include unit tests.

I want to see if I can retrieve the code.

All what I know, is the code that I removed

  1. deleted a file with extensions "pfx"
  2. was under our "test" folder
  3. and was removed by me.

Is there a way to find that code

Upvotes: 0

Views: 68

Answers (2)

dani-vta
dani-vta

Reputation: 7180

You could use the git log command with a glob pattern to narrow down the list of commits that affected pfx files under the test folder.

To help you to identify the test file, you could use the following options:

  • --diff-filter=D to filter for deleted files.
  • --author to filter for the commits recorded by the given author.
  • --name-only to list the files affected by the commit.
# searching from the main line of development 
git log --diff-filter=D --author=<author-name> --name-only master -- test/**/*.pfx

Once you obtain the list of commits, you can restore the file with git checkout from the commit responsible of its deletion.

git checkout <commit-id> -- <file-path>

Upvotes: 0

Hashir Akbar
Hashir Akbar

Reputation: 151

In order to retrieve code that you removed from your codebase, you can try a few approaches

1- Checking the git history first and look for commits to check for the code that is removed :

git log -- path/to/test/folder

2- finding deleted files :

git log --diff-filter=D -- path/to/test/folder

3- After finding the deleted code you can check the commit details by :

git show <commit-hash>

4- And when you found the file/code than just restore that commit:

git checkout <commit-hash> -- path/to/test/folder/your_file

Upvotes: 1

Related Questions