Back to list of posts

Git Tools and Resources

Posted Jan 30, 2020 by Russ Fenn

code.vt.edu is the home for many of the projects we DevCom’ers use daily.

Git Resources

Git Tips and Tricks (or Trips and Traps)

Git (cli) Aliases

Git has dozens of commands, with hundreds of options, most of which most of us will rarely (or never) use. Some of them, when strung together are very useful, but can become tedious to type… Git aliases to the rescue!

Like a shell alias, a git alias is simply a shortcut for a longer string of command flags, and can be created either with git config or by editing your ~/.gitconfig file.

The following aliases are equivalent:

git config --global alias.co checkout
### File: ~/.gitconfig
[alias]
    co = checkout

Some more aliases

### Simple shortcuts
st = status -sb
ci = commit
co = checkout

### Graphical versions of 'log', with more and more glitter
glog = log --graph --pretty=oneline --all
glogc = log --graph --pretty=format:'%C(red)%h%C(cyan)[%G?:%GS]%C(reset) -%C(yellow)%d%C(reset) %s %C(green)(%cr) %C(bold blue)<%an>%C(reset)' --abbrev-commit --date=relative --branches
glogl = log --graph --pretty=format:'%C(red)%h%C(cyan)[%G?]%C(reset) -%C(yellow)%d%C(reset) %s %C(green)(%cr) %C(bold blue)<%an>%C(reset)' --abbrev-commit --date=relative --branches --decorate --numstat

### History of a single file
fh = log -u --
hist = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short

### Git objects
type = cat-file -t
# Ex. git dump HEAD~1
dump = cat-file -p

# aliases that mimic hg commands
out      = !git fetch && git log FETCH_HEAD..
outgoing = !git fetch && git log FETCH_HEAD..
in       = !git fetch && git log ..FETCH_HEAD
incoming = !git fetch && git log ..FETCH_HEAD
sum      = show --no-patch --pretty=format:'%C(red)%h%C(reset) %C(yellow)%d%C(reset)\n%C(blue)%ai<%an>%Creset)\n%s'

More information on --pretty=format: syntax can be found in pretty formats

Per-Directory Configuration

git config can operate on a single directory, or --globally. You can also extend the configuration for a directory (and its child directories) by conditionally including other configuration files:

### File ~/.gitconfig
# Override configuration for Unicorn Project repositories
[includeIf "gitdir:~/src/projects/Unicorn"]
    path = ~/src/projects/Unicorn/_gitconfig

The included file has its own configuration directives

### File ~/src/projects/Unicorn/_gitconfig
[user]
    name = Maxine Chambers
    email = maxine@parts-unlimited.com

Friends of Git

  • ag - The Silver Searcher – a fast grep-like tool that knows how to ignore git-related files. [cli]
  • diff-so-fancy – strives to make your diffs human readable instead of machine readable. [cli]
  • icdiff – side-by-side diffs. [cli]