I recently watched Scott Chacon’s talk called So You Think You Know Git. The talk was so good — it includes new and old git commands I’ve never seen. After watching it, I decided to create this article to summarize all the great tips and tricks with every single git command he mentions in his talk.
-
Create an alias to always stash all files (including ignored and untracked files). In this example, you would run “git staash”, but you can choose any alias.
git config --global alias.staash 'stash --all'
-
Run a bash script defined as an alias from the git command. In this example, you would run “git bb”. Link to the super useful better-branch script here: better-branch script
git config --global alias.bb !better-branch.sh
-
If you want to switch configs based on your project’s features, you can add conditional configs to any
.gitconfig
file.[includeIf "gitdir:~/projects/work/"] path = ~/projects/work/.gitconfig [includeIf "gitdir:~/projects/oss/"] path = ~/projects/oss/.gitconfig
-
Use
git blame
, but make it run on specific lines in a file. Much more readable and understandable.git blame -L 15,26 path/to/file
-
You can also use
git log -L
to find functions inside files that have changed recently. Much cleaner as well.git log -L :functionName:path/to/file
-
Another
git blame
command improvement: ignore whitespace, detect lines that moved or copied in the same commit, or find the commit that created the file.git blame -w -C -C -C
-
One
git blame
command to rule them all. Putting everything together. Scott Chacon recommends creating an alias for this super useful command:git blame -w -C -C -C -L 15,26 path/to/file
-
Use
git log
with a special flag when you want to find a string that changed, butgit grep
won’t be able to find it since it is part of the history of the repo. In this example, the regular expression to search is “files_watcher”:git log -S files_watcher
-
Use
git reflog
as a log of your references, so you can search through all the changes locally in your repo.git reflog
-
Using
git diff
with the--word-diff
flag allows you to find differences in file changes based on words and not the whole line of code. This makes it way easier to read & understand the changes.git diff --word-diff
-
Probably one of the most useful unknown configs: reuse recorded resolution (
rerere
). It stores conflict resolutions and automatically applies them when they show up again. This seems to be a no-brainer productivity boost.git config --global rerere.enabled true
New Stuff
-
Leverage the
git branch --column
command by sorting the branches based on the last modified date and display them in columns.git config --global column.ui auto git config --global branch.sort -committerdate
-
Use a better
git push --force
without worrying you will overwrite someone else’s work. It’s called--force-with-lease
!git push --force-with-lease
-
Sign commits with SSH instead of GPG. Also reuse your same SSH key when pushing commits!
git config gpg.format ssh git config user.signingkey ~/.ssh/key.pub
-
Git maintenance can schedule background tasks (like cleanup) via a cron job, significantly speeding up Git for large repos! Say hello to smoother experiences with monorepos.
git maintenance start
Big Repo Stuff
-
When you enable the filesystem monitor, this starts a daemon that tracks file changes. Now,
git status
will only show the updates on modified files.git config core.fsmonitor true
-
Use
git clone
filter to filter out blobs when cloning large repos.git clone --filter=blob:none
-
…or use
git clone
filter to filter out trees.git clone --filter=tree:0