KISS

Keep It Simple Stupid

My git aliases

| comments

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.

Here’s a list of mine with some description:

(.gitconfig) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
[alias]

    # edit the commit message showing the whole diff
    c = commit -v

    # more concise status
    s = status --short --branch

    # shows diff with words in green and red instead of the whole lines
    d = diff --word-diff=color
    # the same, but for staged changes
    dc = diff --word-diff=color --cached --find-renames

    # pretty graphical log of the whole repository with labels
    l = log --graph --abbrev-commit --pretty=oneline --all --decorate=short --find-renames
    # ditto with commit time
    l2 = 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 forward
    m = 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 branch
    cb = checkout -b
    # the opposite of the a alias
    cp = checkout --patch
    # short for checkout
    co = checkout
    ch = checkout

    # get changes from remote, and prune branches
    f = fetch --prune
    # stands for 'push new branch'
    pnb = push -u origin HEAD

    # show the list of files with conflicts
    conflicts = !git ls-files --unmerged | cut -f2 | uniq
    # edit the conflicted files in an editor
    edit-unmerged = "!f() { git ls-files --unmerged | cut -f2 | sort -u ; }; vim `f`"
    # add the resolved files
    add-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 repository
    serve = !git daemon --reuseaddr --verbose  --base-path=. --export-all ./.git

The last block of aliases was found among other ones here: https://git.wiki.kernel.org/index.php/Aliases.

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:

(.sh_aliases) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# detect current shell
SH=$( ps -p $$ | awk 'END {print $NF}' )

# alias g for git in bash only, as oh-my-zsh's git plugin does it for you
if [[ "$SH" == "bash" ]]; then
    alias g='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 aliases
for cmd in c s d l m a f dc
do
    alias "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?

Comments