Skip to content

Instantly share code, notes, and snippets.

@ManUtopiK
Created August 19, 2023 15:31
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 ManUtopiK/f25efe9e792cfa8b01112ac2a42805c5 to your computer and use it in GitHub Desktop.
Save ManUtopiK/f25efe9e792cfa8b01112ac2a42805c5 to your computer and use it in GitHub Desktop.
git_commit_and_push.sh
#!/bin/bash
# Check if the user provided the branch name and commit message as arguments
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <branch_name> <commit_message>"
exit 1
fi
branch_name="$1"
commit_message="$2"
# Function to check if there are any changes to commit
check_changes() {
if [ -z "$(git status --porcelain)" ]; then
echo "No changes to commit."
exit 0
fi
}
# Function to add changes, commit and push
commit_and_push() {
git add .
git commit -m "$commit_message"
git push origin "$branch_name"
}
# Main script logic
echo "Executing script to automate Git actions..."
# Ensure the current directory is a Git repository
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
echo "Error: Current directory is not a Git repository."
exit 1
fi
# Switch to the specified branch
git checkout "$branch_name"
# Check for changes and proceed accordingly
check_changes
# Perform the commit and push
commit_and_push
echo "Changes committed and pushed successfully to the $branch_name branch."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment