Git

Cheat Sheet

Branching

Create a local branch to track a remote branch

git checkout --track -b (local-branch) (remote-branch)

Example:
git checkout --track -b 1.5-rest-app origin/1.5-rest-app

Working With the Stash

Listing the diff of what's in the stash

For newbies, this isn't totally obvious at first; since the default for git diff is to show the diff and the default for git stash show is to show the summary. Here's how to get git stash show to be more forth-coming:

git stash show -c

Tips

~/.gitconfig

shamelessly lifted from [http://www.jukie.net/~bart/conf/gitconfig].

#
# ini for git
#

[core]
        #whitespace = trailing-space,space-before-tab,indent-with-non-tab,cr-at-eol

[color]
        pager = true
        ui = auto

[merge]
        tool = vimdiff

[apply] 
        #whitespace = strip

[alias]
        rclone = clone --reference /git/cache.git/

    last  = cat-file commit HEAD
        cat   = -p cat-file -p

        st    = "!f() { git ls-files --exclude-standard -t -u -s -m -d $@ | sed -e 's/C.*\t/C /' ; } ; f"
        sto   = st -o

        ds    = diff --stat
        cds   = !git ds --cached

        # format-patch is so damn long
        fp    = format-patch

        s     = "!git diff --name-status | sed -e 's/^/W /' ; git diff --cached --name-status | sed -e 's/^/C /'"

        up    = !git remote update ; git gc

        debug = !GIT_PAGER= gdb --args git

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

        push-head = !git push origin `git symbolic-ref HEAD`
        publish = push publish
        pushall = !git remote | xargs -n1 git push
        pushalltags = !git remote | xargs -n1 git push --tags

        tblog = log --not-grep='Auto-increment of build number due to changes in directory '
        tblg  = "!f() { git lg $@ | grep -v '^[^(]*Auto-increment of build number due to changes in directory ' | less ; } ; f"
        tbk   = !gitk --not-grep='Auto-increment of build number due to changes in directory '

    vgc = repack -f -a -d --depth=250 --window=250
        nvgc = !ionice -n7 nice -n20 git vgc

        svnup = !git config --get-regexp 'svn-remote.*url' | cut -d . -f 2 | xargs -n1 git svn fetch

        #cvscommit = !git --git-dir=`cat CVS/GitDir` cvsexportcommit -c -p -v

    log1 = log --pretty=oneline --abbrev-commit --decorate
        lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
        lgn = "!f() { git log --graph --pretty=format:'%Cred%H%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --date=relative $@ | git name-rev --stdin --name-only ; } ; f"

        findnext = rev-list --reverse -n1 HEAD..master
        checkoutnext = !git checkout `git findnext`

        # example: git subdo pull
        subdo = submodule foreach git

        case = !sh -c 'exec /home/oxygen/bart/work/git-case/git-case.git/git-case-$0 ${0+"$@"}'

    edit-last-commit = !vim `git diff-tree -r --name-only HEAD HEAD~1`
        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`"

    file-blame = !git ls-files | xargs -n1 -i{} git log -1 --pretty=format:\"%ci {}\" -- {}
    amend = commit --amend -C HEAD
    tbrev = "!f() { git show \"$1\":include/dsa_version.h | awk '/define DSA_VERSION_STRING_A/ { print $3 }' ; } ; f"

        #test = !sh -c 'echo cmd=$1 arg1=$2 arg2=$3'
    #dirstatus = !ls -d1 */.git | xargs -n1 -i{} sh -c \"cd {}/.. && echo Checking {} && git ls-files --exclude-standard -tousmd\"

[giggle]
    compact-mode = false
    main-window-maximized = false
    main-window-geometry = 958x1139+960+36
[pack]
    threads = 4

[push]
    #default = tracking
        default = matching

[gc]
        reflogexpire = 300
        reflogexpireunreachable = 90

[color "diff"]
    frag = magenta reverse
[git-p4]
    user = bartt
[diff]
    tool = vimdiff
    guitool = kdiff3
[man]
    viewer = man
[help]
    format = man
[man "man"]
    path = vman
[url "git@github.com:bartman/"]
    insteadOf = github:
[url "bart@git.jukie.net:/home/git/"]
    insteadOf = jukie:

Study Plan

Goal

As a Pragmatic Programmer, I consider source code repositories a core tool, specifically git:

  • I use it to version control everything I do.
  • I easily integrate with git, SVN and CVS repositories.
  • I know how to branch and rebase.
  • I feel free in making changes, know I can back them out or save them as experiments for a later date.

Principles

Practices

  1. Learn one new thing about git every day; capture that in the section below: "Dripping Drops of gitty Water"

Resources:

SQRRR

Survey:
- Distributed
- Locking Options?
- Git command line vs. gui
- Remote Repository
- Tagging vs. Branches
- Playing with History… re-writing commits… how to do this?
- Integrating with SVN (critical)
- What's a git server? How is it different than a remote repository?

Questions:
- what does "logallrefupdates" do? (from .git/config)

Quick Reference:
- git log -1 (and number)… to see just that many log entries back.

Read:

  • working tree — aka "working copy"
  • master branch — main line of development (Default branch) = TRUNK
  • upstream repository — a clone of a git repo shared among a group of developers.
    • push changes to
      • you can't push changes to a file that has since been modified. (optimistic locking)
    • pull = fetch + merge
  • conflict resolution is a key skill in git. Learn how to do it all the ways available (and when)
  • There are three different copies of your code when working with git:
    • your working copy (what you edit and use in building the code)
    • the index (aka "staging area").
    • the respository.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License