Git/aliases

From MediaWiki.org
< Git
Jump to: navigation, search

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[edit]

In 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[edit]

If 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
	log-fancy = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(cyan)<%an>%Creset' --abbrev-commit --date=relative
	# With git >= 1.8.3 you can use %C(auto) for the branch decorator (%d)
	log-fancy = log --graph --pretty=format:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(cyan)<%an>%Creset' --abbrev-commit --date=relative
	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

Git fetchall alias.png

In this case, both origin and gerrit are the same, good enough for the above screenshot.

You can also use git remote update

lg[edit]

[alias]
        lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(cyan)<%an>%Creset' --abbrev-commit --date=relative
        l = ! git lg -n25
Example
git lg (default, with pager) or git l (last 25 only)

Git alias log-fancy.png

log-me[edit]

The 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

Git mylog alias.png

log-nice[edit]

[alias]
        log-nice = log --graph --decorate --pretty=oneline --abbrev-commit
Example
git log-nice -n20

Git alias log-nice.png

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