4 tips to supercharge your Git experience
Tags: git, github, environment, tips
Published: 2022-01-17 15:02:04

---
title: 4 tips to supercharge your Git experience
published: true
description: Having recently done the work of setting up my new computer from scratch, I think I've landed on a pretty sweet Git setup. Let me share it here.
tags: git, github, environment, tips
cover_image: https://images.unsplash.com/photo-1627399270231-7d36245355a9?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1674&q=80
---

Having recently done the work of setting up my new computer from scratch, I think I've landed on a pretty sweet Git setup. Let me share it here.

The tips in this tutorial require changes to the Git configuration file. This file can be found in your home directory as `.gitconfig` on any OS. I personally use Mac so some things in here might be different on other platforms.

## Use main as default branch

In 2020 GitHub among many others changed the default branch name for new repositories from `master` to `main`. Doing the same on our local machines can be achieved with one setting

```bash
# ~/.gitconfig

[init]
    defaultBranch = main
```

## Auto sign commits

Do you want your commits on GitHub to have a green "Verified" badge? ✅ This ensures that you are indeed the author of the commits and that they have not been tampered with (which is surprisingly easy to do by the way).

First, you need to generate a GPG key pair. This is very similar to SSH keys, which you will most likely already have. If you use a Mac, [GPGTools](https://gpgtools.org/) is the best tool I could find to generate and manage GPG Keys.

Now, export the public key through GPGTools and upload it to your GitHub user account.

After that we need to tell Git to sign all commits with our new key.

```bash
# ~/.gitconfig

[user]
    name = Noah
    email = hi@noahflk.com
    singingkey = <your key signature>

[commit]
    gpgsign = true
```

There is a great explanation [here](https://medium.com/@rwbutler/signing-commits-using-gpg-on-macos-7210362d15) if you need more info.

## Multiple Git identities

If you're like me and need to use different email addresses for work and private projects, you can automatically adjust your identity based on which directory you're in.

And yes, you can even set different GPG keys for both your work and private email addresses.

First you will need to define the identity that applies everywhere per default in your main configuration.

```bash
# ~/.gitconfig

[user]
    name = Noah
    email = hi@noahflk.com
    singingkey = <your key signature>
```

Then, create a second file for your work identity and import it in the main configuration only *if* you are in a certain directory.

```bash
# ~/.gitconfig

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

# ~/.gitconfig-work
[user]
    name = Noah
    email = supersecretwork@email.com
    singingkey = <your key signature>

```
Now, make sure you clone all your work repositories into the directory you specified. All repositories in there will automatically make use of your work identity. Any repository not in there will use the default identity.


## Only push the current branch

By default Git will push all branches to the remote and merge them. This is a matter of preference but I do not like it. Therefore I have Git configured so that per default only the current branch that is tracking a remote upstream branch will be pushed. See a more detailed explanation [here](https://stackoverflow.com/questions/23918062/simple-vs-current-push-default-in-git-for-decentralized-workflow).

```bash
# ~/.gitconfig

[push]
    default = simple
```

## Conclusion

These are some of the tricks I use to improve my day-to-day Git/GitHub experience. Let me know if you have any other suggestions!

If you liked this you can connect with me on [Twitter](https://twitter.com/noahflk)