Imposter!
Notes from someone pretending!



Oh Git!

A few quality of life imporovements for working with commandline git

Posted on


Git is a powertool which most folks in the software industry use.

My view of Git

It's the kind of tool, where the users can be classified into:

For me personally, I belong to the bucket which I havent defined (between Basic & Intermediate). This blog post is a quality of life improvement to help with git and help you improving your workflow.

Let's start with the absolute basics of quality of life imporovements

Absolute Basics:

A few quality of life improvements before we move ahead on our journey to improve our git workflow.

Configuring the Prompt:

Out of the box the command line git interface is bare. To get a little more context a good idea is to configure the PS1 of your terminal.

A git repo on the terminal without a custom PS1:

[mfrw@archlinux mfrw.github.io]$ ls
config.toml content public sass static templates
[mfrw@archlinux mfrw.github.io]$

The same repo after some fancy PS1 configuration:

mfrw in archlinux in ~/os/mfrw.github.io on  master:origin/master
fsh ❯ ls
config.toml content public sass static templates
mfrw in archlinux in ~/os/mfrw.github.io on  master:origin/master
fsh

There are mulitple ways to configure the PS1 for your choice of the $SHELL, but I use multiple shells on the differnt machines I have. Some machines are configured to use the fish some have zsh and the remaining are bash. I also, like to have a coherent experience across all my devices and the cognitive load of not writing and maintaining multiple PS1 scripts for differnt non-compatible shells, I use startship. startship is a fast, cross shell and infinitely customizeable prompt for any $SHELL.

Starship Demo

A quick start for starship can be found here. My starship configuration can be found here. Once we have configured the prompt to our hearts content, it becomes becomes hard to work on a branch which we are not supposed to. The visual clues make it difficult.

Configure Aliases

Out of the box git comes with verbose subcommands to do stuff. While I am fan of a good naming convention, if we have a repeated workflow, typing the same tedious commands again and again might cause us to fat-finger and loose our rythm. Git can be configured extensively (like any other self respecting unix tool). Let's setup some aliases for the most common git sub commands.

Here is my .gitconfig where I have created aliases for the most common commands I use with git.

[alias]
  br = branch -vv #display each branch & the commit it is on
  ch = status -uno #only show changed files
  ci = commit -s # commit with --signoff
  cl = clone
  co = checkout
  contributors = !git log --format=format:"%an" | sort | uniq -c | sort -rn
  d  = diff
  dc = diff --cached # diff with the index
  dump = cat-file -p
  fo = fetch origin
  fa = fetch --all --tags
  hist = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short
  lga = log --oneline --graph --all --decorate #show a graphical lineage of all branches
  lo = log --oneline
  pfl = push --force-with-lease #should rename it to 'please'
  pd = push --delete
  pur = pull --rebase
  recent = branch -a --sort=-committerdate
  revert = revert -s
  st = status
  type = cat-file -t
  unstage = reset HEAD --
  rup = remote update --prune #delete stale references and sync with remote

Miscilleanous

After the important configs are done, we do need some more optional configurations that can make our workflow smother.

The git/contrib directory is also an amzing source of additional scripts that have been contributed by the conmmunity and reside in the git source tree.

Completion

Configure commandline complete by sourcing the appropriate file for your shell from here. Once completion is configured, it becomes very easy to TAB your way through git!, which is always pretty cool.

Visual eye-candy

There are a couple of addon pagers that we can configure git with, to have support for side-by-side diffs.

git-delta with syntax highligting (without side by side diff)

My .gitconfig for delta :

[core]
	pager = less

[pager]
        diff = delta
        log = delta
        reflog = delta
        show = delta

[delta]
        features = side-by-side linux-numbers
        whitespace-error-style = 22 reverse
        navigate = true

[delta "decorations"]
        commit-decoration-style = bold yellow box ul
        file-style = bold yellow ul
        file-decoration-style = none

[interactive]
        diffFilter = delta --color-only

git-delta with syntax highligting (with side by side diff)

Online cheat sheets:

Motivation to use cheat-sheet or help

A few cheat-sheets that i use regularly for working with git (or anything for that matter on the terminal)

Integration with GitLab/GitHub & more tools:

Thank you, if you know of anything else that has helped you to improve your workflow or have any comments or suggestions, please feel free to reach out on twitter (@vimfrw).