Search code examples
gitrenamedifffile-renamegit-diff

git How to list all file and directory renames between two commits


I am writing a script that uses git. It needs to know the old file path and the new file path of all renamed directories and files.

Example of what I want to do: git diff --find-renames-only someGitHash someGitHash Then I would like to see a list like:

rename old/path/old-file-name.extension->new/path/new-file-name.extension

or

rename from source/README.md
rename to documentation/README.md

Or something like this as long as it has the old file name with the old directory path and the new file name with the new directory path for each .

I have messed around and searched everywhere so far the closest thing I could find is git diff --find-renames

git diff --find-renames gives me the data I want but with way too much data for each file. It is definitely not something I want to deal with and parse through. Some of the git repos I am working with could have thousands of file/folder renames...

Example:

...tons more data that I do not want to parse through
rename from source/README.md
rename to documentation/README.md
...tons more data including diffs of files before and after... 

this pattern continued for each file...

How do I list all file and directory renames between two git commits without the extra data?

Any help would be much appreciated, Thanks!


Solution

  • git log --name-status <start_commit>..<end_commit> | grep ^R
    

    This will show you a history of renames from newest to oldest.

    The output looks like this:

    R100    the-5-min/.watchmanconfig       the-10-min/.watchmanconfig
    R100    the-5-min/App.tsx       the-10-min/App.tsx
    R085    the-5-min/app.json      the-10-min/app.json
    R100    the-5-min/assets/examples/accordion.png the-10-min/assets/examples/accordion.png
    R100    the-5-min/assets/examples/angular-gradient.png  the-10-min/assets/examples/angular-gradient.png
    

    The first letter is the type of change (see --diff-filter for the details of the different change types), and the number is the degree of similarity between commits. The first filename listed is the old filename, second filename is the new one.