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:
git --versionConfiguring your identity
Every commit records an author name and email. Set them once per computer with --global:
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
noreplyaddress for this.
You can read any setting back by leaving out the value:
git config user.name
git config user.email
git config init.defaultBranchAda 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:
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.emailglobal: 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:
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.
mkdir website && cd website
git init
ls -A
ls .git
git statusInitialized 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
.gitfolder 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?
- git config user.name "Ada"
- git config --global user.name "Ada"
- git init --name "Ada"
- git user "Ada"
A setting exists at both the global and local level. Which value does Git use inside that repository?
- The global one
- The local one
- Whichever was set first
- Git reports an error
What does git init create?
- A GitHub repository
- A hidden .git folder holding the repository
- A first commit
- A README file