Skip to content

Instantly share code, notes, and snippets.

@sumrender
Last active February 20, 2025 06:55
Show Gist options
  • Save sumrender/ef5793a26228cb6d865344ae69a5d678 to your computer and use it in GitHub Desktop.
Save sumrender/ef5793a26228cb6d865344ae69a5d678 to your computer and use it in GitHub Desktop.
#!/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
function git_branch_name() {
branch=$(git symbolic-ref HEAD 2>/dev/null | awk -F/ '{print $NF}')
if [[ $branch == "" ]];
then
:
else
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 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..."
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