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

URL: https://levelrail.com/learn/git/install-and-configure-git/
Updated: 2026-11-27

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](https://levelrail.com/learn/linux/package-management-with-apt/)).

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](https://levelrail.com/learn/git/git-branches-and-merging/)) explains what each one does.

Run the commands as a normal user with [sudo](https://levelrail.com/learn/glossary/#sudo)) only for the install, not as the [root user](https://levelrail.com/learn/glossary/#root-user)). For a server setup that uses Git for deploys, see the blog post [Deploy an Express app with Docker on a VPS](https://levelrail.com/blog/deploy-express-app-docker-vps/)), and use [ssh keys](https://levelrail.com/learn/git/ssh-keys-and-deploy-keys-for-git/)) 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](https://levelrail.com/learn/git/ssh-keys-and-deploy-keys-for-git/)) 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

| 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

```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
```

## Related lessons

- [What is Git and why servers use it](https://levelrail.com/learn/git/what-is-git-and-why-servers-use-it/)): the idea behind the tool you just installed.
- [Your first Git repository](https://levelrail.com/learn/git/your-first-git-repository/)): use your new identity on a real commit.
- [Package management with apt](https://levelrail.com/learn/linux/package-management-with-apt/)): how apt installs and updates packages like Git.
- [Essential Linux commands for servers](https://levelrail.com/learn/server-basics/essential-linux-commands-for-servers/)): the shell basics assumed here.
- [Create a sudo user](https://levelrail.com/learn/server-basics/create-a-sudo-user/)): run Git as a normal user, not root.

## Next

Make your first commit in [your first Git repository](https://levelrail.com/learn/git/your-first-git-repository/)).

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

- [git-config manual](https://git-scm.com/docs/git-config)
- [Ubuntu manpages](https://manpages.ubuntu.com/)
