Computing and the Command Line

Installing and Configuring Git

Install git under WSL or on Linux with apt and check the version. Set your name and email, which go into every commit; make new repositories start on a branch called main; choose your editor; and set line-ending handling so files from Windows don't cause trouble. Where settings live (system, global, local), how to see them all, and a few aliases worth having.

  • 4 min
  • 7 steps
  • 2 questions
  • Lesson 26 of 80

In this lesson

  1. Installing git
  2. Who you are
  3. Three more settings
  4. Where settings live
  5. Aliases
  6. Your turn
  7. So

Installing git

Ubuntu, under WSL or on a Linux desktop, may already have git; if not, install it from the package manager:

me@linuxbox:~$ sudo apt update && sudo apt install git
me@linuxbox:~$ git --version
git version 2.43.0

Your version number depends on your distribution’s release; anything from the last few years teaches the same commands. (Git for Windows is a separate installation of the same program for Windows itself; under WSL, use the Linux one.)

Who you are

The first thing to do after installing git is set your name and email. Every commit you make records them as its author, permanently 1:

me@linuxbox:~$ git config --global user.name "Your Name"
me@linuxbox:~$ git config --global user.email you@example.com

--global means “for every repository I use on this machine,” so you do it once 1. Use the same email you’ll use on GitHub (module 4), so your commits link to your account. GitHub also offers a private “noreply” address if you’d rather not publish your real one.

Three stacked boxes for git's setting levels. System, /etc/gitconfig, every user on this machine, set with git config --system. Global, ~/.gitconfig, you in every repository, set with git config --global. Local, repo/.git/config, this one repository, set with git config inside the repo. An arrow shows each level overrides the one above it. Right, the commands to run once on every new machine: git config --global user.name "Your Name" (shown on every commit), user.email (the same email as GitHub), init.defaultBranch main (new repositories start on main), core.editor nano (for commit messages), and git config --list --show-origin to see every setting and its file.
Set your identity once, globally; override per repository only when you need to. Credit: StudyCorner diagram · CC BY 4.0 · Source

Quick check

Why must you set user.name and user.email before your first commit?

Three more settings

Default branch name. A new repository starts with one branch. Git’s built-in default name is still master, and the documentation says it will change to main in Git 3.0; you choose with init.defaultBranch 2. GitHub and most projects use main:

me@linuxbox:~$ git config --global init.defaultBranch main

Editor. When git needs a longer message from you, it opens an editor 1. Pick the one you’re comfortable with:

me@linuxbox:~$ git config --global core.editor nano

(If you set EDITOR in your ~/.profile in the shell course, git uses that already.)

Line endings. Windows ends lines with two characters (carriage return and line feed, CRLF); Linux uses just a line feed (LF). Mixed endings make files look completely changed and break scripts. Pro Git’s advice is to set core.autocrlf to input on Linux, which turns any stray CRLF into LF when you commit and never adds CRLF; on Windows itself, the setting is true 1:

me@linuxbox:~$ git config --global core.autocrlf input

Where settings live

Git reads settings from three levels, each overriding the one before 1:

  • System, /etc/gitconfig: for every user on the machine (git config --system).
  • Global, ~/.gitconfig: for you, in all your repositories (git config --global).
  • Local, .git/config inside one repository: just that repository (plain git config, run inside it).

To see everything and which file each setting came from 1 3:

me@linuxbox:~$ git config --list --show-origin
file:/home/me/.gitconfig    user.name=Your Name
file:/home/me/.gitconfig    user.email=you@example.com
file:/home/me/.gitconfig    init.defaultbranch=main
...
me@linuxbox:~$ git config user.email          # just one setting

Your global settings are just a small text file; cat ~/.gitconfig shows it. Module 7 puts it in version control along with your other dotfiles.

Quick check

You set user.email globally, but for one work repository you need a different address. What do you do?

Aliases

Aliases are shortcuts for longer git commands 1:

me@linuxbox:~$ git config --global alias.st status
me@linuxbox:~$ git config --global alias.lg "log --oneline --graph --all"

Now git st is git status, and git lg draws your history as a compact graph, which you’ll use a lot once there are branches.

Your turn

Exercises

  1. Install git in your WSL Ubuntu and check its version.
  2. Set your name, email, default branch, editor, and core.autocrlf input.
  3. Run git config --list --show-origin and find each setting you made. Then cat ~/.gitconfig.
  4. Add the lg alias. (You’ll try it in the next lesson.)
  5. Run git help config | head -40, or man git-config. Every git command has its own man page.
Answers
  1. The five git config --global lines above.
  2. All five come from file:/home/<you>/.gitconfig, and the file looks like this, with each setting in its section:

    [user]
        name = Your Name
        email = you@example.com
    [init]
        defaultBranch = main
    [core]
        editor = nano
        autocrlf = input
    
  3. git help <command> opens the same pages as man git-<command>.

So

Install git with apt, then set, once, with --global: user.name and user.email (on every commit), init.defaultBranch main, core.editor, and core.autocrlf input. Settings live at system, global, and local levels, each overriding the last, and git config --list --show-origin shows them all.

Lesson complete

Nice work.

1day streak
0/1today's goal
–correct

Up next · 5 min

Your First Repository

Next lesson
Sources for this lesson
  1. 1
    Scott Chacon, Ben Straub. Pro Git, 2nd edition. Apress; free online at git-scm.com. 2014. verifiedFree CC BY-NC-SA 3.0 book, maintained online. Ch. 1: version control; Git's 2005 origin when the Linux kernel lost free use of BitKeeper; snapshots, not differences (unchanged files stored once); nearly every operation local; integrity through 40-character SHA-1 checksums; the three states (modified, staged, committed) and three areas (working tree, staging area or index, .git directory); first-time setup with system/global/local config levels, user.name and user.email baked into commits, core.editor, git config --list --show-origin. Ch. 2: git init, status (and -s), add, diff and diff --staged, commit (-m, -a), .gitignore, log options, amending, undoing, remotes, tags, aliases. Ch. 3: branches as movable pointers, HEAD, merging and conflicts, remote branches, rebasing and its rule. Ch. 7: reset demystified, stashing, revision selection. Ch. 8: core.autocrlf true on Windows, input on Linux and macOS. Ch. 10: objects (blob, tree, commit) and references.
  2. 2
    git-init documentation. Git project (git-scm.com). verifiedgit init creates an empty repository, a .git directory with objects, refs/heads, refs/tags, and template files. The initial branch name falls back to master, but this will change to main when Git 3.0 is released; init.defaultBranch customizes it, and --initial-branch sets it for one repository.
  3. 3
    git-config documentation. Git project (git-scm.com). verifiedReference for reading and writing git settings at the --system, --global, --local, and --worktree levels, with --list and --show-origin, and for every configuration variable, including user.name, user.email, init.defaultBranch, core.editor, core.autocrlf, and alias.*.