BeginnerLesson 3 of 306 min read

Your First Repository and First Commit

Turn a folder into a repository, save your first commit, and open a commit to see what Git really stored.

In this lesson you will

  • Create a repository with git init
  • Read git status at each step
  • Explain the working folder, staging area and repository
  • Describe what a commit contains

About 28 minutes

Putting the plan under watch

Nia has a folder called garden with one file in it, her planting plan. Today she turns that folder into a repository and saves its first commit. The commands are few, and the important skill is reading what Git tells you after each one. The session below is a real run of every step.

$ mkdir garden
$ cd garden
$ git init
Initialized empty Git repository in /home/nia/garden/.git/
$ git status
On branch main

No commits yet

nothing to commit (create/copy files and use "git add" to track)
$ echo "Beans: row 1" > plan.txt
$ git status
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	plan.txt

nothing added to commit but untracked files present (use "git add" to track)
$ git add plan.txt
$ git status
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   plan.txt
$ git commit -m "Add planting plan"
[main (root-commit) cb0a6d9] Add planting plan
 1 file changed, 1 insertion(+)
 create mode 100644 plan.txt
$ git status
On branch main
nothing to commit, working tree clean
$ git log --oneline
cb0a6d9 Add planting plan
$ ls -a
.
..
.git
plan.txt
$ cat .git/HEAD
ref: refs/heads/main

What each command did

git init turned the folder into a repository by creating a hidden folder named .git inside it. You can see it in the final listing. That hidden folder is the repository: it holds every snapshot and every setting. The files you edit live outside it, in what Git calls the working folder. If you ever delete .git, the history is gone and the folder is just a folder again, so leave it alone.

git status is your compass, and you will run it constantly. Read the three versions in the session. At first it says there are no commits yet. After the file is created, it lists plan.txt as untracked, which means Git can see the file but is not recording it. After git add, the same file appears under “Changes to be committed”. After the commit, it says the working tree is clean: nothing has changed since the last snapshot.

Three places a change can live

This is the idea that confuses most beginners, so take it slowly. A change moves through three places. First it sits in the working folder, where you edit files. Then git add copies it into the staging area, a waiting room for the next snapshot. Then git commit takes everything in the waiting room and saves it as a permanent commit in the repository. The extra step exists so that you choose exactly what goes into each snapshot. Lesson 4 shows why that is so useful.

Place What happens there
Working folder You edit your files
Staging area Changes chosen by git add
Repository Commits saved by git commit

Looking inside a commit

A commit is not magic. It is a small record, and Git lets you open it. These three commands ask what the newest commit is made of. The second uses a format option to print only the main fields, with each long code shortened to seven characters.

$ git cat-file -t HEAD
commit
$ git log -1 --format="tree: %t%nauthor: %an%nmessage: %s%nparents: %p"
tree: b68b41d
author: Nia Okafor
message: Add planting plan
parents: 
$ git ls-tree --abbrev HEAD
100644 blob d0f2908	plan.txt

The first says the object is a commit. The second prints its main fields: a pointer to a tree (the list of files at that moment), the author and the message. The last field is the parents, and it is empty here because this is the very first commit. Every later commit names the commit before it, and that chain of parents is the history. The third command lists the tree: one file, and next to it a short code. That code is a fingerprint of the file’s contents. If the contents change by even one letter, the fingerprint changes completely.

Checking the fingerprint ourselves

Git is not doing anything secret here. We can build the same fingerprint in a few lines of Python. Git hashes the word blob, a space, the length of the file, a zero byte, and then the file’s contents. The program below does exactly that, and also asks Git for its own answer.

import hashlib, subprocess
text = b"Beans: row 1\n"
mine = hashlib.sha1(b"blob %d\0" % len(text) + text).hexdigest()
git = subprocess.run(["git", "hash-object", "--stdin"], input=text, capture_output=True).stdout.decode().strip()
print("my fingerprint:", mine[:12])
print("git fingerprint:", git[:12])
print("same:", mine == git)
my fingerprint: d0f2908d3c47
git fingerprint: d0f2908d3c47
same: True

The two fingerprints match, and they start with the same characters you saw in the listing above, d0f2908. Two people who save the same text get the same fingerprint, and a changed file always gets a different one. That is how Git notices changes, and how it detects damage.

Common mistake: starting a repository in the wrong place

Running git init in the wrong folder, especially your home folder or your Documents folder. Git would then watch everything inside it. Always check where you are with pwd before you run it.

Your IDs will differ

The commit IDs in this lesson came from the computer that built it. Yours will differ, because the ID is built from the content, the author and the time. The sample file is made up, and the Python program is a teaching sketch of Git’s method, not a replacement for Git.

Make a folder, put a short text file in it, and run init, status, add, commit and status once more. Say aloud, after each command, which of the three places the file is in.

Practice for this lesson

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

What does git init create?

  1. A new text file
  2. A hidden .git folder that holds the history
  3. A copy on GitHub
  4. A password

The final listing showed the hidden .git folder next to plan.txt.

What does untracked mean in git status?

  1. The file is deleted
  2. The file is already committed
  3. The file is hidden
  4. Git can see the file but is not recording it

plan.txt was untracked until it was added.

What does git add do?

  1. Saves a permanent snapshot
  2. Copies a change into the staging area for the next commit
  3. Uploads to a website
  4. Deletes a file

The status changed to Changes to be committed after the add.

What does a clean working tree mean?

  1. The folder is empty
  2. Git is broken
  3. The commit failed
  4. Nothing has changed since the last commit

After the commit, status reported that the working tree was clean.

Why does changing one letter of a file give a new fingerprint?

  1. The fingerprint is calculated from the exact contents
  2. Git picks random codes
  3. The date changes it
  4. The author changes it

Python and Git agreed because both hashed the same contents.

Summary

  • git init creates the hidden .git folder, which is the repository.
  • git status shows where each change is.
  • Changes move from the working folder to the staging area to the repository.
  • Each file version and commit has a fingerprint built from its contents.

Next lesson

Lesson 4 looks at the staging area and how to write good commits.

Questions

Is it safe to delete the .git folder?

It deletes the whole history, leaving only the current files. Do it only if you truly want the folder to stop being a repository.

Why are there three places instead of two?

The staging area lets you choose exactly what goes into each commit, so one snapshot can hold one clear idea even when you changed several files.

Is the commit ID a secret?

No. It is just a fingerprint that names the commit. It is safe to share, and you will often paste IDs when talking about a change.

Progress is saved in this browser.