← Back to all snippets
BASH

Streamline Git Commit and Push Workflow

Automate your Git workflow by checking status, adding all changes, committing with a message, and pushing to the remote repository with a single command.

#!/bin/bash
# Usage: ./git_workflow.sh "Your commit message"

COMMIT_MESSAGE="${1:-"Automated commit"}"

echo "Running Git workflow:"

# 1. Check current status
echo -e "
--- Git Status ---"
git status

read -p "Continue with commit? (y/N) " -n 1 -r
echo # Newline for clean output
if [[ ! "$REPLY" =~ ^[Yy]$ ]]
then
    echo "Aborted."
    exit 0
fi

# 2. Add all changes
echo -e "
--- Adding all changes ---"
git add .

# 3. Commit changes
echo -e "
--- Committing changes ---"
git commit -m "$COMMIT_MESSAGE"

# Check if commit was successful (e.g., no changes to commit)
if [ $? -ne 0 ]; then
    echo "Commit failed or no changes to commit. Exiting." >&2
    exit 1
fi

# 4. Push to remote
echo -e "
--- Pushing to remote ---"
git push

if [ $? -eq 0 ]; then
    echo -e "
Git workflow completed successfully!"
else
    echo -e "
Git push failed. Please resolve manually." >&2
    exit 1
fi
How it works: This script automates a common Git workflow: checking the status, adding all modified and new files (`git add .`), committing them with a user-provided or default message, and then pushing to the remote repository. It includes a confirmation prompt before committing and checks the exit status of Git commands to provide feedback on success or failure, making it a robust helper for daily development.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs