Install and configure Git on Ubuntu or Debian
Install Git with sudo apt install git, then run git config --global to set your name, email, default branch and editor so every commit is labelled correctly.
- Updated Nov 27, 2026
- Markdown
Install Git, set your identity, default branch, editor and pull behavior, and read back every setting with its source file.
- An Ubuntu or Debian machine with sudo access
On Ubuntu or Debian, install Git with sudo apt update && sudo apt install git, check it with git --version, then tell Git who you are with git config --global user.name and user.email. A few more settings (default branch, editor, pull behavior) save you from surprises later. Ten minutes is enough, and you only do it once per machine and user.
How it works#
Git reads settings from three layers, and the closest one wins:
- System:
/etc/gitconfig, for every user on the machine. Written with--system. - Global:
~/.gitconfig, for your user. Written with--global. - Local:
.git/configinside one repository. Written with no flag.
When you set the same key in two places, local overrides global, and global overrides system. That is useful: you can use a personal email by default and a work email in one repository.
The name and email matter because each commit records them. Git will not refuse to commit without them, but it may guess from your username and hostname, and you end up with commits authored by ubuntu@vps-1234.localdomain.
Step 1: Install Git#
sudo apt update
sudo apt install gitapt update refreshes the package list, and apt install git installs the version from your distribution. It is usually a few releases behind the newest Git, which is fine for deploys. If you want to know more about how apt works, see the package management lesson.
Check it:
git --versionThe output is git version followed by the number your distribution ships.
Step 2: Set your identity#
git config --global user.name "Your Name"
git config --global user.email "you@example.com"Use your real name and an email you are happy to be public if you push to a public host. Hosts such as GitHub offer a private noreply address for this. On a deploy server that only pulls, identity rarely matters, but set it anyway in case someone makes an emergency commit there.
Step 3: Set the default branch name#
git config --global init.defaultBranch mainOlder Git versions named the first branch master. This setting makes git init create main instead, which matches what most hosts use now. It only affects repositories you create after setting it.
Step 4: Choose an editor#
When you run git commit with no -m, Git opens an editor. Pick one you can exit:
git config --global core.editor "nano"If you skip this, Git uses your $VISUAL or $EDITOR variable, and if neither is set it often falls back to vim. If you land in vim by accident, press Esc, type :q! and press Enter to leave without saving.
Step 5: Decide how git pull behaves#
A plain git pull is a git fetch followed by either a merge or a rebase. Newer Git versions warn you until you choose. The three common choices:
# Merge when branches diverged (the classic default)
git config --global pull.rebase false
# Replay your local commits on top of the remote ones
git config --global pull.rebase true
# Only accept fast-forward updates, otherwise stop and tell you
git config --global pull.ff onlyFor a server that only deploys, pull.ff only is a good safety net: if the history has diverged, the pull fails and you notice, instead of Git creating a surprise merge commit on a production machine. For personal work, the choice between merge and rebase is a taste decision. The branches and merging lesson explains what each one does.
Run the commands as a normal user with sudo only for the install, not as the root user. For a server setup that uses Git for deploys, see the blog post Deploy an Express app with Docker on a VPS, and use ssh keys once you connect to a remote.
Check it worked#
List every setting with the file it came from:
git config --list --show-originEach line starts with file: and a path, then the key and value, for example file:/home/deploy/.gitconfig user.name=Your Name. Read a single value with:
git config user.emailYou can also open ~/.gitconfig in an editor. It is a plain INI-style text file with sections such as [user] and [init].
Edge cases and gotchas#
- Quotes matter. A name with a space needs quotes. Without them Git sets only the first word and may complain about extra arguments.
- Local overrides global. If a repository has its own
user.email, your global value is ignored there. Check withgit config --show-origin user.email. - sudo changes the user.
sudo git config --globalwrites to root’s~/.gitconfig, not yours. Do not use sudo for config or normal Git commands. - Config is not secret.
~/.gitconfigis readable by you. Do not put tokens or passwords in it, and use SSH keys for authentication instead. - Older Git ignores new keys. If
init.defaultBranchhas no effect, your Git may be too old (before 2.28). Checkgit --version. - Changing identity is not retroactive. New settings only apply to future commits. Past commits keep their old author.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
E: Unable to locate package git | Package lists are stale or the repository is not enabled | Run sudo apt update, and check /etc/apt/sources.list |
E: Could not get lock /var/lib/dpkg/lock-frontend | Another apt process is running | Wait for it, or check with ps aux and retry |
Author identity unknown *** Please tell me who you are. | user.name and user.email are not set | Run the two git config --global commands from Step 2 |
fatal: unable to auto-detect email address | Same cause on a host with no usable hostname | Set user.email explicitly |
hint: Using 'master' as the name for the initial branch | init.defaultBranch is not set | git config --global init.defaultBranch main |
fatal: Need to specify how to reconcile divergent branches. | pull.rebase is unset and branches diverged | Set pull.rebase or pull.ff as in Step 5 |
error: could not lock config file /home/deploy/.gitconfig: Permission denied | The file is owned by root from an earlier sudo run | sudo chown deploy:deploy ~/.gitconfig |
Quick reference#
sudo apt update && sudo apt install git
git --version
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git config --global core.editor "nano"
git config --global pull.ff only
git config --list --show-origin
git config --global --unset user.email # remove a settingRelated lessons#
- What is Git and why servers use it: the idea behind the tool you just installed.
- Your first Git repository: use your new identity on a real commit.
- Package management with apt: how apt installs and updates packages like Git.
- Essential Linux commands for servers: the shell basics assumed here.
- Create a sudo user: run Git as a normal user, not root.
Next#
Make your first commit in your first Git repository.
Check what you learned
Frequently asked questions
How do I install Git on Ubuntu?
Run sudo apt update, then sudo apt install git. Confirm it worked with git --version.
How do I set my name and email in Git?
Run git config --global user.name "Your Name" and git config --global user.email "you@example.com". Every commit you make afterwards carries those values.
Where does git config --global store settings?
In the file ~/.gitconfig in your home directory. Repository settings (without --global) go in .git/config inside the repository.
Should I set pull.rebase to true?
It is a team preference. With false, git pull creates a merge commit when branches diverge. With true it replays your commits on top. Setting pull.ff only makes Git refuse anything that is not a fast-forward.
Sources
Keep going: guides and tutorials
- Install a Self-Hosted PaaS on Your Own Server in Five Minutes One installer download, one setup token, one sample app. The full Levelrail install on a fresh Linux server, with the checks and the fixes.
- Deploy a Docker Compose App Self-Hosted: Web, Redis and Rollback Deploy a Docker Compose app on your own server with Levelrail: a web service and Redis, a bad release that does not take traffic, and a compose rollback.