How To – Git: Installation


Git is a free and open source distributed version control system optimised for use with text based files. It is fast and efficient because it doesn’t work by keeping extra copies of files, but just the changes between versions and branches.

It is one of, if not the most, commonly used version control systems in the world.

Git is very commonly used in tandem with the cloud based GitHub service to simplify collaboration amongst geographically dispersed teams and groups. Even when used solely by an individual, GitHub is still often used in tandem to provide long term security against data loss.

Debian 12 (Bookworm) Installation

Ensure System is Up To date

sudo apt update
sudo apt full upgrade

Install Git

sudo apt install git

Set the Default Branch Name

By default git uses the name “master” for the default primary branch name. However, in more recent times it has become more standard to use the name “main” as the primary branch name. In fact this is the standard that has now been adopted by GitHub and so it is usually a good idea to make your own installation conform to this new norm to simplify any interactions with GitHub.

To do this simply issue the following command:

git config --global init.defaultBranch "main"

Set Convenient Default Values

Now configure git for your local needs. This involves setting up the name and email to use in any commits that you make along with a default text editor to use for interactive commit comments.

git config --global user.name "My Name"
git config --global user.email "me@mydomain.com"
git config --global core.editor "nano"

Git has a three level hierarchy of configuration data stored in files that are kept in three different locations:

  1. At the top level of the hierarchy the data is stored in the file “/etc/gitconfig” and stores the defaults to be used across all users and all repositories unless overridden. In other words system level defaults. To set these configuration values use “sudo” and specify the “–system” option flag like this:
    sudo git config –system core.editor “nano”
  2. At the next level down are the defaults for the logged in user. This data is stored in the file “~/.gitconfig” (or sometimes “~/.config/git/config”). The values stored here apply to any actions by the logged in user in any repository and will override anything specified at the system level and are set by passing the “–global” option flag like this:
    git config –global user.name “John Doe”
  3. Finally at the bottom level are the defaults for the current repository. These values are stored in a file called “.git/config” whose parent is the root of the actual repository and override anything set at either of the higher levels. To set these defaults you must be in the root directory of the desired repository and must pass the “–local” option flag like this:
    cd /path/to/root/of/repository”
    git config –local user.name “My Alias”

To list out all the configured defaults just type the command:

git config --list

This will list out all the values that have been set, but it won’t tell you at which level they have been set. Note that if you inside the directory structure of a repository, then this will include the values for that repository. However, if you are inside a repository directory structure when you issue the command then you will only get the values set for the “system” and “user” levels of the hierarchy.

Since the same value can be set at multiple hierarchy levels the output could display more than one value for that default. In such cases the last instance in the list is the one that will apply. To find out which value is stored in which file you can use the “–show-origin” option like this:

git config --list --show-origin

which will tell you which file each key=value was found in.

Configure for Authentication to GitHub

This step is optional, but it will vastly simplify your life if you want to use git alongside GitHub (Trust me. At some point you will!).

At some point during 2023 GitHub blocked the use of a username and password over https as a means for git to authenticate to it (for good security reasons). You can still authenticate over https, but you now need to use “Passkeys” instead of a password.

However, it is much simpler to use “ssh” with a shared public key. The chances are also good that you are already using ssh to remotely access other systems, in which case the setup is even simpler.

Generating SSH Public/Private Keys

First of all check whether you already have a pair of public/private ssh keys on your system:

ls -la ~/.ssh

You are looking for a pair of files named something along the lines of “id_rsa” and “id_rsa.pub” which are the private and public ssh keys using an RSA encryption method. If these two files (or the .ssh directory) are missing then you need to generate them. Otherwise you can skip this step.

To generate the required key pair instigate an interactive session using the following command:

cd
ssh-keygen -o

Then answer the questions generated. Below is an example set of question responses. Note that the defaults can generally be accepted unless you particularly want to change them:

Generating public/private rsa key pair.
Enter file in which to save the key (/home/myuser/.ssh/id_rsa):
Created directory '/home/myuser/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/myuser/.ssh/id_rsa.
Your public key has been saved in /home/myuser/.ssh/id_rsa.pub.
The key fingerprint is:
d0:82:24:8e:d7:f1:bb:9b:33:53:96:93:49:da:9b:e3 myuser@mylaptop.local

Adding an SSH Public Key to GitHub Account

To enable GitHub to authenticate your ssh session you will need to store the public ssh key into GitHub for each computer that you wish to be able to authenticate to your GitHub account. I assume that you you already have a GitHub account. If not then you will need to create one before continuing.

  1. Copy the full contents of your “~/.ssh/id_rsa.pub” file to the clipboard. The contents should start with “ssh-rsa” and end with “myusername@mycomputername”.
  2. Login to your GitHub account using a web browser (typically at “https://github.com/myusername”).
  3. Click on your avatar icon (top right corner of browser window). A menu will pop out from the right side of the screen. Select the “Settings” option.
  4. Choose “SSH and GPG keys” from the left hand menu.
  5. Click the green “New SSH key” button
  6. For the title enter text that will allow you to determine which computer this key is for.
  7. For the “Key type” leave it at the default of “Authentication Key”
  8. Paste the full contents of your public key file as the key.
  9. Click the “Add SSH key” button.

Leave a Reply