Skip to main content

Dev tools and workflow · Beginner · 14 min

How to Install and Configure Git

By Padding · Updated 15 Nov 2026
Ubuntu 26.04.1 LTS · Git 2.53 (Ubuntu package) · OpenSSH

You will install Git 2.53 from Ubuntu 26.04's own packages, set your name and email, make main the default branch, choose an editor and a few safe defaults, use a different email for work projects, and create an SSH key to use with GitHub.

On this page

Git records the history of your files and lets you work on changes side by side. Every commit it makes carries your name and email, so Git needs to know who you are before it does anything useful. It also has a number of settings that decide how it behaves, and a few good defaults save you from confusing surprises later.

This tutorial installs Git and sets it up properly. It is the first of a series that takes you from your first commit to a pull request on GitHub. Ubuntu 26.04 packages Git 2.53, which gets security fixes through the normal updates, and that is the version used here. Newer releases appear on git-scm.com first. If you need one, the Git maintainers publish packages in the ppa:git-core/ppa repository. You do not need it for anything in this series.

Step 1 · Install Git

Install the git package. The second command prints the version:

Bash
sudo apt update
sudo apt install -y git
git --version

Check it worked

The output of git --version is git version 2.53.0. Ubuntu may ship a later patch release of 2.53 after security updates.

Step 2 · Tell Git who you are

Git stores your identity in a configuration file in your home folder. The --global flag means the setting applies to every repository you use. Use your real name, or the name you want shown in project histories, and an email address that you are happy to have in public commits. Replace the two values:

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

If you push to GitHub, the email in your commits is visible to anyone who can see the repository. GitHub gives each account a private address of the form ID+username@users.noreply.github.com, which you can find under Settings, Emails. Use it as user.email if you do not want to publish your own, and keep "Block command line pushes that expose my email" switched on in the same page if you want GitHub to refuse commits that carry another address.

Check it worked

Bash
git config --global user.name
git config --global user.email
cat ~/.gitconfig

The first two commands print the values you set. ~/.gitconfig is the file that holds them, and it has a [user] section with name and email. You can edit the file by hand as well, since git config only changes the text in it.

Step 3 · Make main the default branch

A new repository starts with one branch. Git's own default is named master. Most projects and GitHub now use main, and a mismatch between the two names is a common source of confusion when you first push. Set the name Git uses for new repositories:

Bash
git config --global init.defaultBranch main

This setting affects repositories you create from now on. It does not rename the branch of a repository that already exists.

Check it worked

Create a throwaway repository and ask which branch it is on:

Bash
mkdir -p ~/git-demo
cd ~/git-demo
git init
git branch --show-current

git init prints Initialized empty Git repository in followed by the path of its .git folder, and git branch --show-current prints main. The branch has no commits yet, but it already has its name.

Step 4 · Choose your editor

Git opens a text editor when it needs a message from you, for example for a commit with no -m option. On Ubuntu the default is nano, which is simple to use: type your message, press Ctrl+O and Enter to save, and Ctrl+X to leave. To make that explicit, or to pick another editor, set core.editor:

Bash
git config --global core.editor nano

Use vim if you prefer it. For Visual Studio Code, the value is "code --wait", where --wait makes Git wait until you close the file's tab.

Check it worked

git var GIT_EDITOR prints the command Git will launch, after taking the GIT_EDITOR environment variable and the core.editor setting into account:

Bash
git var GIT_EDITOR

The output is nano. If it prints something else, the GIT_EDITOR environment variable is overriding your setting, because it wins over core.editor. Run echo "$GIT_EDITOR" to see. VISUAL and EDITOR are consulted only when core.editor is not set.

Step 5 · Set a few safe defaults

Three more settings prevent trouble before you have met it. Each is explained in the Git documentation for git config:

Bash
git config --global pull.ff only
git config --global push.autoSetupRemote true
git config --global merge.conflictStyle zdiff3
  • pull.ff only makes git pull stop with an error when your branch and the remote have both moved, instead of creating a merge commit you did not ask for. You then choose between merging and rebasing on purpose.
  • push.autoSetupRemote true makes the first git push of a new branch create the branch on the remote and link the two, so you no longer see the "no upstream branch" error.
  • merge.conflictStyle zdiff3 makes conflict markers show the original text from before both changes, alongside the two versions, which makes conflicts easier to resolve. You will see it in the tutorial on merge conflicts.

Check it worked

Bash
git config --global --list --show-origin

Each line starts with file: and the path of your ~/.gitconfig, then a key=value pair. You see user.name, user.email, init.defaultbranch, core.editor, pull.ff, push.autosetupremote and merge.conflictstyle, with the keys in lower case because Git treats them without regard to case.

Step 6 · Know where settings live

Git reads configuration from three places, and a later one overrides an earlier one:

  • The system file, /etc/gitconfig, applies to every user on the machine. You rarely edit it.
  • The global file, ~/.gitconfig (or ~/.config/git/config), applies to all your repositories. This is the one you have been editing.
  • The local file, .git/config inside a repository, applies to that repository only and wins over the other two.

Without --global, git config writes to the local file. To give one project a different email, run git config user.email "other@example.com" inside it. Here is the effect:

Bash
cd ~/git-demo
git config user.email "other@example.com"
git config user.email
git config --show-origin user.email

The second command prints other@example.com. The third prints it with file:.git/config in front, which tells you which file the winning value came from. Remove the local override again, so the repository uses your global identity:

Bash
git config --unset user.email
git config user.email

The output is the global address you set earlier.

Step 7 · Use a different identity for work projects

If you keep work projects in one folder, Git can pick the right email by itself, so you never commit to a work repository with your personal address. This uses a conditional include: when a repository's .git folder is under a given path, Git also reads another file.

This step writes two settings with example values. Replace you@company.example with your real work email, and replace ~/git-demo-work/ in the second command with the folder where your work repositories really live, such as ~/work/. The folder name here is a throwaway one, so that the demonstration cannot touch a folder you already use. The command adds to ~/.gitconfig, and ~/.gitconfig-work is a new file, so check that you do not already have one with that name:

Bash
cat > ~/.gitconfig-work <<'INI'
[user]
	email = you@company.example
INI
git config --global 'includeIf.gitdir:~/git-demo-work/.path' '~/.gitconfig-work'

The gitdir: pattern must end in a slash for a folder, and ~ means your home folder. The file ~/.gitconfig-work holds only what differs. Check both cases:

Bash
mkdir -p ~/git-demo-work/project
cd ~/git-demo-work/project
git init
git config user.email
cd ~/git-demo
git config user.email

The repository under ~/git-demo-work reports you@company.example and the one outside it reports you@example.com.

Step 8 · Create an SSH key for GitHub

To push to GitHub without typing a password each time, you give GitHub a public key and keep the matching private key on your computer. SSH keys come in pairs: the public half is safe to share, and the private half must stay secret.

Install the OpenSSH client if it is missing, then create the key pair. The ed25519 type is short and strong, -C adds a label that helps you recognise the key later, and -f names the file so it does not overwrite an existing key. ssh-keygen asks for a passphrase. Choose one: it encrypts the private key file, so a stolen laptop or backup does not hand over access to your repositories:

Bash
sudo apt install -y openssh-client
Bash
ssh-keygen -t ed25519 -C "git@my-laptop" -f ~/.ssh/id_ed25519_github

Tell SSH to use this key for GitHub, and only this key. The first command creates ~/.ssh/config with private permissions if it does not exist yet, and leaves it alone if it does. Then the entry is appended, so your existing entries stay:

Bash
mkdir -p -m 700 ~/.ssh
(umask 077; touch ~/.ssh/config)
cat >> ~/.ssh/config <<'CONF'

Host github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_github
    IdentitiesOnly yes
CONF

Check it worked

Bash
ls -l ~/.ssh/id_ed25519_github ~/.ssh/id_ed25519_github.pub
ssh -G github.com | grep -i '^identityfile'
ssh-keygen -lf ~/.ssh/id_ed25519_github.pub

The private key's permissions start with -rw-------, meaning only you can read it. SSH refuses keys that other users can read. ssh -G github.com prints the settings SSH would use for that host without connecting, and its identityfile line ends in .ssh/id_ed25519_github. The last command prints the key's fingerprint, which starts with 256 SHA256: and ends with the label and (ED25519).

Step 9 · Add the public key to GitHub and test it

Print the public key. It is one line, and it is safe to copy and paste:

Bash
cat ~/.ssh/id_ed25519_github.pub

The line starts with ssh-ed25519. Never paste the contents of the file without .pub, which is the private key. On GitHub, open Settings, SSH and GPG keys, choose New SSH key, give it a title that names this computer, leave the type as Authentication Key, paste the line and save.

Now test the connection. The first time, SSH shows GitHub's host key fingerprint and asks whether to continue. Compare it with the fingerprints GitHub publishes at docs.github.com. The ED25519 fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU. Type yes only if they match:

Bash
ssh -T git@github.com

The expected reply is Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access. with your own username. GitHub then closes the connection, and the command exits with status 1, which is normal for this test. If you see Permission denied (publickey), SSH is not offering the key GitHub knows. Re-run ssh -G github.com | grep -i identityfile, check the key on GitHub's SSH keys page, and run ssh -vT git@github.com to see which keys SSH tried.

From now on, clone repositories with their SSH address, such as git@github.com:owner/repo.git. The last tutorial of this series uses it to push and open a pull request. If you would rather use HTTPS, GitHub no longer accepts account passwords for Git, so you would need a personal access token or the GitHub CLI's login, and SSH avoids that.

Step 10 · Make a first commit to check the setup

Prove that your identity ends up in the history. Make an empty commit in the demo repository, and read back its author:

Bash
cd ~/git-demo
git commit --allow-empty -m "Check my Git identity"
git log -1 --format='%an <%ae>'

The output is Your Name <you@example.com>, the name and email you set. If Git had refused with Please tell me who you are, the identity step did not take effect.

Step 11 · Clean up

Delete the two throwaway folders this tutorial made. Your Git settings and SSH key stay, because they are the point of this tutorial, and so does the work-identity setting, which still points at the example folder: edit it in ~/.gitconfig to your real work folder and your real work email, or remove those lines. If you made the key only to follow along, remove it and delete it from GitHub's SSH keys page too:

Bash
cd ~
rm -rf ~/git-demo ~/git-demo-work

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.