Maciej Kravchyk
Maciej Kravchyk

Reputation: 16727

git - show renames in a commit, simple output

I need to see file renames which were done at a commit and be able to parse the output. I have done some research and this is the best I could get:

git show commit_hash --summary --format=""

output:

 rename src/{old/location => new/place}/lib.ts (100%)
 rename src/{old/location => new/place}/lib.types.ts (100%)
 create mode 100644 src/new/place/index.ts

However, I don't want to use it as I think it's difficult to parse - I would have to test different filename edge cases to ensure the paths are always parsed properly.

I'm looking for something like rename src/old/location/lib.ts to src/new/place/lib.ts

Using git show without --summary option, prints full file paths (rename from \n rename to) - which would be easy to parse, although it also shows the whole file diff - which is something I don't want. If I understand correctly, the rename from oldpath / rename to newpath is part of the diff itself.

Is there any way to show renames of a commit in a reliable, easy to parse way, without outputting diffs?

Upvotes: 0

Views: 54

Answers (1)

Maciej Kravchyk
Maciej Kravchyk

Reputation: 16727

Using git diff-tree (thanks @fluffy)

git diff-tree --find-renames -r --name-status --format="" $commit_hash

Using git diff

git diff --name-status $commit_hash~ $commit_hash

Both commands will output something like this:

R100 src/old/location/lib.ts src/new/place/lib.ts
R095 src/old/location/lib.types.ts src/new/place/lib.types.ts
A src/new/place/index.ts

git diff-tree appears to be the better option, as git diff version will throw a fatal error when trying to retrieve the first commit.

Upvotes: 1

Related Questions