A Better Gitconfig

Jamie July 24, 2024
Source
Git provides a rich set of options for customising its behaviour via the .gitconfig file. I present here some of the more interesting options. For more comprehensive information on each and every option, you had better peruse the official documentation. Basics Depending on your preferences, the global gitconfig file is either located at ~/.gitconfig, $XDGCONFIGHOME/git/config, or perhaps ~/.config/git/config β€” the documentation provides all options. If you have no existing gitconfig, go ahead and create one in your preferred location. I'm using ~/.config/git/config. Includes You can include other files into your gitignore with the include directive. This can be useful for colour theme files. Note that files are relative to the current (config) file unless an absolute path is given. Per-folder settings / includes It's also possible to use conditional includes, which lets us enable settings for repos under a particular directory. Suppose, for example, that you use your personal e-mail address for open source work, but want to use your work e-mail for commits to work repositories β€” or, all repositories under ~/work. For this we can use includeIf: See the docs on conditional includes for more. Global gitignore There are some things that you want to make sure never make it into any of your git repos, regardless of how they're each set up, unless you very explicitly force them in. That's why I like to use a global .gitignore file. This file defaults to $XDGCONFIGHOME/git/ignore or ~/.config/git/ignore, but it might make sense to use something under $HOME for consistency if using ~/.gitconfig: Parallelization Some operations can be parallelized, which aren't by default. This might speed up your workflow when working with larger repos, but may also slow you down with smaller repos. YMMV. Conditional tooling I might work on some machines where one diff or merge tool is available and another isn't. For this reason I like a set of fallbacks to pick from. This can be achieved with simple scripts. Then, somewhere in your path, place the scripts. You'll want to change these depending on your preferred fallback order / installed software: Aliases Finally, what's usually the largest section of one's gitconfig is the aliases. Much like shell aliases, these let us get common operations done with fewer keystrokes and ease the need to remember every switch. Here's an example: Now we can use git s and git will run git status for us. Since we all have our preferences, I'll just include my current aliases here for inspiration. I suggest borrowing a couple at a time, lest you end up with a gitconfig full of aliases you've forgotten you have. Bonus Some extra stuff you might want to consider to improve your git workflow: - Check out my annotated gitconfig for more ideas - Use delta as your pager - Try an interactive CLI for git, such as tig, gitu, or gitui - Alias git to g in your shell

Discussion in the ATmosphere

Loading comments...