All tutorials

Tutorial 2 of 8

Your First Commit

Create a repository, stage a file and record your first commit, knowing exactly what each step does.

About 5 minutes

What you will be able to do

  1. Create a repository with git init and set your name and email once.
  2. Explain the three places: the working folder, the staging area and the commit history.
  3. Use git add and git commit -m, and check your work with git status and git log.
  4. Know that git add stages a file as it is at that moment, so later edits need another git add.

Title

Your First Commit. Three commands: init, add, commit.

In this tutorial you will create a repository and save your first commit using three commands.

Tell Git who you are

Once per computer, tell Git your name and email. Every commit you make will carry them.

git config --global user.name "Asha Rao"
git config --global user.email "asha@example.com"

Git stamps your name and email on each commit, so set them once, ideally with the same email you use on GitHub.

git init

git init turns a normal folder into a repository by adding a hidden .git folder.

mkdir my-notes
cd my-notes
git init -b main

Running git init creates the hidden .git folder where Git will keep every commit, and -b main names the first branch.

Three places

Git has three places: your working folder, the staging area, and the commit history.

You edit files in the working folder, choose changes with git add, and save them for good with git commit.

Analogy: the photo studio

Think of a photo studio. Arrange people on the stage, then take the photo.

Staging is like arranging people on a stage, and committing is like taking the photo of whatever is on that stage.

The analogy, and where it breaks

Backstage is your working folder, the stage is the staging area, and the photo is the commit. You pick who is on stage before you click.

A real photo shows people as they look when you click, but Git's stage holds each file as it was when you ran git add, so later edits need another add.

git status and git add

git status shows what changed. git add puts a file on the stage.

echo "Buy milk" > todo.txt
git status
git add todo.txt
git status

git status is your safe, read-only check, and git add copies the current file into the staging area.

git commit -m

git commit -m saves everything on the stage as a new commit, with your message.

git commit -m "Add todo list"

The commit command saves the staged snapshot with your message, and main now points at that first commit.

The edit-after-add trap

Edited a file after git add? The stage still holds the older version. Run git add again.

git add copies a file at that exact moment, so any edit made afterwards needs another git add before you commit.

The analogy, and where it breaks

Back to the photo studio: the stage keeps a copy of each person as they were when they stepped up.

On a real stage, people who change clothes appear in the photo that way, but Git keeps the staged copy until you run git add again.

git log

git log lists your commits, newest first, with ID, author, date and message.

git log --oneline

git log reads your history from newest to oldest, and --oneline shows one short line per commit.

Good commit messages

Say what the commit does, in a few words: "Add login form", not "changes" or "final".

A short message that starts with a verb helps you and your teammates find changes later.

Recap

init creates the repo. add stages a copy of a file. commit saves the stage. status and log help you check.

echo "v1" > a.txt
git add a.txt
echo "v2" > a.txt
git commit -m "Save a"

You now know the basic save cycle, and next you will learn how branches let you try ideas safely.

Check yourself

0 of 4 answered

Check 1 of 4

Put the needed commands in order to save todo.txt as the first commit of a new repo. One card is not needed.

Tap the commands in the order you would run them. One is not needed.

Check 2 of 4

After these commands, what does the new commit hold for a.txt?

echo "v1" > a.txt
git add a.txt
echo "v2" > a.txt
git commit -m "Save a"

Check 3 of 4

You created notes.txt and ran git commit -m "Add notes", but Git says nothing was added to commit. What is the most likely reason?

Check 4 of 4

What is the staging area for?

mkdir -p ~/git-practice/t02-first-commit
cd ~/git-practice/t02-first-commit
git init -b main

echo "Buy milk" > todo.txt
git status                     # todo.txt is untracked
git add todo.txt
echo "Buy eggs" >> todo.txt
git status                     # todo.txt is listed twice: staged and not staged
git commit -m "Add todo list"
git status                     # the eggs line is still not committed

git add todo.txt
git commit -m "Add eggs to todo list"
git log --oneline              # press q if a pager opens