Although an age-old Q&A. I thought I'd throw one in since this has been bugging me for a long time.
I am used to list the files in a directory by reverse-time order (funny me, heh?). The reason is that I would like to see which files I have (or anyone else has) changed recently.
Git will mess my plans because when switching a branch the local repo will completely overwrite the tracked files from the (incremental... I know...) copies that sit in the packed local repo.
This way all files that were checked out will carry the time stamp of the checkout and will not reflect their last modification time..... How so annoying.
So, I've devised a one-liner in bash that will update a $Date:$ property inside any file WITH THE TIME OF LAST MODIFICATION ACCORDING TO WHAT IT HAS ON FILE SYSTEM such that I will have an immediate status telling of last modification without having to browse the git log
, git show
or any other tool that gives the commit times in blame mode.
The following procedure will modify the $Date: $ keyword only in tracked files that are going to be committed to the repo. It uses git diff --name-only
which will list files that were modified, and nothing else....
I use this one-liner manually before committing the code. One thing though is that I have to navigate to the repo's root directory before applying this.
Here's the code variant for Linux (pasted as a multi-line for readability)
git diff --name-only | xargs stat -c "%n %Y" 2>/dev/null | \
perl -pe 's/[^[:ascii:]]//g;' | while read l; do \
set -- $l; f=$1; shift; d=$*; modif=`date -d "@$d"`; \
perl -i.bak -pe 's/\$Date: [\w \d\/:,.)(+-]*\$/\$Date: '"$modif"'\$/i' $f; \
git add $f; done
and OSX
git diff --name-only | xargs stat -f "%N %Sm" | while read l; do \
set -- $l; f=$1; shift; d=$*; modif=`date -j -f "%b %d %T %Y" "$d"`; \
perl -i.bak -pe 's/\$Date: [\w \d\/:,.)(+-]*\$/\$Date: '"$modif"'\$/i' $f; \
git add $f; done