Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 78 ยท Getting Started

Installing and Configuring Git

Install Git on Windows, macOS and Linux, then configure your identity, default branch, editor and line endings. Learn system, global and local config levels with real output.

Before your first commit you need two things: Git installed, and Git told who you are. Every commit carries an author name and email, and Git will not guess them reliably. Beyond identity, a handful of settings (default branch, editor, line endings) save you from small annoyances for years.

This lesson covers installation on the three major systems (those commands are read-only, because our sandbox already has Git 2.56), then goes deep on git config, which is a small but powerful system with three levels. You will run every configuration example for real. Afterwards continue with Your First Repository.

Installing Git

Check first whether Git is already there:

CONSOLE
$ git --version
git version 2.56.0

If you see a version number, you are done. Any recent version works for this tutorial, but aim for 2.40 or newer so that commands such as git switch and git restore behave as described.

The commands in this section need sudo, a package manager or a graphical installer, so they cannot run in our sandbox. Run them on your own computer.

Windows. Download the installer from the official Git for Windows project (git-scm.com), or use a package manager:

CONSOLE
winget install --id Git.Git -e

The installer installs Git Bash (a terminal with Bash) and adds Git to your PATH so it also works in PowerShell and the Windows Terminal. The defaults are fine; the only choice worth thinking about is the default editor, which you can change later.

macOS. Typing git in the Terminal offers to install Apple's Command Line Tools, which include Git. For a newer version, use Homebrew:

CONSOLE
brew install git

Linux. Use your distribution's package manager:

CONSOLE
sudo apt install git        # Debian, Ubuntu
sudo dnf install git        # Fedora, RHEL
sudo pacman -S git          # Arch

Distribution packages can lag behind. If yours is old, look for a newer version in the distribution's backports or build from source.

The three levels of configuration

Git reads settings from up to three files, each more specific than the last. A more specific level overrides a less specific one.

Level Option File (typical) Applies to
System --system /etc/gitconfig Every user on the machine
Global (user) --global ~/.gitconfig or ~/.config/git/config All your repositories
Local --local (default when writing) .git/config in a repository That one repository
TEXT
   system  (all users)
      v   overridden by
   global  (you)
      v   overridden by
   local   (this repository)        <-- wins

Most settings belong at the global level: you set your name once. Use the local level for exceptions, such as a different email address for a work project. You rarely touch the system level.

In our sandbox $HOME is a temporary folder, so --global writes into it and disappears afterwards. Perfect for experiments. Set your identity and read it back:

Bash Set your name and email

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

git config <key> prints the value Git would use. The file it wrote is plain text in INI format: a [section] header and indented key = value lines. You can edit it by hand just as well as with commands. The dotted name user.name simply means section user, key name.

Identity: user.name and user.email

These two values are stamped into every commit you make. Git does not check that they are real, but they matter: hosting sites match the email to your account, and teammates read the name in git log.

If you commit without an identity, Git may refuse or guess from your computer's name (producing an ugly address). Let us see the refusal on purpose. We remove the environment's identity and ask Git to commit.

Bash Committing without an identity (fails on purpose)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

With user.useConfigOnly true Git refuses to invent an identity. It prints "Author identity unknown", shows the two git config commands to run, ends with fatal: no email was given and auto-detection is disabled and exits with code 128. That setting is useful if you have several identities and want Git to stop you from committing with the wrong default. Without it, Git tries a guess from your login and hostname and may only warn.

Default branch name

When you run git init, Git creates the first branch. For years that was master; modern setups use main. The setting is init.defaultBranch. Our sandbox already sets it to main, and you should set it on your own machine:

CONSOLE
git config --global init.defaultBranch main

You can also rename an existing branch later; see Branches. The name only matters by convention, but being consistent across repositories avoids confusion when your remote uses main.

Editor, pager and colour

Git sometimes opens an editor (for commit messages without -m, interactive rebase). It picks one in this order: the GIT_EDITOR environment variable, the core.editor setting, VISUAL, EDITOR, then a built-in default (vi). Set it once:

CONSOLE
git config --global core.editor "code --wait"     # VS Code
git config --global core.editor "nano"            # a simple terminal editor

The --wait matters for graphical editors: Git must block until you close the file. Our sandbox cannot run editors, so we always pass the message with -m.

Git paginates long output through less and colours it when writing to a terminal. Both are configurable (core.pager, color.ui); defaults are good. In scripts and in our sandbox output is plain text.

Line endings across systems

Windows ends lines with two characters (carriage return and line feed, CRLF); Linux and macOS use one (LF). Mixed endings produce confusing diffs where every line seems changed. Git can normalize them with core.autocrlf:

Value Meaning Use on
true Convert CRLF to LF when committing, LF to CRLF when checking out Windows
input Convert CRLF to LF when committing, never the other way macOS, Linux
false Do nothing When a .gitattributes file handles it

The modern recommendation for a team is a .gitattributes file in the repository, which applies to everyone regardless of personal settings (.gitattributes, Line Endings and Large Files). Until then, true on Windows and input elsewhere is a safe personal default.

Reading configuration

git config --list prints every setting Git sees. Adding --show-origin shows which file each came from, which is the best debugging tool when a setting does not behave as expected. The example below sets one value at each of two levels and shows how they combine.

Bash Global and local levels, with origins

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

You can see two user.email lines in the list (the later one wins) and the scope of the winner is local. The origin column shows file:.home/.gitconfig for the global entries and file:.git/config for the local one. This is exactly what happens when you set a work address in a work repository.

To remove a setting use git config --unset user.email (add --global for the global file). To edit by hand, git config --global --edit opens the file in your editor.

Different identities for different folders: includeIf

Setting the email repository by repository is easy to forget. A neater approach uses conditional includes: the global config says "for repositories under this folder, also read that file". Then every repository below ~/work/ automatically gets the work email.

INI
# ~/.gitconfig
[user]
    name = Ada Lovelace
    email = [email protected]

[includeIf "gitdir:~/work/"]
    path = ~/.gitconfig-work
INI
# ~/.gitconfig-work
[user]
    email = [email protected]

The trailing slash on gitdir: matters: it means "this folder and everything below it". Let us try it for real, with the work folder inside the sandbox.

Bash includeIf picks the identity by folder

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Both repositories use the same global file, but the one below work/ also reads the extra file, so its email differs. Besides gitdir: there is onbranch: and hasconfig:remote.*.url: (choose by remote URL, handy for "all repositories on my employer's server").

Useful settings to know

Here are settings worth adding to your global config over time. You do not need them today.

INI
[init]
    defaultBranch = main
[pull]
    rebase = false          # decide how `git pull` combines (see push-and-pull)
[fetch]
    prune = true            # forget remote branches deleted on the server
[rerere]
    enabled = true          # remember conflict resolutions (see merge-conflicts)
[diff]
    colorMoved = default    # highlight moved blocks of code
[core]
    autocrlf = input

Aliases and Everyday Productivity shows a larger set, including aliases (your own short names for commands).

Git Bash and the command line

On Windows, the official installer includes Git Bash: a terminal that gives you a Bash shell and the common Unix tools (ls, cat, grep, ssh) next to git. On macOS and Linux you already have such a shell in the Terminal app. Git Bash writes Windows drives as /c/Users/you instead of C:\Users\you, uses forward slashes, and starts in your home folder (~). You can also run Git from PowerShell, the Windows Terminal or the terminal built into your editor; the Git commands are identical.

A handful of shell commands cover nearly everything you need around Git:

Command What it does
pwd Print the current folder
ls, ls -la List files (-a shows hidden ones like .git, -l shows details)
cd folder, cd .., cd ~ Enter a folder, go up one level, go home
mkdir name, mkdir -p a/b/c Create a folder (-p creates parents and tolerates existing ones)
rmdir name Remove an empty folder
rm file, rm -r folder Delete a file, or a folder and everything in it (no trash, no undo)
cp, mv Copy and move or rename (inside a repository prefer git mv)
cat file, echo text > file Show a file, write text into a file (>> appends)
clear Clear the screen
Bash A whole Git session from the shell

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Notice that Git ignored the empty drafts folder: Git tracks files, not folders. Cloning (git clone <repository>), merging, undoing and the rest work exactly the same in Git Bash as in any other terminal, so the rest of this tutorial applies unchanged. Two Windows-specific tips: paste with Shift+Insert or a right click, and keep the line-ending setting from the section above in mind when a repository is shared with Linux users.

Configuring inside our sandbox

Each run of an example starts with an empty $HOME, so any --global setting lasts only for that run. That is a feature: you cannot break anything. A few notes for reading the examples:

  • The runner already sets init.defaultBranch=main, disables the pager and the editor.
  • Most examples use export GIT_AUTHOR_NAME=... variables instead of git config, because they are quick to read and also fix dates. Environment variables override configuration, as the next example proves.
Bash Environment variables beat config

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The author came from the environment variables, while the committer (for which we exported nothing) fell back to the configuration. A commit really has two identities: the author wrote the change, the committer recorded it. They differ when someone applies another person's patch.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which config level wins when the same key is set globally and locally?

    More specific levels override less specific ones: local beats global, and global beats system.
  2. 2. Which command sets your name for all repositories of your user?

    --global writes to your user-level file. Without a flag, git config writes to the repository's local config, and --system affects every user on the machine.
  3. 3. How can you see which file a particular setting comes from?

    --show-origin prefixes each entry with the file it was read from, which makes override problems easy to find.
  4. 4. What does [includeIf "gitdir:~/work/"] do?

    Conditional includes load another config file only when the repository's .git directory matches the pattern. It is the standard way to use a work email automatically.
  5. 5. What are the two identities stored in every commit?

    The author wrote the change; the committer recorded the commit. They are usually the same person but differ for applied patches or rewritten commits.

Summary

  • Install Git from git-scm.com (Windows), Apple's tools or Homebrew (macOS), or your package manager (Linux), then confirm with git --version.
  • Settings live at three levels: system, global (--global, your user) and local (the repository); the more specific level wins.
  • Set user.name, user.email and init.defaultBranch first; add an editor and core.autocrlf as needed.
  • git config --list --show-origin shows every setting and the file it comes from.
  • includeIf "gitdir:..." applies different settings (such as a work email) to repositories in a folder.
  • Environment variables such as GIT_AUTHOR_NAME override configuration, which our examples use to make output reproducible.

Next: Your First Repository