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:
sudo apt update
sudo apt install -y git
git --versionCheck 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:
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
git config --global user.name
git config --global user.email
cat ~/.gitconfigThe 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:
git config --global init.defaultBranch mainThis 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:
mkdir -p ~/git-demo
cd ~/git-demo
git init
git branch --show-currentgit 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:
git config --global core.editor nanoUse 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:
git var GIT_EDITORThe 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:
git config --global pull.ff only
git config --global push.autoSetupRemote true
git config --global merge.conflictStyle zdiff3pull.ff onlymakesgit pullstop 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 truemakes the firstgit pushof 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 zdiff3makes 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
git config --global --list --show-originEach 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/configinside 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:
cd ~/git-demo
git config user.email "other@example.com"
git config user.email
git config --show-origin user.emailThe 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:
git config --unset user.email
git config user.emailThe 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:
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:
mkdir -p ~/git-demo-work/project
cd ~/git-demo-work/project
git init
git config user.email
cd ~/git-demo
git config user.emailThe 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:
sudo apt install -y openssh-clientssh-keygen -t ed25519 -C "git@my-laptop" -f ~/.ssh/id_ed25519_githubTell 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:
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
CONFCheck it worked
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.pubThe 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:
cat ~/.ssh/id_ed25519_github.pubThe 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:
ssh -T git@github.comThe 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:
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:
cd ~
rm -rf ~/git-demo ~/git-demo-workNext steps
- Make your first commits and read the history: make your first Git commits.
- Work on changes in parallel: create, switch and merge branches in Git.
- When you are ready to share your work: push to GitHub over SSH and open a pull request.
- Never commit passwords or tokens: keep secrets out of Git.
- Log in to servers with the same kind of key: log in to a Linux server with SSH keys.
Related tutorials
- How to Undo Mistakes in Git: restore, revert, reset and reflogYou will discard edits, unstage files, fix the last commit, undo a commit safely with revert, move a branch back with reset, and bring back commits you thought you had lost with the reflog.
- How to Resolve a Git Merge ConflictYou will cause a merge conflict on purpose, read the conflict markers, resolve it by editing the file, finish the merge, back out of a merge you do not want, and pick one side of a conflict with a single command.
- How to Push to GitHub over SSH and Open a Pull RequestYou will connect a local repository to a new GitHub repository over SSH, push your work, push a branch, open a pull request from the website and from the GitHub CLI, merge it, and bring the result back to your computer.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials