Skip to content

Instantly share code, notes, and snippets.

@pamo
Created July 18, 2018 21:23
Show Gist options
  • Save pamo/00d143b5bd0a2facd74072101ffe02b0 to your computer and use it in GitHub Desktop.
Save pamo/00d143b5bd0a2facd74072101ffe02b0 to your computer and use it in GitHub Desktop.
Git Tweaks

Displaying Git Branch in Console Prompt

If you'd like your terminal prompt to display your current Git branch in a nice format, the following is an excerpt from my .bash_login:

###############
# Custom prompt:

function find_git_branch {
  local dir=. head
  until [ "$dir" -ef / ]; do
    if [ -f "$dir/.git/HEAD" ]; then
      head=$(< "$dir/.git/HEAD")
      if [[ $head == ref:\ refs/heads/* ]]; then
        git_branch=" ${head#*/*/}"
      elif [[ $head != '' ]]; then
        git_branch=' (detached)'
      else
        git_branch=' (unknown)'
      fi
      return
    fi
    dir="../$dir"
  done
  git_branch=''
}

PROMPT_COMMAND="find_git_branch; $PROMPT_COMMAND"

# Left these in here in case you want alternate colors.
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
NO_COLOUR="\[\033[0m\]"
txtwht='\e[0;37m' # White
green=$'\e[1;32m'
magenta=$'\e[1;35m'
normal_colours=$'\e[m'

PS1="\[$txtwht\]\u$NO_COLOUR@\[\033[0;35m\]\h$NO_COLOUR:\w\[$magenta\]\$git_branch\[$green\]\\$\[$normal_colours\] "

Colorful Git Output

If you want the settings for all your projects, add the following lines to ~/.gitconfig. Otherwise, add them .git/config within your project directory:

[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

NOTE: make sure the [color] ui setting is not set to always, as that may break some of the scripts in onelife

Git Shortcut Commands

If you want the settings for all your projects, add the following lines to ~/.gitconfig. Otherwise, add them .git/config within your project directory:

[alias]
  st = status
  ci = commit
  br = branch
  co = checkout
  df = diff
  dc = diff --cached
  lg = log -p
  lol = log --graph --decorate --pretty=oneline --abbrev-commit
  lola = log --graph --decorate --pretty=oneline --abbrev-commit --all
  ls = ls-files

  # truncated single-line git log of the last 10 commits
  recent = log --oneline -10

  # Show files ignored by git:
  ign = ls-files -o -i --exclude-standard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment