I want to know details about a file that was in a git repo at some point but is not in the repo now. There are three scenarios to look for here.
- File was renamed. If so what is the current name of the file.
- File was deleted. This should be straight forward provided the file was not renamed at some point before it was deleted.
- File was renamed and then deleted.
in all these cases I am interested in knowing current name of file if renamed or if the file was deleted.
I am in windows machine using git in powershell but these steps should be easy to replicate in other systems.
STEPS
a. I added 2 files
Add-Content file1.txt "This is file 1"
Add-Content file2.txt "This is file 2"
git add .
git commit -m "Added file1.txt and file2.txt"
b. I renamed file1.txt
git mv file1.txt fileone.txt
git commit -m "Renamed file1.txt to fileone.txt"
c. I deleted file2.txt
Remove-Item file2.txt
git add .
git commit -m “Deleted file file2.txt”
There might be more commits between these commits that does not change these 2 files. This is my git log so far
git log –oneline
d618114 (HEAD -> master) deleted file2.txt
ba6ec22 Renamed file1.txt to fileone.txt
fe2a51e Added file1.txt and file2.txt
This is what I have so far.
git log --name-status -- "file1.txt"
outputs:
commit ba6ec22e3fdf7e6eb6f33acd83f49f99e9f2610a
Author: Sunil Shahi <myemail@email.com>
Date: Sun Oct 8 15:35:02 2017 -0500
Renamed file1.txt to fileone.txt
D file1.txt
commit fe2a51e9aa5835c5886b31f988e4076155c1194e
Author: Sunil Shahi <myemail@email.com>
Date: Sun Oct 8 15:31:27 2017 -0500
Added file1.txt and file2.txt
A file1.txt
The problem with this is that it shows that the file was deleted in my second commit when in fact it was renamed. If I use current file name with --follow
flag, I get more detail but I do not know the file name.
git log --follow --name-status -- "fileone.txt"
outputs
commit ba6ec22e3fdf7e6eb6f33acd83f49f99e9f2610a
Author: Sunil Shahi <myemail@email.com>
Date: Sun Oct 8 15:35:02 2017 -0500
Renamed file1.txt to fileone.txt
R100 file1.txt fileone.txt
commit fe2a51e9aa5835c5886b31f988e4076155c1194e
Author: Sunil Shahi <myemail@email.com>
Date: Sun Oct 8 15:31:27 2017 -0500
Added file1.txt and file2.txt
A file1.txt
This approach is sufficient for deleted files. However I will run into same problem if it was renamed at some point before it was deleted.
Thanks in advance.