Skip to content

Instantly share code, notes, and snippets.

@connoro7
Last active October 23, 2020 07:13
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 connoro7/05a8a4a2f81223509ea439575cbf252c to your computer and use it in GitHub Desktop.
Save connoro7/05a8a4a2f81223509ea439575cbf252c to your computer and use it in GitHub Desktop.
Collection of git aliases and functions
###############################################################
Please see "PS1 upgrades, with COLORS!" gist for color variables.
https://gist.github.com/connoro7/8c37fb4fb6abd6bca0c5cc13259e30c7
###############################################################
## Git
## add
alias ga='git add'
alias gaa='git add -A && git status'
## branch
alias gb='git branch'
alias gba="git branch -a"
alias gbuoh='git branch -u origin/$(grph)'
alias gbpp='git log -r --oneline --no-merges --simplify-by-decoration --pretty=format:"%n %Cred CommitID %Creset: %h %n %Cred Remote Branch %Creset :%d %n %Cred Commit Message %Creset: %s %n"'
## branch management
gbmake() { NEW_BRANCH="$1" ; git branch $NEW_BRANCH && git checkout $NEW_BRANCH ; }
gbrename() {
read -p 'Rename which branch? [this branch]: ' OLD_NAME ;
OLD_NAME=${OLD_NAME:-`git rev-parse --abbrev-ref HEAD`}
read -p 'Enter new branch name: ' NEW_NAME &&
git branch --move $OLD_NAME $NEW_NAME &&
git push --set-upstream origin $NEW_NAME ;
git branch -a ;
echo -e "${BYellow}Please remove the old branch on remote${Color_Off}" ;
#git push origin --delete $OLD_NAME ;
}
gbmerged() {
echo -e "${BGreen}> Merged branches:${Color_Off}"
git branch --merged
echo -e "${BRed}> Unmerged branches:${Color_Off}"
git branch --no-merged
}
## branch cleanup
alias gbdl='echo -e "$Red>About to delete a local branch...$Color_Off" && confirm && git branch -d $1'
alias gbdlF='echo -e "$Red>About to $Warning FORCE $Red delete a local branch...$Color_Off" && confirm && git branch -D $1'
alias gbdr='echo -e "$Red>About to delete remote branch...$Color_Off" && confirm && git push $1 --delete $2'
## commit
alias gc='git commit'
alias gcane='git commit --amend --no-edit'
alias gcanenv='gcane --no-verify'
alias gcm='git commit -m'
alias gcms='git commit -S -m'
alias gcmwip='git add -A && git commit -m "WIP" --no-verify'
## diff
alias gdhns='git diff HEAD~ HEAD --name-status'
alias gdi='git diff --ignore-space-at-eol'
## fetch
alias gf='git fetch --all --prune -v'
alias gfom='git fetch origin master:master -v'
## pull
alias gpull="git pull"
alias gpullom='git pull origin master'
alias gpullomd='gch HEAD --detach && git fetch origin master:master && gch master'
alias gpullohr='glp -5 && git pull origin $(grph) --rebase'
fix-failed-ff-abort() {
# Workaround solution for instances where a `git pull` aborts due to failed fast-forwarding
git pull origin "$1" --rebase ;}
## push
alias gpush="git push"
alias gpush+='git push origin'
alias gpush++='git push origin $(grph)'
## stash
alias gsa='git stash apply'
alias gsl='git stash list'
alias gss='git stash save'
## log
alias gl='git log --decorate --abbrev-commit'
alias glg='git log --decorate --abbrev-commit --graph'
alias glgp='git log --decorate --abbrev-commit --graph --pretty=oneline'
alias glp='git log --decorate --abbrev-commit --pretty=oneline'
## rev-parse
alias grph='git rev-parse --abbrev-ref HEAD'
alias grphc='echo $(git rev-parse --abbrev-ref HEAD) | pbcopy'
## misc.
alias gch='git checkout'
alias gd='git diff'
alias gs='git status'
## undo
alias undo-commit='echo -e "$Red>Undo most recent commit?$Color_Off" && confirm && git reset --soft HEAD~1 && echo -e "$Green>Done$Color_Off"'
alias undo-changes='echo -e "$Red>Discard changes since last commit?$Color_Off" && confirm && git reset --hard HEAD~1 && echo -e "$Green>Done$Color_Off"'
## Functions
#
gcht() {
# Fetches the branch at remote, then checks it out and sets it as the upstream branch.
REMOTE="$1" ; BRANCH="$2" ; git fetch $REMOTE $BRANCH && git checkout -t "$REMOTE/$BRANCH"; }
git-amend-current-commit-date() {
# Amends date of most recent commit, using format: "Sat May 19 01:01:01 2019 -0700"
# -0800 for PST, -0700 for PDT; PDT observed 2nd Sunday in March thru 1st Sunday in November
GIT_COMMITTER_DATE="$1" git commit --amend --date="$1" --no-edit ; }
git-view-changes() { FROM="$1" ; TO="$2" ; git checkout $TO && git reset $FROM --soft ; }
code-review() { BRANCH="$1" ; git fetch origin master && git checkout code-review && git reset $BRANCH --hard && git reset origin/master --soft ; }
init-repo() {
# Initializes a new git repo at the current working directory
echo -e "${IGreen}Initializing git repository...$Color_Off"
read -p "Please enter remote URL: " REPO
read -p "Enter the remote name ${Yellow}[origin]${Color_Off}: " REMOTE
REMOTE=${REMOTE:-origin}
git init && git remote add $REMOTE $REPO && git remote --v && git fetch --all --prune -v && git pull $REMOTE master ; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment