0

I want to find out if a file has been renamed. I am using git to track the project.

How do I use git to see if this particular file has been renamed in the past? Also I want to limit search to a recent time period like 1 month or 1 year.

Filburt
  • 17,626
  • 12
  • 64
  • 115
pranith
  • 869
  • 9
  • 24

3 Answers3

2

One option you can try is git blame.

It shows you, for each requested line of the given file, which commit is responsible for that line, and sometimes it shows you what was the name of the file in that commit (if it detects that multiple files contributed to it).

For example (from GitBook) :

Another cool thing about Git is that it doesn’t track file renames explicitly. It records the snapshots and then tries to figure out what was renamed implicitly, after the fact. One of the interesting features of this is that you can ask it to figure out all sorts of code movement as well. If you pass -C to git blame, Git analyzes the file you’re annotating and tries to figure out where snippets of code within it originally came from if they were copied from elsewhere. Recently, I was refactoring a file named GITServerHandler.m into multiple files, one of which was GITPackUpload.m. By blaming GITPackUpload.m with the -C option, I could see where sections of the code originally came from:

$ git blame -C -L 141,153 GITPackUpload.m
f344f58d GITServerHandler.m (Scott 2009-01-04 141)
f344f58d GITServerHandler.m (Scott 2009-01-04 142) - (void) gatherObjectShasFromC
f344f58d GITServerHandler.m (Scott 2009-01-04 143) {
70befddd GITServerHandler.m (Scott 2009-03-22 144)         //NSLog(@"GATHER COMMI
ad11ac80 GITPackUpload.m    (Scott 2009-03-24 145)
ad11ac80 GITPackUpload.m    (Scott 2009-03-24 146)         NSString *parentSha;
ad11ac80 GITPackUpload.m    (Scott 2009-03-24 147)         GITCommit *commit = [g
ad11ac80 GITPackUpload.m    (Scott 2009-03-24 148)
ad11ac80 GITPackUpload.m    (Scott 2009-03-24 149)         //NSLog(@"GATHER COMMI
ad11ac80 GITPackUpload.m    (Scott 2009-03-24 150)
56ef2caf GITServerHandler.m (Scott 2009-01-05 151)         if(commit) {
56ef2caf GITServerHandler.m (Scott 2009-01-05 152)                 [refDict setOb
56ef2caf GITServerHandler.m (Scott 2009-01-05 153)

More about the git blame options (from the man page) :

-C|<num>|

In addition to -M, detect lines moved or copied from other files that were modified in the same commit. This is useful when you reorganize your program and move code around across files. When this option is given twice, the command additionally looks for copies from other files in the commit that creates the file. When this option is given three times, the command additionally looks for copies from other files in any commit.

<num> is optional but it is the lower bound on the number of alphanumeric characters that git must detect as moving/copying between files for it to associate those lines with the parent commit. And the default value is 40. If there are more than one -C options given, the argument of the last -C will take effect.

And to limit it to a time period :

--date <format>

The value is one of the following alternatives: {relative,local,default,iso,rfc,short}. If --date is not provided, the value of the blame.date config variable is used. If the blame.date config variable is also not set, the iso format is used. For more information, See the discussion of the --date option at git-log(1).

Eran
  • 387,369
  • 54
  • 702
  • 768
1

You can use git log with --follow option for the specific path/file and add --after=date option to show commits more recent than a specific date.

git log --oneline --name-only --follow -- path/file 

In the example below, file2 is renamed with 069396b commit. It was file1 before.

$ ls 
file2

$ git log --oneline --name-only --follow -- file2 
6589023 msg
file2
069396b msg
file2
10f5830 msg
file1
2ce8c3d msg
file1

From git-log manual page

--follow : Continue listing the history of a file beyond renames (works only for a single file).

Alper
  • 12,860
  • 2
  • 31
  • 41
0

Alper answered this question partially. I figured I would answer the time-period part of the question too:

git log --oneline --name-only --follow --since=<time period> --diff-filter=R -- path/file

will give the commit, if any, which renamed the file. If there was no rename, no such commit exists and hence, nothing will be returned.

pranith
  • 869
  • 9
  • 24