Skip to content

GeneralGit from scratch

0. Git configuration

Guide · git, version control · updated on 01/06/2026

Author: Ángel Fernández Caravaca
Date: March 1, 2026

Before you start using Git and GitHub, you need to understand the key difference between them:

  • Git: a program that records changes to your code. It is installed on your computer.
  • GitHub: a website used to store code managed with Git online.

From this you can infer that there are two different places where your project is stored:

  • Cloud: the project lives on GitHub, inside a repository. It can be edited directly on the platform, but that is not as convenient as using a code editor. Depending on the repository's visibility, the code can be seen by everyone or only by you.

  • Local: your project lives on your computer and is edited with your favourite code editor. Only you can see the changes and how the project evolves. To upload those changes, you have to use Git.

Understanding this is crucial, because it becomes important when explaining branches, merges and similar concepts. It is essential to know where your code "lives" at any given moment.

Since your code lives in two places at once, how do you upload your local changes to the cloud version of the project? First, you need to set up authentication, which is how you prove to GitHub that you are allowed to push changes. Then you need to set up your identity, so that every change is tied to you and everyone knows who to blame.

HTTPS vs SSH

When you run Git operations such as clone, push or pull, your local machine and GitHub's servers have to connect over the internet. Before transferring any data, GitHub's servers must verify two things:

  1. Authentication: making sure you are who you say you are.
  2. Authorisation: making sure you are allowed to modify the repository.

Git lets you establish this connection with two different transport protocols, each with its own authentication mechanism. This is the key difference between HTTPS and SSH, and the choice determines how you connect and authenticate with the remote server. It is a communication layer, not a content layer, since it does not change the way you use Git at all.

HTTPS

This is the same protocol your browser uses, and authentication is based on a credential you provide. Historically, that credential was your password, but GitHub dropped it for Git operations to make the process more secure. Now you generate a Personal Access Token (PAT) in your account: a long string that acts as a revocable credential in place of your password.

To create a PAT, go to Settings, then Developer Settings and then Personal access tokens. There you can choose between two kinds of token, and the key difference between them is how their permissions are defined:

  • Tokens (Classic): the old kind. They let you choose from broad categories of permissions (scopes), but they cannot be restricted to specific repositories: any scope related to your code necessarily applies to all your repositories at once. In that sense they follow an "all or nothing" approach. They can also be set to never expire, which is convenient but carries an obvious risk, since a leaked credential stays valid indefinitely.

  • Fine-grained tokens: the new kind, created precisely to overcome the limitations of the classic ones. You have full control over which repositories the token is tied to and what privileges it grants on them, and they must have an expiry date. Thanks to this granularity, they are the recommended option for everyone, and an organisation can also oversee and approve which fine-grained tokens are created within it.

What is a scope?
A scope is a category of permissions that answers a single question: what can this token do with your account?

Classic tokens are built entirely on this model. Instead of granting unrestricted access, you pick from a predefined list of scopes, and the token can only do what those scopes allow. The good practice here is to apply the principle of least privilege, which says a credential should receive the minimum access its task requires and nothing more.

Some scopes act as umbrellas that contain others: repo grants full read and write access to both public and private repositories, while public_repo is limited to public ones. However, this access does not cover everything: deleting a repository requires the separate delete_repo scope, kept apart precisely because the operation is irreversible, and modifying GitHub Actions workflow files requires the workflow scope, which repo alone does not cover. The rule of thumb is to grant only what is strictly necessary.

This scope-based approach is what sets classic tokens apart from fine-grained ones: the latter replace broad scopes with per-repository permissions, which lets you limit a token to a single repository and grant it, for example, read-only access to its issues alone.

Once the token is defined, this is how you use it:

The PAT directly replaces your password in any Git operation done over HTTPS. When you run a command that contacts the remote through an HTTPS URL (for example, cloning a private repository or running git push), Git will ask for your credentials in the terminal:

Username for 'https://github.com': your-github-username
Password for 'https://github.com': # paste your PAT here

The crucial point is that, where Git asks for the Password, you paste the token and not your account password, which GitHub no longer accepts for Git operations.

Entering the token again for every operation would be impractical, so Git provides a credential helper that stores it for you. On Linux you have several options, listed here from least to most secure:

# In-memory cache: temporary, writes nothing to disk.
# Remembers the token for 15 minutes by default (configurable with --timeout).
git config --global credential.helper cache

# Plain-text store: convenient, but saves the token UNENCRYPTED in ~/.git-credentials. Persistent.
git config --global credential.helper store

# System keyring through libsecret (recommended on Linux Mint): stores the token encrypted,
# integrated with the desktop's credential manager. Persistent.
git config --global credential.helper libsecret

Avoid a practice you will often see online: embedding the token directly in the remote URL (https://<token>@github.com/user/repo.git). It works, but it leaves the token in plain text inside the repository configuration and in your shell history, which defeats much of its purpose.

To sum up the procedure:

SSH

The Secure Shell (SSH) protocol authenticates you with asymmetric, or public-key, cryptography. Unlike HTTPS, where you have to type in a credential, here you generate a key pair, the private key and the public key, which are related asymmetrically. This means that whatever is signed with the private key can be verified with the public one, but not the other way round. In this protocol, what you must protect is the private key: the public key is derived from it, but the reverse process is computationally infeasible, so the private key cannot be worked out from the public one.

  • Public key (id_ed25519.pub): its job is to verify you. You register it on GitHub, which uses it to check the response generated by your private key and confirm that it comes from the matching key.

  • Private key (id_ed25519): its job is to prove your identity. It never leaves your machine and it is the one that answers GitHub's random challenge by signing it. Only whoever holds this key can produce a valid response.

Notice that during this procedure your private key never travels over the internet: GitHub sends the challenge, your private key solves it locally and sends back only the response, and then the public key stored on GitHub verifies it. What's more, once it is set up, SSH does not require you to enter credentials interactively for each operation.

To set up the SSH protocol on GitHub, first check whether you already have a key and, if not, generate one:

ls -al ~/.ssh                                       # list existing keys (look for id_ed25519 and id_ed25519.pub)
ssh-keygen -t ed25519 -C "name/email/whatever"     # generate a new key pair

When prompted, accept the default location (~/.ssh) and, optionally, set a passphrase. Then print your public key and copy its entire contents:

cat ~/.ssh/id_ed25519.pub

Go to Settings \(\rightarrow\) SSH and GPG keys \(\rightarrow\) New SSH key, paste the value and save it. Finally, check that everything works:

ssh -T git@github.com

If the setup is correct, GitHub replies with:

Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.

To sum up the procedure:

Note

Both protocols are perfectly valid, so the choice is purely a matter of preference. I lean towards SSH, since once it is set up there is no friction at all —no token to paste or renew— and I believe that, as long as you protect your private key and keep your account secure, SSH is perfectly safe for personal use. For day-to-day work, that combination of convenience and security is hard to beat, so SSH is my default choice.

Warning

If you set up the SSH channel, you must clone the repo with its SSH URL. If you set up the HTTPS channel, you must clone the repo with its HTTPS URL. You can change how you communicate with the repo:

git remote set-url origin git@github.com:user/repo.git # to SSH
git remote set-url origin https://github.com/user/repo.git  # to HTTPS

User name and email

To finish setting up GitHub on your computer, you need to set your user.name and user.email. These are the two values Git stamps on every commit, so that each change is permanently tied to an author.

This is the identity part that GitHub requires alongside authentication: not whether you are allowed to push, but who actually made the change. You set them once with:

git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"

The --global flag applies this identity to every repository on your machine. If you need a different identity for a particular project, run the same commands without the --global flag from inside that repository. That way you set a local user.name and user.email that override the global configuration in that specific repo.

As good practice, make sure the email matches one registered on your GitHub account, since that links your commits to your profile. If it doesn't match, the commits will still be created, but GitHub will not attribute them to you.

GPG keys (optional)

So far, in the two previous sections, we have talked about the HTTPS and SSH protocols. Both answer the same question: are you allowed to push to this repository? Now we move on to the GPG key, which answers a different one: was it really you who made this change?

A GPG key does not grant any access. What it does is let you cryptographically sign your commits, proving that you wrote them. Bear in mind that for personal, everyday use it is not necessary, but it becomes valuable in professional or collaborative settings. For example, you will need it in some open source projects or teams whose policies require the authorship of every commit to be verifiable.

The reason has to do with how authorship is recorded. Every commit carries the name and email you configured with user.name and user.email, but they are plain text fields that anyone can fill in with any value. Nothing stops a third party from creating a commit that, at first glance, looks like yours. Signing a commit with your private GPG key closes that gap: GitHub verifies the signature with the public GPG key you have registered and, if it matches, marks the commit as Verified. In short, user.name and user.email declare who you are, while a GPG signature proves it.

Like SSH, GPG is based on a key pair: a private key that stays on your machine and does the signing, and a public key that you upload to GitHub for verification. The setup goes as follows.

To set up your GPG key, first check whether you already have one and, if not, generate a new one:

gpg --list-secret-keys --keyid-format=long   # list existing keys
gpg --full-generate-key                       # generate a new one (interactive)

During generation, accept the default algorithm, set an expiry date if you like and use the same email you use on GitHub, so that the signature can be linked to your account.

Next, find your key's long identifier and export its public part:

1
2
3
4
5
# The key ID is the value that appears after the algorithm.
gpg --list-secret-keys --keyid-format=long

# Export the public key in the text format GitHub expects:
gpg --armor --export YOUR_KEY_ID

Copy the whole block, including the -----BEGIN PGP PUBLIC KEY BLOCK----- and -----END...----- lines, and add it on GitHub under Settings \(\rightarrow\) SSH and GPG keys \(\rightarrow\) New GPG key.

Finally, tell Git which key to use and to sign your commits automatically:

1
2
3
4
5
git config --global user.signingkey YOUR_KEY_ID
git config --global commit.gpgsign true   # sign every commit by default

# On Linux, this lets GPG ask for the passphrase in the terminal:
export GPG_TTY=$(tty)

From then on your commits will be signed, and you can check it by running git log --show-signature locally or by looking for the Verified label next to the commit on GitHub.

If you would rather not sign every commit, leave out commit.gpgsign and add the -S flag to the ones you choose instead: git commit -S -m "message".