Shortcuts and configuration

Shortcuts

Some cheatsheets, tips, tricks, and shortcuts

To quickly check the contents of a single file, from a single commit, without having to checkout, stash dirty working copy, etc. This is equivalent to “svn cat.”:

git show rev:path/to/file

For quickly committing all changed (tracked) files, skip the index/staging, and only a one-line log message that doesn’t open an editor (don’t make this a habit - see best practices for how to write good log messages):

git commit -am 'one-line log message'

To fix the most recent commit without creating a new one. I use this to fix up the log message, or if I catch a minor typo in the content of the latest commit.:

git commit --amend

BEWARE: This changes history, so be aware when using this after a commit has been pushed to a remote repository.

Configuration

Some configuration hints

Using your username in clone url saves typing at every server interaction (pull, push, fetch, etc.).:

git clone https://username@code.ornl.gov/username/repo_name.git

Setting up ssh keys saves more typing at every server interaction. Also, see the page on [git credentials](./git_credentials) for more options about safely storing your git passwords.

Here is my .gitconfig. It sets up some nice conveniences for day-to-day working with git. Colors when diffing code and looking at branching graphs helps me quickly understand output that is otherwise too busy. Aliases save me a lot of typing, and mean I don’t have to remember complex combinations of options.:

[user]
  name = Graham Lopez
  email = email@domain.com

[color]
    ui = auto
[color "branch"]
    current = yellow reverse
    local = yellow
    remote = green
[color "diff"]
    meta = yellow bold
    frag = magenta bold
    old = red bold
    new = green bold
[color "status"]
    added = yellow
    changed = green
    untracked = cyan

[core]
    whitespace=fix,-indent-with-non-tab,trailing-space,cr-at-eol
    pager = less -RMFX
        excludesfile = /home/graham/.gitignore

[alias]
    st = status
    ci = commit
    co = checkout
    br = branch
    logfiles = log --name-status
    logdiff = log -p --stat
    lot = log --graph --decorate --oneline --date-order --all
    lo = log --graph --decorate --oneline --all
    lt = log --graph --decorate --oneline --date-order
    l = log --graph --decorate --oneline
    ls = ls-files
[push]
    default = simple

If you looked at the code, you’ll notice there are several very similar aliases for viewing logs. The differences, “–date-order” and “–all” respectively change the default log graph output to be ordered strictly by commit date, and toggle whether to show all branches, or only those branches who have some history in common with the current branch. I keep all of these variants because sometimes logs and branching get messy, and different output might be cleaner than another. Compare git lo vs. git lot aliases on the resulting history from the script that is attached at the bottom of the repo histories page (see gitflow_history.sh). When I am focused on development in a single branch, the git l alias is the one I use 90% of the time.

Here are a couple of other git log aliases that people have found useful:

[alias]
  lg1 = log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset)%C(dim white)- %an%C(reset)%C(auto)%d%C(reset)' --all
  lg2 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green (%ar)%C(reset)%C(auto)%d%C(reset)%n'' %%C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all

You can see information about the current branch and state of a repository directly in a Bash or Zsh prompt using “git prompt” (https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh). It only adds the git-related information to the prompt when the current directory is part of a git repository. This is the excerpt from my .zshrc that configures the prompt:

source ~/.git-prompt.sh
setopt prompt_subst

export GIT_PS1_SHOWDIRTYSTATE=true
export GIT_PS1_SHOWUNTRACKEDFILES=true
export GIT_PS1_SHOWUPSTREAM="auto"
export GIT_PS1_SHOWCOLORHINTS=true

PROMPT='$WHITE%m$CYAN:$CYAN%3~$YELLOW$(__git_ps1 "(%s)")$CYAN-| $NOCOLOR'
RPROMPT='$RED%(?..[%?]) $CYAN|$WHITE%*$NOCOLOR'

This configuration will cause the left side prompt to show the current branch name, if there are untracked files in the repository, if there are uncommitted changes in the repository, and whether the local branch is behind or ahead of the remote tracking branch.