How to Install Git on Ubuntu (and Configure It Properly)
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.
git --versionWhat 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.0If 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 gitcontinue with the next step.
Step 2 — Update the package index
Section titled “Step 2 — Update the package index”sudo apt updatesudo apt updateWhat 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.
Step 3 — Install Git
Section titled “Step 3 — Install Git”sudo apt install gitapt 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:
git --versionStep 4 — Configure your identity
Section titled “Step 4 — Configure your identity”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.
-
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.
-
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.
-
Verify both were written.
Terminal window git config --global user.namegit config --global user.emailEach command prints the value you just set.
Step 5 — Set the default branch name
Section titled “Step 5 — Set the default branch name”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 namehint: is subject to change. To configure the initial branch name to use in allhint: 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:
git config --global init.defaultBranch mainThis affects only repositories created after you set it. To rename the branch in a repository that already exists:
git branch -m master mainStep 6 — Choose an editor
Section titled “Step 6 — Choose an editor”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:
git config --global core.editor "nano"Simple and always installed. Save with Ctrl+O, exit with Ctrl+X.
git config --global core.editor "vim"Write and quit with :wq. Abort with :q!, which cancels the commit.
git config --global core.editor "code --wait"The --wait flag is required. Without it, code returns immediately and Git sees an empty message.
Step 7 — Inspect your configuration
Section titled “Step 7 — Inspect your configuration”Two commands cover almost every configuration question.
git config --listWhat 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.
git config --list --show-scope --show-originWhat 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 Nameglobal file:/home/you/.gitconfig user.email=you@example.comglobal file:/home/you/.gitconfig init.defaultbranch=mainlocal file:.git/config core.repositoryformatversion=0local file:.git/config core.filemode=truelocal file:.git/config core.bare=falselocal file:.git/config core.logallrefupdates=trueConfiguration scopes
Section titled “Configuration scopes”That output introduces the three scopes. They form a hierarchy, and the most specific one wins.
| Scope | Flag | File | Applies to |
|---|---|---|---|
| System | --system | /etc/gitconfig | Every user on the machine |
| Global | --global | ~/.gitconfig | Your user account, all repositories |
| Local | --local | .git/config | One 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:
cd ~/work/some-projectgit 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.
sudo add-apt-repository ppa:git-core/ppasudo apt updatesudo apt install gitTo confirm which one you are running afterwards:
git --versionwhich gitOptional: credential handling
Section titled “Optional: credential handling”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:
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:
sudo apt install build-essential pkg-config libsecret-1-devsudo make --directory=/usr/share/doc/git/contrib/credential/libsecretgit config --global credential.helper \ /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecretThe 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.
A high-level note on SSH
Section titled “A high-level note on SSH”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.
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.
Uninstalling Git
Section titled “Uninstalling Git”Two levels, depending on what you want removed.
Remove the package but keep its system-wide configuration files:
sudo apt remove gitRemove the package and its configuration files:
sudo apt purge gitYour personal configuration in ~/.gitconfig is not removed by either command. Delete it manually if
you want a completely clean slate.
Troubleshooting
Section titled “Troubleshooting”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 addressYou 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:
eval "$(ssh-agent -s)"ssh-add ~/.ssh/id_ed25519Git 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.
What You Learned
Section titled “What You Learned”git --versiontells you both whether Git is installed and which version you have.sudo apt updatefollowed bysudo apt install gitinstalls Git on Ubuntu.- Git requires
user.nameanduser.emailbefore it will create a commit, and they become permanent parts of every commit. init.defaultBranch mainsets 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-originshows exactly which file supplies each value.- The
git-corePPA offers newer Git than Ubuntu packages, at the cost of trusting a third-party repo.
Try It Yourself
Section titled “Try It Yourself”- Run
git --versionand note the version. - Set your name and email with
--global, then confirm withgit config --global --list. - Create a scratch directory,
cdinto it, and rungit init. - Inside it, run
git config --local user.email "local@example.com". - Run
git config --show-scope --get user.email. Predict the answer before you press Enter. - Now
cdout 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.
Next Lesson
Section titled “Next Lesson”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.