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
# ~/.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 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.
# ~/.gitconfig
[user]
name = Noah
email = hi@noahflk.com
singingkey = <your key signature>
[commit]
gpgsign = true
There is a great explanation here 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.
# ~/.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.
# ~/.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.
# ~/.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
Top comments (0)