diff --git a/git_push.sh b/git_push.sh new file mode 100755 index 00000000..c723a636 --- /dev/null +++ b/git_push.sh @@ -0,0 +1,54 @@ +#!/bin/bash + +# Check if the correct number of parameters is provided +if [ "$#" -ne 2 ]; then + echo "Usage: $0 " + exit 1 +fi + +# Assign parameters to variables +COMMIT_MESSAGE=$1 +BRANCH_NAME=$2 + +# Check if the branch exists +BRANCH_EXISTS=$(git branch --list "$BRANCH_NAME") + +if [ -z "$BRANCH_EXISTS" ]; then + echo "Branch '$BRANCH_NAME' does not exist. Creating it now..." + git checkout -b "$BRANCH_NAME" + if [ $? -ne 0 ]; then + echo "Failed to create branch '$BRANCH_NAME'." + exit 1 + fi +else + echo "Branch '$BRANCH_NAME' exists. Checking it out..." + git checkout "$BRANCH_NAME" + if [ $? -ne 0 ]; then + echo "Failed to checkout branch '$BRANCH_NAME'." + exit 1 + fi +fi + +# Perform git operations +echo "Adding all changes..." +git add . +if [ $? -ne 0 ]; then + echo "Failed to add changes." + exit 1 +fi + +echo "Committing changes with message: $COMMIT_MESSAGE" +git commit -m "$COMMIT_MESSAGE" +if [ $? -ne 0 ]; then + echo "Commit failed." + exit 1 +fi + +echo "Pushing changes forcefully to branch: $BRANCH_NAME" +git push -f origin "$BRANCH_NAME" +if [ $? -ne 0 ]; then + echo "Push failed." + exit 1 +fi + +echo "Git operations completed successfully."