Skip to content

Instantly share code, notes, and snippets.

@ejhayes
Last active May 3, 2023 01:12
Show Gist options
  • Save ejhayes/854d3b7965f14d41a5387d362c8ed30e to your computer and use it in GitHub Desktop.
Save ejhayes/854d3b7965f14d41a5387d362c8ed30e to your computer and use it in GitHub Desktop.
Find nearest equivalent git commit that does not differ from the current commit
#!/bin/sh
# Get the HEAD commit hash
HEAD=$(git rev-parse HEAD)
# Generate a list of all commit hashes in the repository
git rev-list --no-merges --all | while read COMMIT; do
# Compare the commit to the HEAD
if [ -z "$(git diff --name-only $COMMIT $HEAD)" ]; then
# If there are no differences, exit the loop and return the commit hash
echo "Found commit with no differences: $COMMIT"
exit 0
fi
done
# If no matching commit was found, exit with an error
echo "No matching commit found"
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment