Search code examples
gitgit-diff

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


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?


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.