Skip to content

Instantly share code, notes, and snippets.

@shadyvb
Created May 24, 2016 15:13
Show Gist options
  • Save shadyvb/b294034c8f268002241c255f6806c7d0 to your computer and use it in GitHub Desktop.
Save shadyvb/b294034c8f268002241c255f6806c7d0 to your computer and use it in GitHub Desktop.
Git Aliases
[alias]
# Merge the current branch to another one, created back when I needed to merge into three branches before issuing a PR
merge-to = "!f() { git checkout $1 && git merge $2 && git checkout $2; }; f"
# Commit shorthand family:
c = commit
cm = commit -m
cam = commit -am
camp = "!f() { git commit -am \"$1\"; git push; }; f"
ca = commit -a
# My favorite and top-most used one
st = status -sb
# Pretty log format with colors and one-liners
lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
# Export the whole repo to a zip file named after the current directory, date, and current SHA1 of HEAD
export = "!git archive `git rev-parse --abbrev-ref HEAD` -o ../`basename $PWD`-`date +%d%m%Y`-`git rev-parse --short HEAD`.zip"
# Just zip the repo contents and put it one directory above the repo base
zip = "!git archive `git rev-parse --abbrev-ref HEAD` -o ../`basename $PWD`.zip"
# See what files were changed since yesterday
changed = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative --since=yesterday --name-only
# Open changed files in ZendStudio
open-changed = "!~/_apps/ZendStudio/ZendStudio $(git status --short | awk '$1 ~ /^M$/ {print $2}')"
# Create a new git-flow branch off develop
in = "!f() { git checkout develop && git pull origin develop && ISSUENO=$1 && git checkout -b "issue/$ISSUENO"; }; f"
# Push a branch to origin, and issue a pull-request against develop branch. Uses the value from `git config pull-request.repo` to decide which remote to issue a PR to
ip = "!f() { BRANCHNAME=`git rev-parse --abbrev-ref HEAD` && [ $BRANCHNAME != 'develop' ] && git push origin $BRANCHNAME && hub pull-request -b develop -h `git config pull-request.repo`:$BRANCHNAME; }; f"
# Same as the above, just commits code first, mostly for no-brainer quick fixes
ic = "!f() { BRANCHNAME=`git rev-parse --abbrev-ref HEAD` && [ $BRANCHNAME != 'develop' ] && git ca && git push origin $BRANCHNAME && hub pull-request -b develop -h `git config pull-request.repo`:$BRANCHNAME; }; f"
# `git retire foo/bar` merges foor/bar branch to the current branch and deletes it
retire = "!f() { git merge $1 && git branch -d $1; }; f"
# Just issue a PR for the current branch, assumes it is pushed already
pr = "!f() { BRANCHNAME=`git rev-parse --abbrev-ref HEAD` && hub pull-request -b `git config pull-request.tobranch|cut -d'/' -f 2` -h `git config pull-request.repo`$BRANCHNAME; }; f"
# `git issue 21` Creates a new issue using a pre-set prefix ( using git config issues.prefix SOMETHING ) from the pre-set master branching ( from `git config pull-request.tobranch )
issue = "!f() { git checkout -b `git config issues.prefix`$1 `git config pull-request.tobranch`; }; f"
# Pulls the `preview` branch from origin, merges the current branch to it, pushes it back. Used for simple CI purposes where `preview` is auto deployed to a staging env
preview ="!f() { git push; git branch -D preview; git fetch; git checkout -b preview origin/preview; git merge - --no-edit; git push; git checkout -; }; f"
# A shorthand for setting the upstream branch for the current one, a lengthy command otherwise
track="!git branch --set-upstream-to origin `git rev-parse --abbrev-ref HEAD`"
# Prepare for a commit to VIP, does not actually commit, just prepares everything up till the commit stage
vip="!f(){ git checkout vip; svn up; svn revert --recursive .; git merge - --no-commit --no-ff; svn diff | more; }; f"
# `git search STH` Search for a certain keyword through all the repo history
search= "!f(){ git rev-list --all | xargs git grep $1; }; f"
# Reset all changes to file permissions
permission-reset = !git diff -p -R | grep -E \"^(diff|(old|new) mode)\" | git apply
# Gets the commits after the HEAD of `master`, tries to cherry-pick each one into the `preview` branch, instead of merging the whole branch into `preview`, very useful one.
prev-cherry="!f() { git rev-list --reverse --topo-order master... | while read rev; do git checkout preview; git cherry-pick $rev || break; done; }; f"
# A shorthand to get the current branch name, used for scripting
current= rev-parse --abbrev-ref HEAD
# `git conflicts SOMEBRANCH` Checks if the current branch will cause a conflict if merged to that passed branch, used for scripting as well
conflicts="!f() { git checkout $1 &> /dev/null; git merge --no-commit --no-ff - &> /dev/null || { echo \"Conflicted with $1\"; git checkout -f &> /dev/null; git checkout - &> /dev/null; }; }; f"
# Cleanup all the merged local branches, and removes all obsolete remote branches that were deleted in the remote
cleanup="!f(){ git fetch -p; git checkout master; for branch in `git branch --merged`; do git branch -d $branch; done; }; f"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment