git notes main page | gitolite main page | license

IMPORTANT NOTE: although this page has a "gitolite.com" URL, this is not about gitolite. That's just an artifact of "sitaramc.github.com" being translated to "gitolite.com" and so ALL my git related stuff gets carried over. Gitolite documentation has another /gitolite in the URL, so you can tell. My apologies for this confusion.

tips for the beginner

set up your gitconfig

This is $HOME/.gitconfig. Whenever you add the --global option to the git config command, it acts on the global gitconfig file.

Setup your identity

git config --global user.email your.name@your.com
git config --global user.name "your name"

Setup to edit files from within “git gui”; replace “gvim -f” with whatever you prefer

git config --global guitool.Edit.cmd 'gvim -f $FILENAME'
git config --global guitool.Edit.noconsole yes
git config --global guitool.Edit.needsfile yes

If you intend to use the command line more than the GUI, please do check out my gitconfig (at the end of this page) for many useful examples that may save you lots of time and trouble.

my ~/.gitconfig file

This is as of Dec 2013. One of these days I’ll stop being lazy and explain some of them :-)

[user]
    name = Sitaram Chamarty
    email = sitaram@atc.tcs.com
[color]
        ui = true
[core]
    pager = less -R
    compression = 6

[rerere]
    enabled = 1
[diff]
    mnemonicprefix = true
    tool = kdiff3
[merge]
    tool = kdiff3
[gui]
    editor = gvim
[guitool "edit"]
    cmd = gvim -f $FILENAME
    noconsole = yes
    needsfile = yes
[guitool "mergetool"]
    cmd = git mergetool -y -t kdiff3 $FILENAME
    noconsole = yes
    needsfile = yes
[push]
    default = matching
[mergetool]
    keepBackup = false

# everyday ones
[alias]
    s   = status -s -b -uno

    b   = branch
    ba  = branch -a -v -v
    bc  = !git-branch-check

    ci  = commit -v -uno
    co  = checkout

    d   = diff  -C
    dw  = diff  -C -w
    ds  = diff  -C --stat
    dsp = diff  -C --stat -p
    dcw = diff  -C --word-diff

    du  = diff  -C @{u}
    dwu = diff  -C -w @{u}
    dcwu= diff  -C --word-diff @{u}

    l   = log   -C --decorate
    ls  = log   -C --stat --decorate
    lsp = log   -C --stat -p --decorate

    lgbw= log   --graph --boundary '--format=%h %ar %d %s'
    lg  = log   --graph --boundary '--format=%Cblue%h%Creset %Cgreen%ar%Creset %Cblue%d%Creset %s'
    lga = log   --graph --boundary '--format=%Cblue%h%Creset %Cgreen%ar%Creset %Cblue%d%Creset %s' --all
    # this is the most commonly used one, it turns out!
    l19 = log   --graph --boundary '--format=%Cblue%h%Creset %Cgreen%ar%Creset %Cblue%d%Creset %s' --all -19
    # and this one can be a real lifesaver on a complex tree
    lsd = log   --graph --boundary '--format=%Cblue%h%Creset %Cgreen%ar%Creset %Cblue%d%Creset %s' --all --simplify-by-decoration
    # log left-right; requires argument like A...B (3 dots)
    lglr= log   --graph --boundary '--format=%Cblue%h%Creset %Cgreen%ar%Creset %Cblue%d%Creset %s' --boundary --left-right --cherry-pick

    # this is so frequent for me...
    pom = push origin master
    # pushall = "!f() { git remote | map -p git push; }; f"
    pall = !git remote | map -p git push

    rl  = reflog show --date=relative

    ru  = remote update

    sl  = stash list
    sp  = stash pop
    ss  = stash save
    # "show-branch -g=N" can't be aliased for N easily, so we stop here:
    sb  = show-branch
    # pg sbt master pu -- if the last line is 'master' and not 'master^'
    # or 'master~N' then pu is a descendent of master
    sbt = show-branch --topics
    sbs = show-branch --sha1-name

    ka      = !gitk --all
    kdo     = !gitk --date-order
    kado    = !gitk --all --date-order
    kasd    = !gitk --all --simplify-by-decoration

    z   =   squirrel --repo=/home/sitaram/imli/tech/repos/squirrel
    nb  =   squirrel --repo=/home/sitaram/imli/tech/repos/notebook

# exotic ones
[alias]
    # checkpoint and checkpoint recover
    cp      = !git stash save $(date +%F--%T) && git stash pop --index
    cpr     = "!f() { git fsck | grep commit | cut -d' ' -f3 | while read hash; do git rev-parse --verify --no-revs --quiet $hash^2 2>/dev/null && echo $hash | xargs git rev-list --since=${1:-1day} -1; done | xargs -L 1 git log --format=%at:%h -1 | sort|cut -f2 -d: | xargs -L 1 git log --graph --oneline --format='%Cblue%h %Cgreen(%ar) %Creset%s' -3 ; }; f"

    # when was this file last updated, on each local branch
    wwflu = "!f() { for b in $(git rev-parse --symbolic --branches); do echo -e `git log --format=%at:%ar -1 $b -- \"$1\"`\\\\t$b; done | sort -r |cut -f2 -d: ; }; f"

    # branch fast-forward: update from upstream if it's a fast-forward
    # (obsoleted now by 'git-branch-check but keep it just in case)
    # bff = "!f() { for i; do [[ $(git rev-parse $i) == $(git merge-base $i $i@{upstream}) ]] && git checkout $i && git merge $i@{upstream} && git checkout -; done; }; f"

    graphviz = "!f() { echo 'digraph git {' ; git log --pretty='format: %h -> { %p }' \"$@\" | sed 's/[0-9a-f][0-9a-f]*/\"&\"/g' ; echo '}'; }; f"

    cat   = -p cat-file -p

    top = !eval cd "$(pwd)/$(git rev-parse --show-cdup)" && pwd

# exotic ones that I might forget they exist; keep them at the end of the file
# for 'cat' or 'tail' to show them easily
[alias]
    ls-del  = ls-files -d
    ls-mod  = ls-files -m   # this will include deleted files also
    ls-new  = ls-files --exclude-standard -o
    # this one is a MUST have
    ls-ign  = ls-files --exclude-standard -o -i
    lsfiles = ls-files --exclude-per-directory=.gitignore \
                     --exclude-from=.git/info/exclude \
                     -v -d -o -m -s -u

    sk = !gitk --date-order $(git stash list | cut -d: -f1) --not --branches --tags --remotes

    d1 = !gitk --date-order $(git log -g --pretty=%H) --not --branches --tags --remotes
    d2 = !gitk --date-order $(git fsck | grep "dangling.commit" | cut -f3 -d' ') --not --branches --tags --remotes