Skip to content

Instantly share code, notes, and snippets.

@markddavidoff
Last active April 5, 2024 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markddavidoff/43cdae787afb90c13e04fb77329c01f8 to your computer and use it in GitHub Desktop.
Save markddavidoff/43cdae787afb90c13e04fb77329c01f8 to your computer and use it in GitHub Desktop.
git shell plugins / functions / aliases
# warning these are hacky script thrown together with no care or understanding, so NO WARRANTY, USE AT YOUR OWN RISK
# you can install the git zsh plugin for more goodies, but these are some customized versions
# git zsh plugin: https://github.com/ohmyzsh/ohmyzsh/blob/master/plugins/git/git.plugin.zsh
# also check out grecent
alias fetch='git fetch --all'
alias switch='git checkout'
alias switchb='gcb'
alias pull='ggpull'
alias branch='gcb'
alias push='ggpush'
alias forcepush='ggfl'
alias recentbranches="git for-each-ref --sort=-committerdate --count=10 --format='%(refname:short)' refs/heads/"
alias switchback='git checkout @{-1}'
function switchm() {
# switch to master or main
mbranch=$((git rev-parse -q --verify master > /dev/null && echo "master") || (git rev-parse -q --verify main> /dev/null && echo "main"))
git checkout "$mbranch"
}
function switchprev() {
# switch to previous non-master/main branch
i=0
current_branch=$(git rev-parse --abbrev-ref HEAD)
while [ true ]
do
i=$(( $i + 1 ))
prev=$(git rev-parse --abbrev-ref @{-$i})
if [[ "$prev" == "master" ]]; then
continue
fi
if [[ "$prev" == "main" ]]; then
continue
fi
if [[ "$prev" == "$current_branch" ]]; then
continue
fi
break
done
git checkout $prev
}
function rbm() {(
# rebase current branch on most recent master/main branch.
# lots of ways to do this, this is a lazy one.
set -e
curr_branch=$(git rev-parse --abbrev-ref HEAD)
git checkout main || git checkout master
main_branch=$(git rev-parse --abbrev-ref HEAD)
git pull origin $main_branch
git checkout $curr_branch
git rebase $main_branch
)}
@markddavidoff
Copy link
Author

markddavidoff commented Apr 5, 2024


alias fetch='git fetch --all'
alias switch='git checkout'
alias switchb='gcb'
alias pull='ggpull'
alias branch='gcb'
alias push='ggpush'
alias forcepush='ggfl'
alias recentbranches="git for-each-ref --sort=-committerdate --count=10 --format='%(refname:short)' refs/heads/"
alias switchback='git checkout @{-1}'
alias kube-update-staging="aws eks --region us-east-1 update-kubeconfig --name staging"
alias kube-update-prod="aws eks --region us-east-1 update-kubeconfig --name production"
export STAGING_CLUSTER="arn:aws:eks:us-east-1:477832038283:cluster/staging"
function switchm() {
    mbranch=$((git rev-parse -q --verify master > /dev/null && echo "master") || (git rev-parse -q --verify main> /dev/null && echo "main"))
    git checkout "$mbranch"
}

function switchprev() {
    i=0
    current_branch=$(git rev-parse --abbrev-ref HEAD)
    while [ true ]
    do
        i=$(( $i + 1 ))
        prev=$(git rev-parse --abbrev-ref @{-$i})
        if  [[ "$prev" == "master" ]]; then
            continue
        fi
        if  [[ "$prev" == "main" ]]; then
            continue
        fi
        if  [[ "$prev" == "$current_branch" ]]; then
            continue
        fi
        break
    done
    git checkout $prev 
}   

function rbm() {( 
    set -e  
    curr_branch=$(git rev-parse --abbrev-ref HEAD) 
    git checkout main || git checkout master
    main_branch=$(git rev-parse --abbrev-ref HEAD) 
    git pull origin $main_branch
    git checkout $curr_branch 
    git rebase $main_branch
)}  

function rbmi() {(  
    set -e   
    curr_branch=$(git rev-parse --abbrev-ref HEAD)  
    git checkout main || git checkout master 
    main_branch=$(git rev-parse --abbrev-ref HEAD)  
    git pull origin $main_branch 
    git checkout $curr_branch  
    git rebase -i $main_branch
)}


function rbbase() {(
    set -e
    prev=$(getbasebranch)
    
    echo -e "prev:$prev"
    curr_branch=$(git rev-parse --abbrev-ref HEAD)
    echo "Are you sure you want to rebase\n\u001b[32m* $curr_branch\n\u001b[0m↓ onto ↓\n\u001b[31m* $prev\u001b[0m?\n[y/n]"
    if read -q "yn?"; then
        echo "\n\u001b[32mrebasing\u001b[0m"
        git checkout $prev
        prev_branch=$(git rev-parse --abbrev-ref HEAD)
        git pull origin $prev_branch
        git checkout $curr_branch
        git rebase $prev_branch
    else
        echo "\n\u001b[31maborting\u001b[0m"
    fi

)}

function rbbasei() {(
    set -e
    prev=$(getbasebranch)
    curr_branch=$(git rev-parse --abbrev-ref HEAD)
    echo "Are you sure you want to rebase\n\u001b[32m* $curr_branch\n\u001b[0m↓ onto ↓\n\u001b[31m* $prev\u001b[0m?\n[y/n]"
    if read -q "yn?"; then
        echo "\n\u001b[32mrebasing\u001b[0m?"
        git checkout $prev
        prev_branch=$(git rev-parse --abbrev-ref HEAD)
        git pull origin $prev_branch
        git checkout $curr_branch
        git rebase -i $prev_branch
    else
        echo "\n\u001b[31maborting\u001b[0m?"
    fi

)}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment