Git tips and tricks

Updates
Added git repl section.

A collection of Git tips and tricks you might not know.

Find the most frequently changed files

You can find the most frequently changed files in the repository with this command:

git log --pretty=format: --name-only <path> | sort | uniq -c | sort -rn | head -10
git log --pretty=format: --name-only | sort | uniq -c | sort -rn | head -10
git log --pretty=format: --name-only packages/news | sort | uniq -c | sort -rn | head -10

The path limits the search to a specific directory, but you can omit it to search the whole repository.

You can use this list to decide which files to refactor first. For example, imagine you want to move a project from JavaScript to TypeScript. You can find the most changed files and start with them.

Or imagine you encounter a new codebase. Finding the most frequently changed files can help you figure out, what to focus on first.

Find the most recent branches

The following command will show you the branches sorted by the date of the last commit.

git config --global alias.recent "!git for-each-ref refs/heads --sort committerdate --format='%(HEAD) %(refname:short)'"
git recent

I sometimes forget the name of the branch I want to switch to, and this command makes finding it easy. But instead of git recent I aliased it to git brt, because it’s faster to type.

Reuse recorded resolution of conflicted merges

Git rerere can automatically resolve merge conflicts you resolved before, but you need to enable it first:

git config --global rerere.enabled true

More info in Git rerere.

Avoid unnecessary merge commits on pull

When you pull a branch, Git will by default create a merge commit. If you don’t want to have a merge commit, you can use the following setting to rebase the local commits instead:

git config --global pull.rebase true

As nobody knows your commits yet, nobody will get confused when you rebase them, and it keeps the history clean.

More info in Git config pull.rebase documentation.

Setup Git SSH signing

In version 2.34, Git introduced SSH signing. Git supported GPG signing for a long time, but I found it cumbersome. With SSH signing, you can use your already configured SSH key.

See how to set up Git SSH signing locally and on GitHub.

Import configs

You can include other configs in your .gitconfig file.

git config --global --add include.path '~/.git-config-work'

More info at Git config includes documentation.

Use git bisect to find buggy commits

If you have a bug in your code, and you want to find out which commit introduced it, you can use git bisect. With it, you will use binary search which is much faster than going through all the commits in a sequence.

git bisect start HEAD abc123 # HEAD is the buggy commit, abc123 is the good commit without a bug. It can be anywhere from the past.
git bisect bad # if Git switched to a commit which is bad/has a bug
git bisect good # if commit is good
git bisect reset # to abort

More info at The git’s guide to git: Bisect.

Auto setup remote on push

Git version 2.37 introduced a new config option: push.autoSetupRemote. You can use it to automatically set up a remote when you push. Which means you don’t need git push –set-upstream origin my-work or git push -u origin my-work anymore.

git config --global push.autoSetupRemote true

More info at Git config push.autoSetupRemote.

Git REPL

Tired of typing git when you run git commands? You can install git-extras and use its git repl to run multiple commands in a row.

git repl
status
add .
commit -m "Add feed"
push
exit