Git Config: A Beginner's Guide with Advanced Tips
Tags: git, github, learning
Published: 2024-05-12 06:00:00

## Let's Get Started with Git!
So, you're ready to dive into the world of Git? Excellent choice! Git is like your trusty assistant, keeping your project organized while you and your team work together seamlessly.

We'll be covering the following topics:

* [Installing Git](#installing-git)
* [Configuring Git](#configuring-git)
  * [Identity](#configure-your-identity)
  * [Text Editor](#choosing-your-text-editor)
  * [Signing Commits](#signing-your-commits)
* [Commands Cheat Sheet](#quick-commands-cheat-sheet)

Let's dig in!

---

## Installing Git
First things first, let's get Git installed on your machine. Head over to the [official Git website](https://git-scm.com/downloads) and grab the latest version.


### MacOS:
If you're on a Mac, you probably already have Git installed, but just in case, here's how you can do it with [brew](https://brew.sh/).

```bash
brew update # Ensure everything's up to date
brew install git # Install Git
```


### Linux (Debian/Ubuntu/WSL on Windows)
For Linux users, installing Git is straightforward. Just run:

```bash
apt-get install git
```

For other Linux distributions, refer to the [documentation](https://git-scm.com/download/linux).


### Verifying the Installation
Once Git is installed, let's double-check to make sure it's working. Open your terminal and type:

```bash
git version # Check Git's version
```

If you see something like this, you're good to go!

![Git Version Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dhj5wfgsw71v87uoepoy.png)

---

## Configuring Git
Now that Git is on your machine, let's configure it to suit your preferences.


### Configure your identity
Let's start by telling Git who you are:

```bash
git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"
git config --global user.username "yourusername"
```


### Choosing your text editor

Next, let's set your preferred text editor for Git to use:

```bash
git config --global core.editor "your-editor"
```

I like to use [zed](https://zed.dev/) for this given its speed, to use zed you can run:

```bash
git config --global core.editor "zed"
```

For VSCode you can run:
```bash
git config --global core.editor "code -w"
```

### Signing your commits

As you've become adept at navigating Git, you may now be considering enhancing your workflow by signing your commits for added security and authenticity. Traditionally, Git has relied on GPG (GNU Privacy Guard) for commit signing. However, configuring and understanding GPG can present challenges, especially for those new to the tool.

Thankfully, there's a new solution. OpenSSH has introduced a feature allowing the signing of data using existing SSH keys and Git has seamlessly integrated this capability as an alternative to GPG.

You can take a look at the official Github documentation [here](https://docs.github.com/en/authentication/managing-commit-signature-verification/signing-commits)

#### Signing Git Commits with SSH

(Make sure to remove the `--global` parameter if you only want to do this for a specific repository)

First we need to switch the signature format to SSH:

```bash
git config --global gpg.format ssh
```

You also need to tell Git which SSH key to use for signing. You can just use your public key for `user.signingKey`.

```bash
git config --global user.signingKey ~/.ssh/id_rsa.pub
```

You can now create signed commits:

```bash
git commit -S -m 'Create a signed commit'
```

> If you want to force all future commits to be signed you need to add:
> ```bash
> git config --global commit.gpgsign true
> ```

If you're using Github make sure you have added your Public Key to both "Authentication Keys" and "Signing Keys" in the [settings page](https://github.com/settings/keys).

![Github Settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sum6879d0q9wikljfg3i.png)

#### Verifying the signatures

To show and validate the signatures, you can run `git log` with the `--show-signature` parameter.

```bash
git log --show-signature --oneline
```

![Git Log](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/34bbaqe6862aylxyq7vl.png)

Sometimes, as you embark on signing your commits, you may encounter an error message within the commit information. As is the case on this scenario with the error:

```bash
error: gpg.ssh.allowedSignersFile needs to be configured and exist for ssh signature verification
```

This error stems from Git's lack of knowledge regarding which SSH keys to trust for signature verification. Git relies on a predefined list of trusted keys known as the "allowed signers file," which operates similarly to the "authorized keys file" utilized by SSH.

#### Creating the Allowed Signers File

To address this issue, begin by creating a file on your system (e.g., ~/.config/git/allowed_signers) to manage trusted signing keys. Each entry in this file corresponds to an email address used in committing, followed by the associated public key. For instance, you can do so running:

```bash
# Create allowed_signers file
echo "$(git config user.email) $(cat ~/.ssh/id_rsa.pub)" > ~/.config/git/allowed_signers
# Add allowedSignersFile configuration
git config --global gpg.ssh.allowedSignersFile "$HOME/.config/git/allowed_signers"
```

And now the signature can be verified!

![Good Git Log](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t9uuftbg78fgeh2xgxqq.png)


### Checking your configuration
To confirm that everything is set up correctly, run:

```bash
git config --list
```

Expecting an output like this:

![Git Config Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lfvlopds6yo9xdlfo8rh.png)

---

## Conclusion
Congratulations! You've successfully configured Git and you're ready to start coding and collaborating like a pro.

Remember, Git is a powerful tool with many more features to explore. Happy coding!

---

## Quick Commands Cheat Sheet:

### MacOs:
```bash
# Ensure everything's up to date
brew update
# Install Git
brew install git
# Basic Configuration
git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"
git config --global user.username "yourusername"
# Signing configuration
git config --global gpg.format ssh
git config --global user.signingKey ~/.ssh/id_rsa.pub
git log --show-signature --oneline
# Check your Git configuration
git config --list
```

### Linux:
```bash
# Make sure package list is updated
apt-get update
# Install Git
apt-get install git
# Basic configuration
git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"
git config --global user.username "yourusername"
# Signing configuration
git config --global gpg.format ssh
git config --global user.signingKey ~/.ssh/id_rsa.pub
git log --show-signature --oneline
# Check your Git configuration
git config --list
```

### Allowed Signers File
```bash
# Create allowed_signers file
echo "$(git config user.email) $(cat ~/.ssh/id_rsa.pub)" > ~/.config/git/allowed_signers
# Add allowedSignersFile configuration
git config --global gpg.ssh.allowedSignersFile "$HOME/.config/git/allowed_signers"
```