Skip to content

Instantly share code, notes, and snippets.

@ellemedit
Last active July 21, 2024 01:49
Show Gist options
  • Save ellemedit/9d476206bed36709ebea8752b51f0137 to your computer and use it in GitHub Desktop.
Save ellemedit/9d476206bed36709ebea8752b51f0137 to your computer and use it in GitHub Desktop.
git shortcut

gacm: Git Add, Commit, and Push Function for Zsh

Overview

gacm (Git Add Commit Merge) is a Zsh function that simplifies the process of adding, committing, and optionally pushing Git changes. It combines git add ., git commit -m, and (optionally) git push into a single command, with an additional option for force push.

Installation

Add the following function to your ~/.zshrc file:

gacm() {
    local force_push=false
    local push=false
    local commit_message=""

    while [[ $# -gt 0 ]]; do
        case "$1" in
            -p|--push)
                push=true
                shift
                ;;
            -f|--force)
                force_push=true
                shift
                ;;
            *)
                commit_message="$1"
                shift
                ;;
        esac
    done

    if [[ -z "$commit_message" ]]; then
        echo "Error: Commit message is required."
        return 1
    fi

    git add . && git commit -m "$commit_message"

    if $push; then
        if $force_push; then
            git push --force
        else
            git push
        fi
    fi
}

After adding the function, either restart your terminal or run source ~/.zshrc to apply the changes.

Usage

Basic Usage: Add and Commit

To add all changes and create a commit:

gacm "Your commit message"

Advanced Usage: Add, Commit, and Push

To add all changes, create a commit, and push to the remote repository:

gacm -p "Your commit message"

Force Push

To add all changes, create a commit, and force push to the remote repository:

gacm -p -f "Your commit message"

Options

  • -p or --push: Push changes to the remote repository after committing
  • -f or --force: Use force push when pushing changes (must be used with -p)

Error Handling

  • If you forget to provide a commit message, the function will display an error message and exit without performing any Git operations.

Examples

  1. Add and commit changes:

    gacm "Add new feature: user authentication"
    
  2. Add, commit, and push changes:

    gacm -p "Fix bug in login form"
    
  3. Add, commit, and force push changes:

    gacm -p -f "Update project structure"
    
  4. Incorrect usage (will result in an error):

    gacm
    gacm -p
    gacm -f "Force push without -p option"
    

Notes

  • This function uses git add . which adds all changes in the current directory and its subdirectories. Be cautious if you have files that you don't want to commit.
  • The push operation (when using -p) pushes to the current branch's upstream. Make sure you have set up the upstream branch correctly.
  • Force push (-f option) should be used with caution as it can overwrite remote changes.
  • The order of options doesn't matter, but the commit message should always be the last argument.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment