Git recipes & tricks

These are some low-level tips for the console, for power users of Git.

Useful config 🔗

Safest way to “update” a local copy 🔗

I find this sequence of commands the “safest” way to quickly “refresh” a clone of some remote repo, and at the same time check its status (where “safest” means “reducing to the minimum the probability of messing up things with conflicts, outdated branches, uncommitted changes, etc”):

$ git branch -a
$ git pull -r
$ git status
$ git remote prune origin --dry

You can have those four lines as an alias, or inside a script somewhere.

Even better: if you set up the aliases suggested above, the whole thing becomes:

$ git br -a;git pl -r;git st;git re prune origin --dry

You can then type it and run it just once, and, from that moment on, simply recover the line from your shell history.

For example, if you're using Bash: press Ctrl+r, then start typing a distinctive chunk of the line (eg, r;, or st;gi); if you used it not too long ago for the last time, the entire line should appear, and you can simply press Enter.

An alias to view the history of the repo 🔗

alias.lg=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(bold yellow)%d%C(reset)' --all

Then, simply type

$ git lg

for a colourful graph of commits, tags and branches.

See also 🔗