Git diff - how to get different files (only name) between two tags by 'git diff' and order by the file commit time

StackOverflow https://stackoverflow.com/questions/18400336

  •  26-06-2022
  •  | 
  •  

Question

Through the command:

$git diff --name-status tag1 tag2 -- target-src

The output is like:

M       src/config.h
M       src/createTable.sql
A       src/gameserver/BattleGround_10v10.h
M       src/gameserver/CMakeLists.txt
M       src/gameserver/achieve.cpp
M       src/gameserver/achieve.h
A       src/gameserver/action.cpp
A       src/gameserver/action.h
A       src/gameserver/activity.cpp
A       src/gameserver/activity.h
M       src/gameserver/admin.cpp

I got the files that has modified between the two tags. But I want the list order by committed time. How can I do that?


Thanks to ilius's answer, I added awk for my request:

git diff --name-status tag1 tag2 | while read line ; do
    status=${line:0:1}
    path=${line:2}
    date=$(git log -1 '--pretty=format:%ci' -- "$path")
    echo "$date    $status   $path"
done | sort -r | awk '{print $4" "$5}'

But I think it is too complicated. Can it be simpler?

Was it helpful?

Solution

Using mart1n's idea,

git log --name-status '--pretty=format:' tag1 tag2 -- target-src | grep -vxh '\s*'

gives a clean output.

Also try this script:

git diff --name-status tag1 tag2 | while read line ; do
    status=${line:0:1}
    path=${line:2}
    date=$(git log -1 '--pretty=format:%ci' -- "$path")
    echo "$date    $status   $path"
done | sort -r

You can remove the dates (used for sorting) later, I think dates are useful though.

You can also remove -r option from sort if you want the older changed files to be on top.

OTHER TIPS

You can try the same thing, but with git log:

git log --name-status tag1 tag2 -- target-src

That way, the files are sorted in the order the commits were made. Also git log has various sorting options you can try out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top