Git is the version-control system behind nearly every software project on the planet, and if you work on a Linux server you will need it installed and configured before you can do anything else. In this guide you will install Git on Ubuntu 24.04 from the official repositories, set your identity and sane defaults, wire up SSH authentication for GitHub or GitLab, and prove everything works with your first commit. By the end you will have a Git setup that is ready for real work.
This tutorial is written for Ubuntu 24.04 LTS, but every command works identically on Ubuntu 22.04 LTS.
Prerequisites
- An Ubuntu 24.04 or 22.04 server or desktop.
- A user account with sudo privileges.
- An internet connection (to reach Ubuntu's package repositories).
- Basic comfort running commands in a terminal.
Step 1: Update the package index
Before installing anything, refresh APT's package lists so you pull the newest package metadata:
sudo apt update
This command syncs your local package index with Ubuntu's repositories. It installs nothing by itself, but it guarantees the Git package you fetch in the next step is the current one for your release.
Step 2: Install Git
Now install Git from Ubuntu's official main repository:
sudo apt install -y git
The -y flag answers "yes" to the confirmation prompt so the install runs unattended. On Ubuntu 24.04 this installs Git 2.43 (or newer, depending on security updates) — a fully current, stable release that is maintained by Canonical.
Step 3: Verify the installation
Check that Git is installed and see exactly which version you have:
git --version
You should see output similar to:
git version 2.43.0
If the command prints a version number, Git is installed correctly. The exact patch version does not matter — anything in the 2.4x series is current for Ubuntu 24.04.
Step 4: Set your identity (name and email)
Every Git commit is stamped with an author name and email. Set them globally so every repository on this machine uses them:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
Replace "Your Name" and "you@example.com" with your real name and the email you use for GitHub, GitLab, or your company. The --global flag writes the settings to ~/.gitconfig, applying them to every repository you work with. If you skip this step, Git will refuse to create commits later with the error "Author identity unknown".
Step 5: Set useful defaults
A few one-time settings make Git behave the way modern workflows expect. Run each of these once:
git config --global init.defaultBranch main
This makes git init create a main branch instead of the legacy master name, matching GitHub and GitLab's default.
git config --global pull.rebase false
This tells git pull to merge remote changes instead of rebasing — the classic, predictable behavior that beginners should start with.
git config --global core.editor "nano"
This sets nano as the editor Git opens for commit messages and interactive tasks, which is far friendlier than the default vim if you are not a vim user.
Step 6: Review your configuration
List everything Git will actually use, so there are no surprises:
git config --list
You should see output like:
user.name=Your Name
user.email=you@example.com
init.defaultbranch=main
pull.rebase=false
core.editor=nano
If any value is wrong, just re-run that git config command with the corrected value — it overwrites the old one.
Step 7: Set up SSH authentication (for GitHub / GitLab)
Cloning and pushing over HTTPS asks for credentials every time, and password authentication is being phased out. SSH keys are the standard fix. Generate an Ed25519 key pair:
ssh-keygen -t ed25519 -C "you@example.com"
Press Enter to accept the default file location (~/.ssh/id_ed25519) and choose a strong passphrase when prompted. Ed25519 is the current recommended key type — it is faster and shorter than RSA.
Next, start the SSH agent and add your key:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
The first command starts the agent in the background; the second loads your private key into it so you only type the passphrase once per session.
Finally, display your public key and add it to your Git host (GitHub: Settings → SSH and GPG keys → New SSH key):
cat ~/.ssh/id_ed25519.pub
Copy the entire output line (it starts with ssh-ed25519) into the "Key" field on the site. Only the public key ever leaves your machine — the private key in id_ed25519 stays secret.
Step 8: Make your first commit
Prove the whole setup works end to end. Create a test directory, initialize a repository, add a file, and commit it:
mkdir ~/git-test && cd ~/git-test
git init
git init creates the hidden .git directory that turns an ordinary folder into a repository. Because of Step 5, the default branch is named main.
echo "Hello, Git!" > hello.txt
git add hello.txt
git commit -m "First commit: testing the Git setup"
git add stages the file, and git commit records it in history with your name and email from Step 4. Inspect the result:
git status
git log --oneline
git status shows a clean working tree ("nothing to commit"), and git log --oneline prints your commit with its hash, e.g. a1b2c3d First commit: testing the Git setup. If you see your commit, identity, branching, and committing all work.
When you are done, remove the test directory:
cd ~ && rm -rf ~/git-test
Verify it worked
Run this quick checklist. All of these should succeed without errors:
git --versionprints a 2.4x version number.git config --global --get user.emailprints the email you set.- You created, committed, and listed a commit in the test repository.
- Optional:
ssh -T git@github.comprints a greeting like "Hi username! You've successfully authenticated" after you add your key to GitHub.
If any check fails, work through the fixes in the next section.
Common issues & fixes
1. "git: command not found"
The package is not installed, or your shell has not picked it up. Re-run the install and confirm APT reported no errors:
sudo apt update && sudo apt install -y git
which git
which git should print /usr/bin/git. If it does not, try opening a fresh terminal session.
2. "Author identity unknown" when committing
Git refuses to commit because user.name or user.email is missing. Set both globally as in Step 4, then retry the commit. You can confirm what is set with git config --global --list | grep user.
3. "Permission denied (publickey)" when cloning or pushing
The Git host does not recognize your SSH key. Double-check that the full id_ed25519.pub contents were pasted into the SSH key settings on GitHub/GitLab, that the key is actually loaded (ssh-add -l should list it), and that you are using the SSH clone URL (git@github.com:...) rather than the HTTPS one.
4. "fatal: not a git repository"
You ran a Git command outside any repository. Either cd into your project folder or run git init there first. A quick sanity check: ls -a should show a .git directory in a working repository.
5. "error: src refspec main does not match any" on push
You tried to push before making your first commit, or your branch is named differently. Make at least one commit (see Step 8), confirm the branch name with git branch --show-current, and push with git push -u origin main (adjust main if your branch has another name).
You now have Git installed on Ubuntu 24.04 with your identity configured, sensible defaults in place, SSH authentication ready for GitHub or GitLab, and a tested commit workflow. The same steps apply verbatim to Ubuntu 22.04.
If you would rather have an expert handle server setup, Git hosting, or CI/CD pipelines for your team, XpertBees offers managed DevOps and server configuration services — get in touch and we will get your infrastructure production-ready.



