Skip to content

Instantly share code, notes, and snippets.

@Vinfall
Last active February 4, 2024 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vinfall/6799100178828234ed9ef29e167f313e to your computer and use it in GitHub Desktop.
Save Vinfall/6799100178828234ed9ef29e167f313e to your computer and use it in GitHub Desktop.
Action-bot automated repository can be filled with GA commits, the script would delete GA commits in HEAD until the earliest commit not authored by GA.
#!/bin/bash
# Usage: action-bot automated repository can be filled with GA commits,
# the script would delete GA commits in HEAD until the earliest commit not authored by GA.
# Author: Vinfall
# License: CC0 (Public Domain)
# Define a function to delete commits authored by a specific email address
del_ga_commits() {
# Define the email address of the author to be deleted
author_email="action@github.com"
# Get the HEAD of the current branch
current_commit=$(git rev-parse HEAD)
# Loop to find the first commit that is not authored by the specified email address
while true; do
# Get the author of the current commit
commit_author=$(git log -1 --pretty=format:"%ae" "$current_commit")
# If the author is not the specified email address, stop the loop
if [ "$commit_author" != "$author_email" ]; then
break
fi
# Get the parent commit of the current commit
current_commit=$(git log -1 --pretty=%P "$current_commit")
# If it has reached the earliest commit, stop the loop
if [ -z "$current_commit" ]; then
echo "No commits authored by GitHub Action in HEAD found, no need to delete."
return
fi
done
# Get the short version of the commit hash
short_commit=$(git rev-parse --short=8 "$current_commit")
# Prompt the user to confirm the operation
echo "You are about to delete all commits from HEAD to commit $short_commit. Continue? [y/n] \c"
read -r REPLY
if [[ $REPLY =~ ^[Yy]$ ]]
then
# Reset the main branch to the specified commit
git reset --hard "$current_commit"
# Push the changes to the remote repository
git push origin main --force
else
echo "Operation canceled."
fi
}
# Call the function to delete commits authored by a specific email address
del_ga_commits
@Vinfall
Copy link
Author

Vinfall commented Feb 4, 2024

Updated to make it pass shellcheck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment