Updated on 2021-06-12: Made the sample files non-hidden by removing the leading dot because octopress has suddenly stopped copying those to the public/ directory.
As you should already know, git is a pretty awesome version control system. And you can make it even more cool with the help of different aliases that will speed up your work. Also, your shell is your friend, more is below.
[alias]# edit the commit message showing the whole diffc=commit -v# more concise statuss=status --short --branch# shows diff with words in green and red instead of the whole linesd=diff --word-diff=color# the same, but for staged changesdc=diff --word-diff=color --cached --find-renames# pretty graphical log of the whole repository with labelsl=log --graph --abbrev-commit --pretty=oneline --all --decorate=short --find-renames# ditto with commit timel2=log --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset' --abbrev-commit --graph --decorate=short --all# usual log plus shows tags, branches, etc.lg=log --decorate=short# merge w/o fast forwardm=merge --no-ff# add changes on a change-by-change basis (I always use this alias)a=add --patch# create and switch to a new branchcb=checkout -b# the opposite of the a aliascp=checkout --patch# short for checkoutco=checkoutch=checkout# get changes from remote, and prune branchesf=fetch --prune# stands for 'push new branch'pnb=push -u origin HEAD# show the list of files with conflictsconflicts=!git ls-files --unmerged | cut -f2 | uniq# edit the conflicted files in an editoredit-unmerged="!f() { git ls-files --unmerged | cut -f2 | sort -u ; }; vim `f`"# add the resolved filesadd-unmerged="!f() { git ls-files --unmerged | cut -f2 | sort -u ; }; git add `f`"# generate source for a graphviz graph, use like this: git graphviz | dotty -graphviz="!f() { echo 'digraph git {' ; git log --pretty='format: %h -> { %p }' \"$@\" | sed 's/[0-9a-f][0-9a-f]*/\"&\"/g' ; echo '}'; }; f"# starts an ad-hoc git server with this repositoryserve=!git daemon --reuseaddr --verbose --base-path=. --export-all ./.git
For additional acceleration and convenience, I’ve defined very short shell aliases like gs for git status, etc. This is an excerpt of my ~/.sh_aliases file where I keep my shell aliases:
# detect current shellSH=$( ps -p $$| awk 'END {print $NF}')# alias g for git in bash only, as oh-my-zsh's git plugin does it for youif[["$SH"=="bash"]];thenaliasg='git'# completion doesn't work with the alias# it works now http://nuclearsquid.com/writings/git-tricks-tips-workflows/complete -o default -o nospace -F _git g
fi# git aliasesfor cmd in c s d l m a f dc
doalias"g${cmd}"="git ${cmd}"done
First, the file works for both bash and zsh, so you include it this way: [ -r ~/.sh_aliases ] && source ~/.sh_aliases in ~/.bashrc or ~/.zshrc respectively.
Second, I use great oh-my-zsh pack, which enables the g alias for git, so I have to do it myself in bash only.
That’s it for today. Do you know any other git tips and tricks?