Git/aliases
Git is a powerful tool with a lot of useful options. Did you know you can make an alias for a command and a set of options? You won't have to write long shell one-liners anymore. Below is a collection of some useful aliases.
How to set up an alias
editIn your ~/.gitconfig
add an [alias]
section.
Then each line is the <tab>key = value
format.
Below is a snippet of the [alias]
section of a .gitconfig
file
[alias]
short-name = actual (long?) command
Everything
editIf you want to get everything at once, here is a collection of all the aliases on this page:
[alias]
amend = commit --amend -a
br = branch
branch-cleanup = "!f() { git branch --merged ${1-master} | grep -v " ${1-master}$" | xargs -n 1 git branch -d; }; f"
co = checkout
ds = diff --staged
di = diff
fetchall = fetch -v --all
lg = log --graph --pretty=format:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(cyan)<%an>%Creset' --abbrev-commit --date=relative
l = ! git lg -n25
log-me = !UN=$(git config user.name)&& git log --author="\"$UN\"" --pretty=format:'%h %cd %s' --date=short
log-nice = log --graph --decorate --pretty=oneline --abbrev-commit
panic = !tar cvf ../git_panic.tar *
st = status
wdiff = diff --word-diff=plain
amend
edit[alias]
amend = commit --amend -a
- Example
git amend
With Gerrit you may have to correct a patchset. This usually means editing files and then having to correct the previous commit message. The workflow would be something like:
$ git review -d 1234 // gerrit changeset id <edit files> $ git add <files> $ git commit --amend <update commit-msg>
If you only changed files (not added new ones), the last two commands can be grouped by using git commit --amend -a
.
br
edit[alias]
br = branch
- Example
git br foo --track origin/foo
git br -D some/old/branch
branch-cleanup
edit[alias]
branch-cleanup = "!f() { git branch --merged ${1-master} | grep -v " ${1-master}$" | xargs -n 1 git branch -d; }; f"
This deletes all branches-references that are fully merged into master (or some other branch, if you specify).
co
edit[alias]
co = checkout
- Example
git co master
ds
edit[alias]
ds = diff --staged
- Example
git ds
A plain git diff
shows the diff between the (unstaged) working copy and the staged working copy.
In most cases this is the same as the diff between the working copy and the last committed revision of the current branch (HEAD).
However, if you have already done git add
and then made some more changes, then the changes at time of last git add
no longer show up in git diff
.
Use git diff --staged
to create a diff between the already staged (but uncommitted) changes and the current HEAD.
di
edit[alias]
di = diff
- Example
git di
fetchall
edit[alias]
fetchall = fetch -v --all
- Example
git fetchall
In this case, both origin
and gerrit
are the same, good enough for the above screenshot.
git remote update
lg
edit[alias]
lg = log --graph --pretty=format:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(cyan)<%an>%Creset' --abbrev-commit --date=relative
l = ! git lg -n25
- Example
git lg
(default, with pager) orgit l
(last 25 only)
log-me
editThe author name is case-sensitive.
[alias]
log-me = !UN=$(git config user.name)&& git log --author="\"$UN\"" --pretty=format:'%h %cd %s' --date=short
- Example
git log-me -n20
log-nice
edit[alias]
log-nice = log --graph --decorate --pretty=oneline --abbrev-commit
- Example
git log-nice -n20
panic
edit[alias]
panic = !tar cvf ../git_panic.tar *
Sometimes, you might have done something wrong in git. You think you've lost your commits, or something like that. Chances are, the information is still there — so the best course of action is to make an immediate backup, before you risk actually losing data.
st
edit[alias]
st = status
- Example
git st
wdiff
edit[alias]
wdiff = diff --word-diff=plain
- Example
git wdiff