Skip to content

Instantly share code, notes, and snippets.

@nezaboravi
Created December 29, 2024 10:33
Show Gist options
  • Save nezaboravi/ced6123f8165dcbf12df55403f27211d to your computer and use it in GitHub Desktop.
Save nezaboravi/ced6123f8165dcbf12df55403f27211d to your computer and use it in GitHub Desktop.
git commit function
commit() {
# Run Laravel Pint if installed
if command -v pint &> /dev/null; then
echo "Running Laravel Pint..."
pint
# Stage changes if Pint modifies files
if ! git diff --quiet; then
echo "Pint modified files. Staging changes..."
git add .
fi
fi
# Check for changes not staged for commit
local unstaged_changes
unstaged_changes=$(git diff --name-only)
if [[ -n $unstaged_changes ]]; then
echo "Changes not staged for commit:"
echo "$unstaged_changes"
echo -n "Do you want to stage all unstaged changes? (Y/n): "
read stage_unstaged
stage_unstaged=${stage_unstaged:-y} # Default to 'y'
if [[ $stage_unstaged == [yY] ]]; then
git add .
echo "Unstaged changes have been staged."
else
echo "No changes staged. Exiting."
return
fi
fi
# Check for untracked files
local untracked_files
untracked_files=$(git ls-files --others --exclude-standard)
if [[ -n $untracked_files ]]; then
echo "Untracked files found:"
echo "$untracked_files"
echo -n "Do you want to stage all untracked files? (Y/n): "
read stage_untracked
stage_untracked=${stage_untracked:-y} # Default to 'y'
if [[ $stage_untracked == [yY] ]]; then
git add .
echo "Untracked files have been staged."
fi
fi
# Check if there are any staged changes
if git diff --cached --quiet; then
echo "No changes to commit. Checking if there’s anything to push..."
# Check for staged files ready to push
if git status | grep -q "Your branch is up to date"; then
echo "Nothing to push. Everything is up to date."
return
fi
# Pull latest changes and push
echo "Pulling latest changes from the remote branch..."
git pull --no-edit
if [[ $? -ne 0 ]]; then
echo "Merge conflicts detected. Resolve conflicts before pushing."
return
fi
echo "Pushing changes..."
git push
echo "Push completed."
return
fi
# Prompt for commit message
echo -n "Enter commit message (default: WIP): "
read message
message=${message:-WIP}
# Confirm and commit
echo -n "Commit staged changes with message '$message'? (Y/n): "
read confirm_commit
confirm_commit=${confirm_commit:-y} # Default to 'y'
if [[ $confirm_commit == [yY] ]]; then
git commit -m "$message"
echo "Changes committed with message: '$message'"
# Pull latest changes and push
echo "Pulling latest changes from the remote branch..."
git pull --no-edit
if [[ $? -ne 0 ]]; then
echo "Merge conflicts detected. Resolve conflicts before pushing."
return
fi
echo "Pushing committed changes..."
git push
echo "Push completed."
else
echo "Commit aborted."
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment