abhi's logbook

Adventures in coding and coding for adventures.

Commit Messages

~/.gitconfig

The following is my ~/.gitconfig file which makes using git a bit
more enjoyable. It provides color on diffs, shortcuts for commonly
used commands and other goodies.

[user]
    email = [email protected]
    name = Abhi Yerra
[alias]
    ap = add -p
    cl = clone --recursive
    st = status
    stat = status -sb
    sb = show-branch
    ci = commit
    co = checkout
    ch = checkout
    chekcout = checkout
    br = branch
    df = diff
    dt = difftool -y
    lg = log -p --stat
    rev = rev-parse HEAD
    wc = whatchanged --stat
    wd = diff --word-diff
    up = remote -v update -p
    prb = pull --rebase
    sub = submodule
    subup = submodule update --init
    remotes = remote -v show
    remup = remote update --prune
    # remtrack = !git branch -vv | ruby -ne '$_ =~ /\s+([^\s]+)\s.*\[([^\]]+)/; branch, remote = $1, $2; `git show #{remote} 2>&1 >/dev/null`; puts "#{branch} -> #{remote}, remote_exists? = #{$?==0}";'
    m  = !B=$(git branch | grep '*' | awk '{print $2}') && R=$(git config branch.$B.remote) && RB=$(git config branch.$B.merge | cut -d/ -f3-) && git merge $R/$RB
    d  = !B=$(git branch | grep '*' | awk '{print $2}') && R=$(git config branch.$B.remote) && RB=$(git config branch.$B.merge | cut -d/ -f3-) && git wc $B..$R/$RB
    rd = !B=$(git branch | grep '*' | awk '{print $2}') && R=$(git config branch.$B.remote) && RB=$(git ) && git wc $R/$RB..$B
    human = name-rev --name-only --refs=refs/heads/*
    humin = name-rev --name-only --refs=refs/heads/* --stdi
    change-commits = "!f() { VAR=$1; OLD=$2; NEW=$3; shift 3; git filter-branch --env-filter \"if [[ $`echo $VAR` = \\\"$OLD\\\" ]]; then export $VAR=\\\"$NEW\\\"; fi\" [email protected]; }; f "
[color]
    ui = auto
    diff = auto
    status = auto
    branch = auto
    interactive = auto
    current = yellow reverse
    local = yellow
    remote = green
[color "diff"]
    whitespace = red reverse
    frag = cyan
    old = red bold
    new = green bold
[color "status"]
    added = green
    changed = yellow
    untracked = red
[core]
    excludesfile = ~/.gitignore-global
    whitespace=trailing-space,cr-at-eol
[apply]
    whitespace = nowarn
[push]
    default = tracking
[branch]
    autosetupmerge = true
[merge]
    stat = true
[status]
    submodulesummary = true
[mergetool]
        keepBackup = true
[github]
        user = abhiyerra
[filter "media"]
        clean = git-media-clean %f
        smudge = git-media-smudge %f

Branches

This will show the remote branches.

$ git branch -r

Adds a remote repo other then origin. Do a git-fetch to pull the
updated changes. This will allow you to track multiple repos at the
same time and merge from each which is pretty sweet.

$ git remote add linux-nfs git://linux-nfs.org/pub/nfs-2.6.git
$ git fetch linux-nfs

Say you are on a local branchname and you want to
push it to a remote server you can do it via the following.

$ git push -u origin branch_name

To delete that remote branch do:

$ git push origin :branch_name

This removes the remote branch without warning so use with care.

If you want to branch off a different remote branch do the following:

$ git checkout -b a-branch-that-tracks-a-remote --track origin/remote_branch

Set a remote branch for the current branch.

$ git branch --set-upstream-to upstream/master

Check how far behind or forward a branch is from remote

$ git branch -vv

Bisect

If there is a regression the best way to check at what commit the error
occured is through git-bisect

To start the process

$ git bisect start # starts the bisecting process
$ git bisect good <tag/commit>
# Picks a version where the error didn't occur
$ git bisect bad master # Start bisecting from current branch.

For each version see if it is bad do:

$ git bisect bad

At the end it will show where the error occured when doing this process.
will end the bisecting process.

$ git bisect reset

Logs and Blames

Shows the log with the diffs as well as what files have changed.

$ git log -p --stat

Adding Patches to Staging.

If you want to add individual patches from a file without adding the
whole file.

$ git add -p

Generate TAGS file using ctags

If I put this in my .git/hooks/post-commit it generates a TAGS file
after each commit.

$ git ls-files | xargs /usr/local/bin/ctags -e -a &> /dev/null &

Squashing commits

$ git remote update
$ git pull --rebase upstream master
$ git rebase -i upstream/master

Grepping commits

$ git log --grep=word

Have a branch with unrelated changes

I want to pull changes from say an old version of the project to my
current project but under a branch that is unrelated.

git remote add old <path>
git checkout -b old --track old/master
git remote rm old
git branch --set-upstream origin old
git push