Post

Git cheatsheet

Random references

Git config

Git commit

1
2
3
# Show diff in commit terminal
[commit]
  verbose = true

Aliases

1
2
3
4
5
[alias]
  edit = !$EDITOR $(git status --short | awk '$1 ~ /^M|A|U/ {print $2}' )
  ds = diff --staged
  lol = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
  lg1 = log --graph --abbrev-commit --decorate --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

Git merge

1
2
3
[merge]
  # Shows the "base" too
  conflictstyle = zdiff3

rerere

Reuse Recorded Resolution

1
2
3
[rerere]
	enabled = true
	autoUpdate = true

Git init

I find this useful for keeping the same hooks across repos.

1
2
[init]
  templatedir = ~/.git-templates

Git reset

While checking out Gerrit’s documentation I found a cool presentation covering git’s basics.

I was particularly surprised that I was doing git reset wrong. I thought there only were git reset --hard and git reset --soft. So whenever I wanted to split a commit, I would

  1. git reset --soft A
  2. git restore --staged .
  3. Stage things for the first commit
  4. git commit

It turns out the right way to do this is to git reset --mixed, which automatically unstages the commits you’re resetting.

Git extras

Git extras is a set of utilities extending git which target typical needs. Worth checking whenever you’re missing something in git.

Git Reflog

Allows you to see unreacheable commits. For example, after a rebase.

I’m sure I’m missing a lot of details about reflog, but the only time it has been really helpful is as follows:

Let’s say you have the following git log:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
commit a4b06af620d781f1a0b58178547235a5ed81064a (HEAD -> master)
Author: ---------------------------------
Date:   Sat Feb 24 13:28:44 2024 +0100

    very nice commit message

commit fdccfe8639ba065e81ac42966723325bd640aba6
Author: ---------------------------------
Date:   Sat Feb 24 13:29:16 2024 +0100

    something

commit 21c09170728e1f263a5768f058f0ded9ee6cb35d
Author: ---------------------------------
Date:   Sat Feb 24 11:25:14 2024 +0100

    Initial commit

And you do git rebase -i:

1
2
pick fdccfe8 something
f    a4b06af very nice commit message

After which, commit a4b06af has disappeared from your history:

1
2
3
4
5
6
7
8
9
10
11
commit df4ac6962ef777d1dc84c7d7c95011852f22784d (HEAD -> master)
Author: ---------------------------------
Date:   Sat Feb 24 13:29:16 2024 +0100

    something

commit 21c09170728e1f263a5768f058f0ded9ee6cb35d
Author: ---------------------------------
Date:   Sat Feb 24 11:25:14 2024 +0100

    Initial commit

But after you have done this, you realize that you actually needed the commit message from a4b06af (which you lost due to the fixup). git reflog has you covered:

1
665050f HEAD@{9}: commit: very nice commit message

From which you can do git show 665050f or whatever you want. The point is, you have the original commit 665050f which is otherwise unreachable after you rebased your branch.

This post is licensed under CC BY 4.0 by the author.