I got sent a bunch of files originating from the same git repo I work with, but they were developed against an older commit. How to find out which commit they used? Something like least lines of diff.
How to find out which commit they used? Something like least lines of diff.
Well, you can do exactly that: find the commit with the smallest diff against your target directory. Just run a loop over all the commits in your repository, and for each one compute a diff against your target directory, and remember the one with the smallest diff.
Let's assume you have your repository in ./repo
and the files in question in ./target
.
#!/bin/sh
cd repo
HEAD=$(git rev-parse HEAD)
git log --pretty='%H' | while read rev; do
git checkout -q $rev
lines=$(diff -ruN -x .git . ../target | wc -l)
if ! [[ "$minlines" ]] || [[ $lines -lt $minlines ]]; then
minlines=$lines
commit=$rev
echo "$lines $rev"
fi
[[ $lines -eq 0 ]] && break
done | tail -1
git checkout $HEAD
This will take a while for a repository with a long history, but it works. It will print out the size of the diff followed by the commit id for the commit with the smallest diff.
If you interrupt this script while it's running you'll need to check out an appropriate branch head (e.g., git checkout master
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With