Skip to content

Instantly share code, notes, and snippets.

@Drarig29
Last active April 17, 2024 20:36
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Drarig29/5580ccd123988af4fc6da1a2cacca054 to your computer and use it in GitHub Desktop.
Save Drarig29/5580ccd123988af4fc6da1a2cacca054 to your computer and use it in GitHub Desktop.

So You Think You Know Git

Dump of all commands from "So You Think You Know Git - FOSDEM 2024":

The full blog post: https://blog.gitbutler.com/git-tips-and-tricks/

Some Helpful Config Stuff

Some aliases

# All ignored and untracked files are also stashed and then cleaned up with `git clean`.
git config --global alias.staash 'stash --all'
# Download the script here: https://gist.github.com/schacon/e9e743dee2e92db9a464619b99e94eff

# To run an arbitrary shell script
git config --global alias.bb !better-git-branch.sh

Conditional Configs

[includeIf "gitdir:~/projects/work/"]
  path = ~/projects/work/.gitconfig

[includeIf "gitdir:~/projects/oss/"]
  path = ~/projects/oss/.gitconfig

Oldies But Goodies

Git blame a "L"ittle

# Blame a line range
git blame -L 15,26 path/to/file

...equivalent for git log:

# Inspect line range over time
git log -L 15,26:path/to/file
# Inspect a symbol called "Foo" (e.g. a class) over time
git log -L :Foo:path/to/file
# Ignore whitespace
git blame -w
# Ignore whitespace,
# and detect lines moved or copied in the same commit
git blame -w -C
# Ignore whitespace,
# and detect lines moved or copied in the same commit,
# or the commit that created the file
git blame -w -C -C
# Ignore whitespace,
# and detect lines moved or copied in the same commit,
# or the commit that created the file,
# or any commit at all
git blame -w -C -C -C

The "pickaxe"

# Find all the commits where `<some-text>` appears in the diff
git log -S <some-text> -p

You can reverse it as well, to get the oldest commit first:

git log -S <some-text> -p --source --all --reverse

Other

# Shows when the tips of branches and other references were updated in the local repository
git reflog
# When you want to look at diffs inside a line
git diff --word-diff
# (RE)use (RE)corded (RE)solution
# Remember how you solved conflicts, to reapply them later automatically
git config --global rerere.enabled true

Some New Stuff (You May Not Have Noticed)

git branch --column

or enable it globally:

git config --global column.ui auto
# Sort branches by most recent commit first 
git config --global branch.sort -committerdate
# Safer force push
git push --force-with-lease
# Signing commits with SSH
git config gpg.format ssh
git config user.signingkey ~/.ssh/key.pub
# Not supported by GitHub or GitLab
git push --signed
# Building commit graph, prefetching, removing loose objects and incremental repack automatically
git maintenance start

Big Repo Stuff

Commit graph

# Faster `git log --graph --oneline`
git commit-graph write
# Write commit graph on fetch
git config --global fetch.writeCommitGraph true

Filsystem monitor

# Filesystem monitor for faster `git status`
git config core.untrackedcache true
git config core.fsmonitor true

Partial clone

# Do not clone blobs, and only do it on-demand
git clone --filter=blob:none
# Good for CI
git clone --filter=tree:0

Sparse checkout

# Only checkout `build` and `base` folders
git sparse-checkout set build base

Some New GitHub Stuff

# Fetch all PRs automatically on `git fetch`
git config remote.origin.fetch '+refs/pull/*:refs/remotes/origin/pull/*'
@Drarig29
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment