TExcelTAD
Home › Git › Git Get Started

Git Get Started

Before your first commit you need two things: Git installed, and Git told who you are. This lesson covers both, then creates a first repository.

Installing Git

System How to install
Windows Download "Git for Windows" from git-scm.com (includes Git Bash, a Bash terminal)
macOS Run git --version in Terminal; macOS offers to install the Command Line Tools. Or brew install git
Debian/Ubuntu sudo apt install git
Fedora sudo dnf install git

Check that it works:

bash
git --version

Configuring your identity

Every commit records an author name and email. Set them once per computer with --global:

bash
git config --global user.name "Ada Lovelace"
git config --global user.email "ada@example.com"

Note: Use the same email as your GitHub account if you want commits linked to your profile. The email is public in the history of public repositories; GitHub offers a private noreply address for this.

You can read any setting back by leaving out the value:

bash · try it yourself
git config user.name
git config user.email
git config init.defaultBranch
Ada Lovelace
ada@example.com
main

The three configuration levels

Git reads settings from three files. A more specific level overrides a more general one:

Level Flag File Applies to
System --system /etc/gitconfig every user on the machine
Global --global ~/.gitconfig all your repositories
Local --local (default inside a repo) .git/config this repository only

A common use of the local level is a different email for work projects:

bash · try it yourself
mkdir work-project && cd work-project
git init -q
git config user.email "ada@company.example"
echo "global: $(git config --global user.email)"
echo "local:  $(git config --local user.email)"
echo "used:   $(git config user.email)"
git config --list --show-origin | grep user.email
global: ada@example.com
local:  ada@company.example
used:   ada@company.example
file:/home/learner/.gitconfig	user.email=ada@example.com
file:.git/config	user.email=ada@company.example

--show-origin tells you which file each value came from, which is the fastest way to debug "why does Git use the wrong email?".

Other useful settings

Setting Example Purpose
init.defaultBranch main name of the first branch in new repositories
core.editor "code --wait", nano, vim editor for commit messages
core.autocrlf true (Windows), input (macOS/Linux) line-ending conversion
pull.rebase false or true what git pull does when branches diverged
alias.st status -s your own short commands

Aliases save typing. After git config --global alias.lg "log --oneline --graph", git lg runs the longer command:

bash · try it yourself
git config --global alias.st "status -s"
git config --global alias.lg "log --oneline --graph"
mkdir demo && cd demo && git init -q
echo "hello" > hello.txt
git st
git add hello.txt && git commit -q -m "Say hello"
git lg
?? hello.txt
* c08bfe8 Say hello

Creating a repository

git init turns the current folder into a repository by creating a hidden .git folder. Everything Git knows about the project lives there; the files you edit stay as normal files around it.

bash · try it yourself
mkdir website && cd website
git init
ls -A
ls .git
git status
Initialized empty Git repository in /home/learner/website/.git/
.git
HEAD
config
description
hooks
info
objects
refs
On branch main

No commits yet

nothing to commit (create/copy files and use "git add" to track)

Warning: Deleting the .git folder deletes the entire history. The files in the folder survive, but Git forgets everything about them.

Test yourself

Which command sets your name for all repositories on this computer?

  1. git config user.name "Ada"
  2. git config --global user.name "Ada"
  3. git init --name "Ada"
  4. git user "Ada"

A setting exists at both the global and local level. Which value does Git use inside that repository?

  1. The global one
  2. The local one
  3. Whichever was set first
  4. Git reports an error

What does git init create?

  1. A GitHub repository
  2. A hidden .git folder holding the repository
  3. A first commit
  4. A README file