Created
May 2, 2025 20:16
-
-
Save iamronsuez/bed2cc2c0fef0e2576786b75c44ab61f to your computer and use it in GitHub Desktop.
Setup GIT commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# setup_git_aliases.sh | |
# This script sets up useful Git aliases both as Git config aliases and shell aliases | |
# It checks if Git is installed first and provides feedback at each step | |
# Function to display messages with color | |
print_message() { | |
echo -e "\033[1;34m$1\033[0m" | |
} | |
print_error() { | |
echo -e "\033[1;31m$1\033[0m" | |
} | |
print_success() { | |
echo -e "\033[1;32m$1\033[0m" | |
} | |
# Check if Git is installed | |
print_message "Checking if Git is installed..." | |
if ! command -v git &> /dev/null; then | |
print_error "Git is not installed! Please install Git first." | |
echo "On macOS: brew install git" | |
echo "On Ubuntu/Debian: sudo apt install git" | |
echo "On Fedora/RHEL: sudo dnf install git" | |
exit 1 | |
fi | |
GIT_VERSION=$(git --version) | |
print_success "✓ Git is installed: $GIT_VERSION" | |
# Set up Git configuration aliases | |
print_message "\nSetting up Git configuration aliases..." | |
# Define Git config aliases | |
git config --global alias.st status | |
git config --global alias.a add | |
git config --global alias.co checkout | |
git config --global alias.cob "checkout -b" | |
git config --global alias.br branch | |
git config --global alias.ci commit | |
git config --global alias.amend "commit --amend" | |
git config --global alias.unstage "reset HEAD --" | |
git config --global alias.last "log -1 HEAD" | |
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit" | |
git config --global alias.df diff | |
git config --global alias.dfs "diff --staged" | |
git config --global alias.pl pull | |
git config --global alias.ps push | |
print_success "✓ Git configuration aliases set up successfully" | |
# Detect shell and set up shell aliases | |
print_message "\nSetting up shell aliases..." | |
# Determine which shell config file to use | |
SHELL_NAME=$(basename "$SHELL") | |
SHELL_CONFIG_FILE="" | |
if [ "$SHELL_NAME" = "zsh" ]; then | |
SHELL_CONFIG_FILE="$HOME/.zshrc" | |
elif [ "$SHELL_NAME" = "bash" ]; then | |
# For bash, prefer .bashrc, but use .bash_profile on macOS if .bashrc doesn't exist | |
if [ -f "$HOME/.bashrc" ]; then | |
SHELL_CONFIG_FILE="$HOME/.bashrc" | |
else | |
SHELL_CONFIG_FILE="$HOME/.bash_profile" | |
fi | |
else | |
print_error "Unknown shell: $SHELL_NAME. Only zsh and bash are supported." | |
exit 1 | |
fi | |
print_message "Using shell config file: $SHELL_CONFIG_FILE" | |
# Define shell aliases | |
SHELL_ALIASES=( | |
"alias g='git'" | |
"alias gst='git status'" | |
"alias ga='git add'" | |
"alias gaa='git add --all'" | |
"alias gc='git commit'" | |
"alias gcm='git commit -m'" | |
"alias gco='git checkout'" | |
"alias gb='git branch'" | |
"alias gd='git diff'" | |
"alias gpl='git pull'" | |
"alias gps='git push'" | |
"alias gl='git log'" | |
"alias glg='git lg'" | |
) | |
# Add aliases to shell config file, avoiding duplicates | |
for alias_def in "${SHELL_ALIASES[@]}"; do | |
if ! grep -q "^$alias_def$" "$SHELL_CONFIG_FILE"; then | |
echo "$alias_def" >> "$SHELL_CONFIG_FILE" | |
fi | |
done | |
print_success "✓ Shell aliases added to $SHELL_CONFIG_FILE" | |
# Instructions for the user | |
print_message "\nGit aliases have been set up successfully!" | |
print_message "To start using the new shell aliases in this terminal session, run:" | |
echo " source $SHELL_CONFIG_FILE" | |
print_message "\nAvailable Git aliases:" | |
echo "• git st / gst → git status" | |
echo "• git a / ga → git add" | |
echo "• git co / gco → git checkout" | |
echo "• git cob → git checkout -b (create and checkout branch)" | |
echo "• git br / gb → git branch" | |
echo "• git ci / gc → git commit" | |
echo "• gcm \"message\" → git commit -m \"message\"" | |
echo "• git amend → git commit --amend" | |
echo "• git unstage → git reset HEAD --" | |
echo "• git last → git log -1 HEAD (show last commit)" | |
echo "• git lg / glg → git log with pretty format" | |
echo "• git df / gd → git diff" | |
echo "• git dfs → git diff --staged" | |
echo "• git pl / gpl → git pull" | |
echo "• git ps / gps → git push" | |
echo "• g → git (shorthand for git command)" | |
# Set execute permissions on the script | |
chmod +x "$0" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment