Skip to content

How to Install Git on Ubuntu (and Configure It Properly)

Lesson 4 of 12Beginner9 min readGit Fundamentals · Getting StartedVerified: Git 2.43.0 on Ubuntu 24.04.3 LTS

On Ubuntu, Git is a single apt package. Installation is one command; the part worth spending time on is the configuration that follows, because a misconfigured identity will be baked permanently into every commit you make.

Step 1 — Check whether Git is already installed

Section titled “Step 1 — Check whether Git is already installed”

Many Ubuntu systems already have Git, pulled in as a dependency of something else. Check before installing anything.

Terminal window
git --version

What it doesPrints the version of the git executable found on your PATH.

Why we run itIt answers both questions at once: whether Git is installed, and which version you have.

Expected resultA line such as git version 2.43.0. If Git is not installed, the shell reports that the command was not found — and Ubuntu usually suggests the package that provides it.

git version 2.43.0

If you see a version, Git is installed and you can skip to configuration. If instead you see:

Command 'git' not found, but can be installed with:
sudo apt install git

continue with the next step.

Terminal window
sudo apt update
Terminal window
sudo apt update

What it doesDownloads the current package lists from every repository configured on your system. It does not install or upgrade anything.

Why we run itapt installs from its local cache of what is available. If that cache is stale, installation can fail or fetch an outdated package.

Expected resultSeveral Get: and Hit: lines, ending with a summary of how many packages can be upgraded.

Terminal window
sudo apt install git

apt lists what it will install and asks for confirmation. Press Y then Enter. To skip the prompt in a script, add -y.

Then confirm the installation succeeded:

Terminal window
git --version

Git records an author name and email on every commit. It has no default for these, and it will refuse to commit until you set them.

  1. Set your name.

    Terminal window
    git config --global user.name "Your Name"

    This is a display name, not an account. Use the name you want to appear in project history.

  2. Set your email.

    Terminal window
    git config --global user.email "you@example.com"

    If you plan to push to a hosting service later, use the address associated with that account so your commits are attributed to you there.

  3. Verify both were written.

    Terminal window
    git config --global user.name
    git config --global user.email

    Each command prints the value you just set.

When Git initialises a repository, it creates a first branch. Without configuration it uses master and prints a hint saying so:

hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: git config --global init.defaultBranch <name>

Most projects and hosting platforms now use main. Setting it explicitly matches that convention and silences the hint:

Terminal window
git config --global init.defaultBranch main

This affects only repositories created after you set it. To rename the branch in a repository that already exists:

Terminal window
git branch -m master main

Some Git commands open an editor — most commonly git commit without -m. On Ubuntu the default is usually nano, which is fine and easy to exit (Ctrl+X).

To choose a different one:

Terminal window
git config --global core.editor "nano"

Simple and always installed. Save with Ctrl+O, exit with Ctrl+X.

Two commands cover almost every configuration question.

Terminal window
git config --list

What it doesLists every configuration setting in effect, merged across all scopes.

Why we run itIt shows you the values Git will actually use, rather than what any single file contains.

Expected resultA list of key=value lines, including the user.name, user.email and init.defaultbranch you just set. Note that Git normalises key names to lowercase in this output.

Terminal window
git config --list --show-scope --show-origin

What it doesLists every setting along with the scope it came from and the file that defines it.

Why we run itWhen a value is not what you expect, this shows which file is responsible — which is nearly always the actual question.

Expected resultThe same list, each line prefixed with system, global or local and the path to the file.

global file:/home/you/.gitconfig user.name=Your Name
global file:/home/you/.gitconfig user.email=you@example.com
global file:/home/you/.gitconfig init.defaultbranch=main
local file:.git/config core.repositoryformatversion=0
local file:.git/config core.filemode=true
local file:.git/config core.bare=false
local file:.git/config core.logallrefupdates=true

That output introduces the three scopes. They form a hierarchy, and the most specific one wins.

ScopeFlagFileApplies to
System--system/etc/gitconfigEvery user on the machine
Global--global~/.gitconfigYour user account, all repositories
Local--local.git/configOne repository only

Local overrides global, and global overrides system. --global is the right default for personal settings like identity and editor.

The local scope is genuinely useful for identity. If you contribute to work projects with one address and personal projects with another, set the exception inside the repository that needs it:

Terminal window
cd ~/work/some-project
git config --local user.email "you@company.example"

The --local flag is the default when you are inside a repository, so git config user.email "..." does the same thing. Being explicit avoids accidents.

Optional: installing a newer Git from the git-core PPA

Section titled “Optional: installing a newer Git from the git-core PPA”

Ubuntu’s packaged Git lags upstream by design. If you need newer features, the Git project maintains a PPA carrying current stable releases for supported Ubuntu versions.

Terminal window
sudo add-apt-repository ppa:git-core/ppa
sudo apt update
sudo apt install git

To confirm which one you are running afterwards:

Terminal window
git --version
which git

You only need this once you start pushing to a remote over HTTPS. Without a helper, Git prompts for credentials on every network operation.

Git ships a simple cache helper that keeps credentials in memory for a limited time:

Terminal window
git config --global credential.helper 'cache --timeout=3600'

That stores them in memory for one hour and never writes them to disk.

For a desktop system with GNOME Keyring, the libsecret helper stores credentials in the system keyring. Ubuntu ships its source rather than a compiled binary, so you build it once:

Terminal window
sudo apt install build-essential pkg-config libsecret-1-dev
sudo make --directory=/usr/share/doc/git/contrib/credential/libsecret
git config --global credential.helper \
/usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret

The apt line installs the compiler, pkg-config and the development headers the helper’s Makefile needs. Check /usr/share/doc/git/contrib/credential/ on your own system before running this — the contents of that directory are set by the git package, and the path can differ on other distributions.

Instead of HTTPS and tokens, you can authenticate to most Git hosts using an SSH key pair. The private key stays on your machine; the public key is uploaded to the host.

Terminal window
ssh-keygen -t ed25519 -C "you@example.com"

That writes ~/.ssh/id_ed25519 (private, never share it) and ~/.ssh/id_ed25519.pub (public, safe to upload). You then add the public key to your hosting account, and clone using an SSH URL rather than an HTTPS one.

SSH keys are a topic in their own right — agents, passphrases, per-host configuration and key rotation all matter. A future GitHub Engineering pillar will cover them properly. For now, know that the option exists and that HTTPS with a token is a perfectly reasonable starting point.

Two levels, depending on what you want removed.

Remove the package but keep its system-wide configuration files:

Terminal window
sudo apt remove git

Remove the package and its configuration files:

Terminal window
sudo apt purge git

Your personal configuration in ~/.gitconfig is not removed by either command. Delete it manually if you want a completely clean slate.

git: command not found after installing. Open a new terminal. Your shell caches the locations of executables; a fresh session re-scans PATH. If it persists, run which git and check that /usr/bin is on your PATH.

Unable to locate package git. The package index is stale or incomplete. Run sudo apt update first. If that fails, check that the universe and main repositories are enabled in your sources.

Author identity unknown when committing.

*** Please tell me who you are.
fatal: unable to auto-detect email address

You have not set user.name and user.email, or you set them in a scope that does not apply here. Return to Step 4 and use --global.

Permission denied running apt. Package management needs root. Prefix the command with sudo.

Could not open a connection to your authentication agent when using SSH. The SSH agent is not running in this shell:

Terminal window
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

Git prompts for a password on every push. No credential helper is configured. See credential handling above.

A hint about master still appears on git init. The init.defaultBranch setting was written to a different scope, or to a different user’s config because you ran the command under sudo. Never run git config --global with sudo — it configures root, not you.

  • git --version tells you both whether Git is installed and which version you have.
  • sudo apt update followed by sudo apt install git installs Git on Ubuntu.
  • Git requires user.name and user.email before it will create a commit, and they become permanent parts of every commit.
  • init.defaultBranch main sets the branch name for new repositories.
  • Configuration exists in three scopes — system, global and local — with the most specific winning.
  • git config --list --show-scope --show-origin shows exactly which file supplies each value.
  • The git-core PPA offers newer Git than Ubuntu packages, at the cost of trusting a third-party repo.
  1. Run git --version and note the version.
  2. Set your name and email with --global, then confirm with git config --global --list.
  3. Create a scratch directory, cd into it, and run git init.
  4. Inside it, run git config --local user.email "local@example.com".
  5. Run git config --show-scope --get user.email. Predict the answer before you press Enter.
  6. Now cd out of that directory and run the same command again.

Step 5 shows local; step 6 shows global. That is scope resolution in action, and it is the source of most “but I already configured that” confusion.

If you also work on Windows or macOS, the next two lessons cover those platforms. If Ubuntu is your only environment, skip ahead to Lesson 7 and build a real repository.