Skip to content

Instantly share code, notes, and snippets.

@alexdovzhanyn
Last active August 14, 2024 20:56
Show Gist options
  • Save alexdovzhanyn/427564fb1ddd8bb44a4cdae1b48fcbfa to your computer and use it in GitHub Desktop.
Save alexdovzhanyn/427564fb1ddd8bb44a4cdae1b48fcbfa to your computer and use it in GitHub Desktop.
Shell script to delay a git commit + push until a later time
#!/bin/bash
# Shell script to delay a git commit + push until a later time. Useful for making sure code gets pushed when you want it
# even if you're away.
#
# This will commit with the given commit message after the specified amount of time has passed.
# It will push to the current branch.
days=0
hours=0
minutes=0
seconds=0
message="Default commit message"
usage() {
echo "Usage: $0 [-d days] [-h hours] [-m minutes] [-s seconds] -c 'commit message'"
exit 1
}
if [[ ! "$*" =~ "-c" ]]; then
echo "Error: Commit message is required."
usage
fi
# Parse command line options
while getopts "d:h:m:s:c:" opt; do
case $opt in
d) days=$OPTARG;;
h) hours=$OPTARG;;
m) minutes=$OPTARG;;
s) seconds=$OPTARG;;
c) message=$OPTARG;;
*) usage;;
esac
done
total_seconds=$((days * 86400 + hours * 3600 + minutes * 60 + seconds))
echo "Will push to git after $days day(s), $hours hour(s), $minutes minute(s), and $seconds second(s)."
sleep $total_seconds
git add .
git commit -m "$message"
git_branch=$(git rev-parse --abbrev-ref HEAD)
git push origin $git_branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment