Aliases
From Git SCM Wiki
Aliases
With aliases, you can avoid typing the same commands over and over again. Aliases were added in Git version 1.4.0.
Table of contents:
Contents |
Introduction
To show how to use aliases, suppose you wish you could write git ci instead of git commit. You can achieve this by adding the following section to .git/config:
[alias]
ci = commit
Some people are uncomfortable editing the config file themselves. They can edit the config by calling git config alias.ci commit instead.
If you want to have the alias available everywhere on your local machine, you can either
- edit $HOME/.gitconfig, or
- use the --global flag: git config --global alias.ci commit
Simple
A shortcut for seeing the fetched commits
If you want to be able to say git lc to list all new commits after you fetched, with stats, but excluding merges, add this alias:
[alias]
lc = log ORIG_HEAD.. --stat --no-merges
Undoing any change in the current branch
Note that this is dangerous!
[alias]
undo=reset --hard
Aliases with arguments
Right from the start, aliases were meant as an easy way to avoid really simple, really short scripts. Therefore, they can take arguments, which are appended to the command.
'ci'
That's right. The simplest alias on this page takes arguments, so you can call:
$ git ci -m message file1 file2 dir1
Shortcut for displaying dates in your local timezone
Here's how to create an alias git llog that will behave just like git log, but will display dates in your local timezone.
[alias]
llog = log --date=local
Simple diff wrappers
These two aliases wrap commonly used options to git diff, and accept the full range of arguments that diff accepts:
[alias]
changes=diff --name-status -r
diffstat=diff --stat -r
Advanced
Since version 1.5.0, Git supports aliases executing non-git commands, by prefixing the value with "!":
Calling "gitk"
Since gitk does not follow the common form git-<name>, and is no builtin either, you have to use the prefix "!" to call gitk from an alias:
[alias]
gitkconflict = !gitk --left-right HEAD...MERGE_HEAD
Poor man's "stash"
A concatenation of git programs can also be achieved by the prefix "!":
[alias]
stsh = !CURRENT=$(git symbolic-ref HEAD) && git symbolic-ref HEAD refs/heads/stash && git commit -a -m stashed && git checkout $CURRENT
Serve repo on the spot
This fires up a git daemon for the repo you are currently in:
[alias]
serve = !git daemon --reuseaddr --verbose --base-path=. --export-all ./.git
It makes use of the fact that (currently, as of git 1.5.6.1) non-git alias are executed from the top-level dir of a repo. The simpler version
[alias]
serve = daemon --reuseaddr --verbose --base-path=. --export-all ./.git
works only when called from the top-level dir. In any case, you can connect simply by git ls-remote git://127.0.0.1/ etc.
Prune all your stale remote branches
There's no way to tell git remote update to prune stale branches, and git remote prune does not understand --all. So here is an alias to do the job:
[alias]
prune-all = !git remote | xargs -n 1 git remote prune
Advanced aliases with arguments
Starting with version 1.5.3, git supports appending the arguments to commands prefixed with "!", too. If you need to perform a reordering, or to use an argument twice, you can use this trick:
[alias]
example = !sh -c 'ls $2 $1' -
The final dash is so that arguments start with $1, not with $0.
NOTE: later on the page presents a nice trick using a shell function instead of sh -c. Most aliases could be converted to use that style.
Spelunking of the project's history
Here are two aliases suggested on the mailing list by Junio Hamano:
[alias]
whois = "!sh -c 'git log -i -1 --pretty=\"format:%an <%ae>\n\" --author=\"$1\"' -"
whatis = show -s --pretty='tformat:%h (%s, %ad)' --date=short
Try then by yourself! The first takes the name of a person or their email address. The second takes a commit name.
A 'debug' alias to help debugging builtins
When debugging builtins, you often use gdb to analyze the runtime state. However, you have to disable the pager, and often you have to call the program with arguments. If the program to debug is a builtin, you can use this alias:
[alias]
debug = !GIT_PAGER= gdb --args git
Suppose you want to debug git log HEAD..next, you can call gdb by git debug log HEAD..next now.
Calling "interdiff" between commits
If upstream applied a slightly modified patch, and you want to see the modifications, you should use the program interdiff of the patchutils package. Then you can add the alias intercommit:
[alias]
intercommit = !sh -c 'git show "$1" > .git/commit1 && git show "$2" > .git/commit2 && interdiff .git/commit[12] | less -FRS' -
This accept two commits, typically the first coming from upstream (e.g. origin/master) and the second coming from your own topic branch.
Collection of aliases by Git users
Here is a collection of some Git users' aliases. If you know a simpler way to achieve the same, please add some notes.
Getting the diff of only one function
When you want to see just the differences of one function in one file in two different commits, you can do this:
$ git config alias.funcdiff '!sh -c "git show \"\$1:\$3\" | sed -n \"/^[^ \t].*\$4(/,/^}/p\" > .tmp1 &&
git show \"\$2:\$3\" | sed -n \"/^[^ \t].*\$4(/,/^}/p\" > .tmp2 &&
git diff --no-index .tmp1 .tmp2"' -
The idea is to create two temporary files which contain only the function, and call git diff on them. Use this alias this way: git funcdiff <old-rev> <new-rev> <path> <function>.
Editing/adding conflicted files
You get a lot of merge conflicts and want to quickly solve them using an editor and then add the conflicted files. Try this:
[alias]
edit-unmerged = "!f() { git ls-files --unmerged | cut -f2 | sort -u ; }; vim `f`"
add-unmerged = "!f() { git ls-files --unmerged | cut -f2 | sort -u ; }; git add `f`"
You should replace "vim" by your favorite editor.
Then just use
$ git edit-unmerged ... edit ... $ ... test ... $ git add-unmerged $ git commit # or git rebase --continue or whatever
Use graphviz for display
[alias]
graphviz = "!f() { echo 'digraph git {' ; git log --pretty='format: %h -> { %p }' \"$@\" | sed 's/[0-9a-f][0-9a-f]*/\"&\"/g' ; echo '}'; }; f"
This produces output that can be displayed using dotty, for example:
$ git graphviz HEAD~100..HEAD~60 | dotty /dev/stdin $ git graphviz --first-parent master | dotty /dev/stdin
Note how defining a function eliminates the need to use sh -c.
Shortcuts
[alias]
st = status
ci = commit
br = branch
co = checkout
df = diff
lg = log -p
who = shortlog -s --
git k
If you use gitk in your git sessions quite frequently, you have perhaps misused your history and done:
$ gitk foo..bar $ gitk checkout baz
Using git k instead of gitk may solve your problem:
[alias]
k = !gitk
alias
Now that you know all about aliases, it might be handy to define some, using an alias:
[alias]
alias = !sh -c '[ $# = 2 ] && git config --global alias.\"$1\" \"$2\" && exit 0 || echo \"usage: git alias <new alias> <original command>\" >&2 && exit 1' -
then define new aliases with:
$ git alias new_alias original_command
Sending multiple messages from a single file
This can be useful to use send-email on the output of git format-patch --stdout
[alias]
send-mbox = "!bash -c 'eval f=\\$$#; eval set -- `seq -f\"\\$%.0f\" 1 $(($#-1))`; mkdir .mboxsplit || exit; trap \"st=\\$?; rm -rf .mboxsplit; exit \\$?\" 0 INT TERM; if last=`git mailsplit -d4 -o.mboxsplit -b -- \"$f\"`; then echo Found $last messages in \"$f\"; git send-email \"$@\" .mboxsplit; fi' -"
Most of the complication is because the last argument must be passed to git mailsplit, while the others must be passed to git send-email.
