Skip to content

Instantly share code, notes, and snippets.

@sumrender
Last active August 26, 2025 16:10
Show Gist options
  • Select an option

  • Save sumrender/ef5793a26228cb6d865344ae69a5d678 to your computer and use it in GitHub Desktop.

Select an option

Save sumrender/ef5793a26228cb6d865344ae69a5d678 to your computer and use it in GitHub Desktop.
bash
#!/bin/bash
# ------------------------------- INSTRUCTIONS ---------------------------------
# Save as setup-git-config.sh
# In terminal run: zsh ./setup-git-config.sh
# ------------------------------------------------------------------------------
# Define the content to be added
read -r -d '' CONTENT << 'EOF'
# Function to get git branch (handles names like "task/my-branch" correctly)
function git_branch_name() {
# --short keeps the full branch ref without chopping by '/'
local branch
branch=$(git symbolic-ref --short HEAD 2>/dev/null)
if [[ -n "$branch" ]]; then
echo "($branch)"
fi
}
# Enable substitutions in prompt
setopt PROMPT_SUBST
# Set the prompt with git branch
PROMPT='%F{green}%n@%m%f:%F{blue}%~%f %F{red}$(git_branch_name)%f$ '
# Simple git aliases
alias ga='git add .'
alias gs='git status'
alias gl='git log'
alias gp='git pull'
alias gd='git diff'
alias gb='git branch'
alias gst='git stash'
alias gstp='git stash pop'
alias gsr1='git reset --soft HEAD~1'
alias gco="git checkout"
# Function to push current branch
function push() {
git push origin "$(git symbolic-ref --short HEAD)"
}
# Function to pull current branch
function pull() {
git pull origin "$(git symbolic-ref --short HEAD)"
}
# Function to add and commit
function gac() {
if [[ -z "$1" ]]; then
echo "Please provide a commit message"
return 1
fi
git add .
git commit -m "$1"
}
# Function to add, commit, and push
function gacp() {
if [[ -z "$1" ]]; then
echo "Please provide a commit message"
return 1
fi
# Get the branch name from parameter or current branch
local branch=${2:-$(git symbolic-ref --short HEAD)}
git add .
git commit -m "$1"
git push origin "$branch"
}
EOF
# Path to .zshrc
ZSHRC="$HOME/.zshrc"
# Check if .zshrc exists
if [ ! -f "$ZSHRC" ]; then
echo "Creating new .zshrc file..."
touch "$ZSHRC"
fi
# Check for existing configurations
if grep -q "function git_branch_name()" "$ZSHRC"; then
echo "Git configurations already exist in .zshrc!"
echo "To prevent duplicates, please remove existing configurations first."
exit 1
fi
# Add a newline for cleaner separation
echo "" >> "$ZSHRC"
# Add the content to .zshrc
echo "Adding git configurations to .zshrc..."
echo "$CONTENT" >> "$ZSHRC"
# Source the updated .zshrc
echo "Sourcing .zshrc..."
# shellcheck disable=SC1090
source "$ZSHRC"
echo "Git configurations have been successfully added to your .zshrc!"
echo "Your terminal is ready with the new git helpers!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment