Operating System Ubuntu

How to Install and Set Up Git on Ubuntu 26.04 LTS

Install Git on Ubuntu 26.04 LTS, configure your identity, default branch and SSH key, and create your first repository and commits.

This guide installs Git on Ubuntu 26.04 LTS and takes you through the first-time setup: your identity, the default branch name, an SSH key for talking to remote repositories, and your first repository with a commit in it. Everything applies to Ubuntu 24.04 LTS as well; the only difference is the Git version the distribution ships (2.53 on 26.04, 2.43 on 24.04).

Ubuntu 14.04 and the other releases covered by older tutorials are long past end of life. The basic commands have not changed since then, but a few defaults and recommendations have, and they are noted below.

Requirements

  • A server or desktop running Ubuntu 26.04 LTS or 24.04 LTS.
  • A regular user with sudo rights; the examples use deploy. Only the installation needs sudo. Run every other command as the user who will work with Git, not as root.

Step 1: Install Git

Git is in Ubuntu's main repository:

sudo apt update
sudo apt install git

Check the result:

git --version

The distribution package receives security updates through the normal apt upgrade process and is the right choice for almost everyone. If you need the newest upstream release, the Git project points Ubuntu users to a PPA maintained for that purpose:

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

Step 2: Set your identity

Every commit records the name and email address of its author, and that information cannot be changed afterwards without rewriting the commit. Set both before you commit anything:

git config --global user.name "Deploy User"
git config --global user.email "deploy@example.com"

--global stores the setting in ~/.gitconfig, so it applies to every repository you use under this account. To use a different identity for a single project, run the same commands inside that repository without --global; the value is then written to the repository's .git/config and takes precedence.

Step 3: Choose the default branch name and editor

When you create a repository, Git names its first branch master unless told otherwise. Most hosting services use main, and Git itself has announced that main becomes the default in Git 3.0. Set it now so your local repositories match:

git config --global init.defaultBranch main

Git opens a text editor when it needs a commit message. To choose which one, for example nano:

git config --global core.editor nano

Review everything Git has picked up, and from which file each value comes:

git config --list --show-origin

Step 4: Create an SSH key

To clone from and push to a remote server over SSH (your own server or a hosting service), you need a key pair. First check whether you already have one:

ls ~/.ssh

A pair looks like id_ed25519 (private key) and id_ed25519.pub (public key). If there is none, generate one:

ssh-keygen -t ed25519 -C "deploy@example.com"

Press Enter to accept the default file location, then type a passphrase. The passphrase protects the key if the file is ever copied; you can leave it empty, but then anyone who obtains the file can use it.

Print the public key:

cat ~/.ssh/id_ed25519.pub

Copy that single line into the "SSH keys" section of your account on the Git hosting service, or append it to ~/.ssh/authorized_keys of the account on your own Git server. Share only the .pub file; the private key never leaves your machine.

Step 5: Create your first repository

Create a project directory and turn it into a repository:

mkdir ~/myproject
cd ~/myproject
git init

Git creates a hidden .git directory that holds the whole history. Nothing is tracked yet. Add a file and look at the state of the working directory:

echo "# My project" > README.md
git status

git status lists README.md as untracked. Committing is a two-step process: git add places changes in the staging area, and git commit records what is staged.

git add README.md
git commit -m "Initial commit"

To stage every new and changed file in the current directory at once, use git add .. Check first with git status so that you do not commit files by accident.

Step 6: Make changes and inspect history

Change the file, see what changed, and commit again:

echo "Notes for the project." >> README.md
git diff
git add README.md
git commit -m "Add project notes"

Show the history, one line per commit:

git log --oneline

Files that should never be committed (logs, build output, files containing passwords) belong in a .gitignore file in the project directory, one pattern per line:

printf '%s\n' '*.log' '.env' > .gitignore
git add .gitignore
git commit -m "Add .gitignore"

Step 7: Connect to a remote repository

Create an empty repository on your Git server or hosting service and copy its SSH URL. Register it under the conventional name origin and push your branch (replace the URL with your own):

git remote add origin git@git.example.com:deploy/myproject.git
git push -u origin main

-u links your local main to the remote branch, so from now on plain git push and git pull are enough. The first connection asks you to confirm the server's host key fingerprint; compare it with the one published by the service before answering yes.

To start from a repository that already exists on a server, clone it instead of running git init:

git clone git@git.example.com:deploy/myproject.git

Troubleshooting

  • "Author identity unknown" when committing: Step 2 was skipped or was run as a different user. Run git config --global --list as the user who commits.
  • "src refspec main does not match any" when pushing: your branch has another name (usually master, if the repository was created before Step 3) or has no commits yet. Rename it with git branch -m main.
  • "Permission denied (publickey)": the public key is not registered on the server, or you are running Git as a different user than the one who owns the key (for example with sudo).

Frequently asked questions

Should I run Git with sudo?

No. Git reads its settings and SSH keys from the home directory of the user running it, so with sudo it uses root's configuration and leaves root-owned files in your repository. Use sudo only for apt.

Where does Git store its configuration?

In three places, each overriding the previous one: the system file /etc/gitconfig (--system), your personal ~/.gitconfig or ~/.config/git/config (--global), and .git/config inside a repository (--local, the default when you are in one).

How do I rename the branch of a repository I created earlier?

init.defaultBranch only affects repositories created afterwards. In an existing repository run git branch -m master main, then push the new name with git push -u origin main.

Can I use HTTPS instead of SSH for remotes?

Yes. Use the repository's https:// URL with git clone or git remote add; Git then asks for a user name and a password or access token instead of using your SSH key.

Share story:

Continue reading other guides on How VPS.