Git cheatsheet
Random references
Git config
Git commit
1# Show diff in commit terminal
2[commit]
3 verbose = true
Aliases
1[alias]
2 edit = !$EDITOR $(git status --short | awk '$1 ~ /^M|A|U/ {print $2}' )
3 ds = diff --staged
4 lol = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
5 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[merge]
2 # Shows the "base" too
3 conflictstyle = zdiff3
rerere
1[rerere]
2 enabled = true
3 autoUpdate = true
Git init
I find this useful for keeping the same hooks
across repos.
1[init]
2 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
git reset --soft A
git restore --staged .
- Stage things for the first commit
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: {: .prompt-info}
Let’s say you have the following git log:
1commit a4b06af620d781f1a0b58178547235a5ed81064a (HEAD -> master)
2Author: ---------------------------------
3Date: Sat Feb 24 13:28:44 2024 +0100
4
5 very nice commit message
6
7commit fdccfe8639ba065e81ac42966723325bd640aba6
8Author: ---------------------------------
9Date: Sat Feb 24 13:29:16 2024 +0100
10
11 something
12
13commit 21c09170728e1f263a5768f058f0ded9ee6cb35d
14Author: ---------------------------------
15Date: Sat Feb 24 11:25:14 2024 +0100
16
17 Initial commit
And you do git rebase -i
:
1pick fdccfe8 something
2f a4b06af very nice commit message
After which, commit a4b06af
has disappeared from your history:
1commit df4ac6962ef777d1dc84c7d7c95011852f22784d (HEAD -> master)
2Author: ---------------------------------
3Date: Sat Feb 24 13:29:16 2024 +0100
4
5 something
6
7commit 21c09170728e1f263a5768f058f0ded9ee6cb35d
8Author: ---------------------------------
9Date: Sat Feb 24 11:25:14 2024 +0100
10
11 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:
1665050f 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.