Using Git Like a Pro – aliases

1 minute read

Aliases are created through the use of the git config command, and will be saved to $HOME/.gitconfig, use below two commands to see a list of aliases:

git config --global -e
git config --get-regexp alias

Or, make it an alias with:

git config --global alias.alias 'config --get-regexp alias'

Using alias can save a lot of time by typing less characters, here is my list of handy aliases:

git config --global alias.st 'status -s'
git config --global alias.co 'checkout'

# branch
git config --global alias.br branch
git config --global alias.contains 'branch --contains'

# commit
git config --global alias.ci 'commit -v'
git config --global alias.cm 'commit -m'
git config --global alias.ca 'commit --amend'
git config --global alias.ac '!git add . && git commit -as'
git config --global alias.amend '!git add -u && git commit --amend --no-edit'
git config --global alias.this '!git init && git add . && git commit -m \"initial commit\"'

# diff
git config --global alias.dc 'diff --cached'
git config --global alias.ds 'diff --stat'

# cleanup
git config --global alias.cleanup '!git clean -fd && git checkout -- .'

# history
git config --global alias.last 'log -1 HEAD'
git config --global alias.ls 'log --pretty=oneline --decorate --graph --abbrev-commit --all'
git config --global alias.mine 'log --pretty=format:"%C(yellow)%h %C(green)%ad %C(red)%d %Creset%s" --decorate --date=short --author=Fudong'

git config --global alias.lrd 'log --pretty=format:"%C(yellow)%h %C(green)%>(12,trunc)%ad %C(dim cyan)%cn %C(red)%d %Creset%s" --decorate --date=relative'
git config --global alias.lsd 'log --pretty=format:"%C(yellow)%h %C(green)%ad %C(dim cyan)%cn%C(red)%d %Creset%s" --decorate --date=short'
git config --global alias.today 'log --pretty=format:"%C(yellow)%h %C(green)%ad %C(dim cyan)%cn %C(red)%d %Creset%s" --date=local --since=midnight'

git config --global alias.lrg 'log --pretty=format:"%C(yellow)%h %C(green)%>(12,trunc)%ad %C(dim cyan)%cn %C(red)%d %Creset%s" --decorate --date=relative --graph'
git config --global alias.lsg 'log --pretty=format:"%C(yellow)%h %C(green)%ad %C(dim cyan)%cn%C(red)%d %Creset%s" --decorate --date=short --graph'


# remove already deleted files from git
git config --global alias.del '!git ls-files -z --deleted | xargs -0 git rm'

# ignore a file
git config --global alias.ignore '!f() { echo $1 >> .gitignore; }; f'

# misc
git config --global alias.unstage 'reset HEAD --'

# Push changes to the current remote branch
git config --global alias.pc '!git push origin $(git rev-parse --abbrev-ref HEAD)'

git config --global alias.xx '!git reset $(git merge-base master $(git rev-parse --abbrev-ref HEAD))'

# see all git aliases
git config --global alias.alias 'config --get-regexp alias'

References

Tags: ,

Categories:

Updated: