Chapter 6 · Lesson 2 of 10
Git for deployers

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.

By the end you will

Install Git, set your identity, default branch, editor and pull behavior, and read back every setting with its source file.

Time
10 min
Level
Beginner
Check
4 questions
You need
  • 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/config inside 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#

bash
sudo apt update
sudo apt install git

apt 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:

bash
git --version

The output is git version followed by the number your distribution ships.

Step 2: Set your identity#

bash
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#

bash
git config --global init.defaultBranch main

Older 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:

bash
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:

bash
# 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 only

For 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:

bash
git config --list --show-origin

Each 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:

bash
git config user.email

You 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 with git config --show-origin user.email.
  • sudo changes the user. sudo git config --global writes to root’s ~/.gitconfig, not yours. Do not use sudo for config or normal Git commands.
  • Config is not secret. ~/.gitconfig is 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.defaultBranch has no effect, your Git may be too old (before 2.28). Check git --version.
  • Changing identity is not retroactive. New settings only apply to future commits. Past commits keep their old author.

Troubleshooting#

SymptomLikely causeFix
E: Unable to locate package gitPackage lists are stale or the repository is not enabledRun sudo apt update, and check /etc/apt/sources.list
E: Could not get lock /var/lib/dpkg/lock-frontendAnother apt process is runningWait 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 setRun the two git config --global commands from Step 2
fatal: unable to auto-detect email addressSame cause on a host with no usable hostnameSet user.email explicitly
hint: Using 'master' as the name for the initial branchinit.defaultBranch is not setgit config --global init.defaultBranch main
fatal: Need to specify how to reconcile divergent branches.pull.rebase is unset and branches divergedSet pull.rebase or pull.ff as in Step 5
error: could not lock config file /home/deploy/.gitconfig: Permission deniedThe file is owned by root from an earlier sudo runsudo chown deploy:deploy ~/.gitconfig

Quick reference#

bash
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 setting

Next#

Make your first commit in your first Git repository.

Check what you learned4 questions
1Which command installs Git on Ubuntu?
2Where does git config --global user.name write its value?
3Which command shows every setting together with the file that defines it?
4What does git config --global init.defaultBranch main change?

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

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios