Created
August 19, 2023 15:31
-
-
Save ManUtopiK/f25efe9e792cfa8b01112ac2a42805c5 to your computer and use it in GitHub Desktop.
git_commit_and_push.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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