Skip to content
$ lynuxpro
← Git

LESSON 2 OF 21 · BEGINNER · 14 MIN

Installing and configuring Git

HOW IT WORKSWhere Git reads its settings

--system/etc/gitconfig--global~/.gitconfig--localrepo/.git/configgitmerges all threeEvery commitauthor name + email
1/4

Git reads settings from three levels: the whole machine, your user, and one repository.

Before your first commit, Git needs to know who you are, and a few settings make daily work much smoother. This lesson covers installing Git on each platform, the three configuration levels, the settings every engineer should set, and how to connect to GitLab or GitHub securely with SSH keys.

In simple words

  • git config: the command that sets your name, email and preferences. Git puts your name and email on every commit.
  • Configuration levels: settings for the whole machine (system), for you (global) or for one repo (local). The most specific wins.
  • SSH key: a key pair that lets you push to GitLab or GitHub safely without typing a password.

Installing Git

PlatformCommand
Ubuntu / Debiansudo apt update && sudo apt install -y git
RHEL / Fedora / Amazon Linuxsudo dnf install -y git
macOSxcode-select --install or brew install git
WindowsGit for Windows from git-scm.com (includes Git Bash)
Containersapk add git (Alpine), or use an image that includes it

Check the version. Anything from 2.40 onward has the modern commands used in this course (git switch, git restore):

git --version
SHELLbashutf-8 · 1L
git version 2.47.1
OUTPUTterminalutf-8 · 1L

Your identity

Every commit records an author name and email. Set them once for your user:

git config --global user.name "Leela Kumar"git config --global user.email "leela@acme.dev"git config user.name
SHELLbashutf-8 · 3L
Leela Kumar
OUTPUTterminalutf-8 · 1L

Use the same email as your GitLab or GitHub account, so commits are linked to your profile.

The three configuration levels

LevelFlagFileApplies to
System--system/etc/gitconfigevery user on the machine
Global--global~/.gitconfigyou, in every repository
Local--local (default).git/configone repository

More specific levels win. A common pattern is a personal email globally and a work email in work repositories:

cd ~/work/paymentsgit config user.email "leela@company.com"     # local: only this repository
SHELLbashutf-8 · 2L

Many people use conditional includes so this happens automatically for every repository under a folder:

# ~/.gitconfig[includeIf "gitdir:~/work/"]    path = ~/.gitconfig-work
OUTPUTterminalutf-8 · 3L

See all effective settings, and where each one comes from, with:

git config --list
SHELLbashutf-8 · 1L

Settings worth setting today

git config --global init.defaultBranch maingit config --global pull.rebase truegit config --global core.editor "nano"git config --global alias.st "status -s"git config --global alias.lg "log --oneline --graph --all"git config --list
SHELLbashutf-8 · 6L
  • init.defaultBranch main: new repositories start on main, matching GitLab and GitHub.
  • pull.rebase true: git pull rebases your local commits on top of the remote instead of creating noisy merge commits (explained in the rebase lesson).
  • core.editor: the editor Git opens for commit messages. Use nano, vim or code --wait for VS Code.
  • Aliases shorten frequent commands: git st, git lg.

Other useful ones: core.autocrlf input on macOS/Linux (or true on Windows) to avoid line-ending problems, fetch.prune true to remove deleted remote branches automatically, and rerere.enabled true to remember how you resolved conflicts.

Connecting to GitLab or GitHub with SSH

You can clone over HTTPS with a personal access token, but most engineers use SSH keys: no passwords to type and nothing to leak in shell history.

# 1. Create a key pair (ed25519 is modern and short)ssh-keygen -t ed25519 -C "leela@acme.dev"# press Enter to accept ~/.ssh/id_ed25519, and set a passphrase # 2. Load it into the agent so you type the passphrase once per sessioneval "$(ssh-agent -s)"ssh-add ~/.ssh/id_ed25519 # 3. Copy the PUBLIC key and paste it in GitLab: Preferences → SSH Keyscat ~/.ssh/id_ed25519.pub # 4. Testssh -T git@gitlab.com
SHELLbashutf-8 · 13L
Welcome to GitLab, @leela!
OUTPUTterminalutf-8 · 1L

Never share or commit the private key (id_ed25519 without .pub). For HTTPS, use a credential helper (git config --global credential.helper store stores tokens in plain text; prefer libsecret on Linux, osxkeychain on macOS or Git Credential Manager on Windows).

Getting help

Every command has a manual page and a short help:

git help commit        # full manualgit commit -h          # short summary of options
SHELLbashutf-8 · 2L

Real-world scenario: the anonymous commits

A new CI runner image did not have user.name and user.email set, so commits made by the release job were authored by root <root@runner-7f9c.local>. Audits could not tell automated commits from human ones, and GitLab could not link them to the bot account. The fix was two lines in the job:

git config user.name "release-bot"git config user.email "release-bot@acme.dev"
SHELLbashutf-8 · 2L

Setting identity explicitly in automation is a habit worth having.

Common mistakes

  • Not setting user.email, or using a different one from your Git hosting account, so commits do not show your avatar.
  • Using --global on shared machines like bastion hosts, so everyone's commits get your name. Use local config there.
  • Pasting the private key into GitLab instead of the .pub file.
  • Line-ending chaos between Windows and Linux; agree on core.autocrlf or a .gitattributes with * text=auto.

Practice

  1. Set your name and email globally and read them back with git config user.name and git config user.email.
  2. Set init.defaultBranch to main.
  3. Create an alias git lg for log --oneline --graph --all.
  4. Run git config --list and find each value you set.
  5. On your own machine, create an SSH key and add it to your GitLab or GitHub account.

Cheat sheet

git --versiongit config --global user.name "Name"git config --global user.email "you@example.com"git config --global init.defaultBranch maingit config --global pull.rebase truegit config --global core.editor nanogit config --global alias.lg "log --oneline --graph --all"git config --list [--show-origin]git config user.email work@company.com     (this repo only)ssh-keygen -t ed25519 -C "you@example.com"ssh -T git@gitlab.com
OUTPUTterminalutf-8 · 11L

Key takeaways

  • Install Git 2.40+; check with git --version.
  • Set user.name and user.email before your first commit; they are stored in every commit.
  • Configuration has system, global and local levels; the most specific wins.
  • A few defaults (init.defaultBranch, pull.rebase, aliases) make daily work smoother.
  • Use SSH keys or a credential manager to authenticate with GitLab and GitHub.