Salomonsson.se
Mar 13 2023

Practical Git

You know all the basic stuff of git: pull, add, commit, push, log, branch, diff. But want to be able to practically use it. To get actual overview of what’s going on. Here are a few really neat commands with options that might be good candidates for you to add as git aliases.

Interactive add

Ever find yourself doing

  1. git status
  2. git diff path/to/file1
  3. git add path/to/file1
  4. repeat for every file?

You can review each change much faster by using the patch option inside

git add -i

Log like a boss

Logging is these things thats important, but also super important if you want to get an overview. Most of these commands can be combined!

Tip number 1:

git log --graph --all

Show log as a graph, and how its relationship with other branches/remotes


Tip number 2 (can be combined with 1)

git log --oneline

Shows a condensed list with only hashes and messages


Tip number 3 (this I don’t see enough of, use it often! Can also be combined with 1):

git log --stat

Shows which files were affected, with a brief addition/deletion summary

I cannot stress how often I find the need to see which files were actually touched by a specific commit.


So you found a suspicious commit and a file you want to inspect

git show hash:file

Quickly show the file at the specific commit


But git diff hash -- file will compare it to the version in your working directory, and you want to see what changes were introduced in that specific commit! That means you need to compare that version of the file with the version of the file as it looked before that commit. This is actually not as easy as one would wish, but here it comes:

git log -p -1 [-m] [--follow] [hash] file

Let’s break this one down.

  • Weird enough we use log -p where one would assume git diff
  • -p will log only changes to that specific file
  • -1 (optional) will limit to only one entry, in our case we do
  • -m (optional) will allow us to diff with merge-commits, most likely we do
  • -follow (optional) will follow commit history of file even if it was renamed
  • hash if we omit this it will show the latest introduced change, we might want to inspect an older one
  • file yeah, no dashes… I do find git syntax to be quite inconsistent