Skip to content

Instantly share code, notes, and snippets.

@DanEdens
Last active December 9, 2022 20:36
Show Gist options
  • Save DanEdens/9d257f82e62072469ebebbe3e3e21b51 to your computer and use it in GitHub Desktop.
Save DanEdens/9d257f82e62072469ebebbe3e3e21b51 to your computer and use it in GitHub Desktop.
.git/hooks/post-commit
#!/bin/sh
# Exit immediately if any command returns a non-zero exit code
set -e
# Check if the current directory is a git repository
if ! git rev-parse --git-dir > /dev/null 2>&1; then
printf "Error: current directory is not a git repository\n"
exit 1
fi
# Get the current branch name
branch_name=$(git symbolic-ref --short HEAD)
# Save the exit code of the previous command
git_symbolic_ref_exit_code=$?
# Set the non-push branch suffix
non_push_branch_suffix="_local"
# Only push if the current branch was found (may be empty if in detached head state)
if [ $git_symbolic_ref_exit_code -eq 0 ]; then
# Only push if the current branch does not end with the non-push suffix
if [[ $branch_name != *$non_push_branch_suffix ]] ; then
echo
echo "**** Pushing current branch $branch_name to origin [i4h post-commit hook]"
echo
git push origin $branch_name;
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment