Suppose I have a file \"ChangeLog\" in branch \"master\". I want to record information about all changes in any branch in this file (in more detail than in a commit message
You could use a post-checkout hook:
#!/bin/sh
newref="$2"
isbranch="$3"
# ignore file checkouts
if test $isbranch -eq 0; then
exit 0
fi
path=ChangeLog
git for-each-ref --sort=-committerdate \
--format='%(objectname) %(refname:short)' \
refs/heads/\* |
while read sha ref; do
if git rev-parse --verify --quiet "$sha:$path" >/dev/null
then
if test "$newref" != "$sha"; then
echo Checking out $path from $ref
git checkout $sha -- "$path"
fi
break
fi
done
It will still be up to you to add and commit the ChangeLog on your branch if appropriate.
This hook sorts branches by their heads' respective commit dates, which could be problematic. Say you create a topic branch rooted at a commit in master's history. Even though its snapshot of ChangeLog is older in the sense of calendar time, the hook above will treat it as the newest because it is referenced by a commit that was created more recently, so be careful that you don't accidentally lose work by switching branches when you have unstaged changes to ChangeLog.