Himanshu SharmaSoftware engineer
Let’s talk

Git and GitHub · Lesson 4 of 30

Your first repository: init, add and commit

Create a Git repository from scratch, stage and commit files, check status and diffs as you go, and learn what a commit actually stores.

14 minYour first commitsUpdated 14 November 2026

Awaiting technical review. This lesson is readable but not yet listed in search engines.

By the end of this lessonCreate a repository, make several commits with git init, add and commit, and use git status and git diff to check your work before each one.

What you can do after this lesson

You will be able to start a repository from nothing, make commits with confidence, and check each step before you take it. Type along in a scratch folder. We use a fictional bakery menu so the examples stay small. Paths in the output are generic, and hashes will differ on your machine.

Creating a repository

Make a folder and turn it into a repository:

bash
mkdir bakery
cd bakery
git status
text
fatal: not a git repository (or any of the parent directories): .git

That error is useful: outside a repository, most Git commands refuse to run. Now initialise:

bash
git init
text
Initialized empty Git repository in /home/you/bakery/.git/

Because you set init.defaultBranch in lesson 2, the first branch is main. If your Git prints a hint about branch names, that is the default being used. Run git status:

text
On branch main

No commits yet

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

The core loop

Almost all daily Git is one loop: edit, inspect, stage, inspect, commit.

Create a file:

bash
echo "Sourdough, rye and seeded loaf." > menu.txt
git status -s
text
?? menu.txt

Stage it and commit:

bash
git add menu.txt
git commit -m "Add the bakery menu"
text
[main (root-commit) 5bf5647] Add the bakery menu
 1 file changed, 1 insertion(+)
 create mode 100644 menu.txt

root-commit marks the very first commit, which has no parent. The number in brackets is the start of the commit's hash; yours will not match.

Checking before committing

Change the file and look at the difference:

bash
echo "Add a cinnamon bun." >> menu.txt
git diff
text
diff --git a/menu.txt b/menu.txt
index 6b85d7c..b645e4a 100644
--- a/menu.txt
+++ b/menu.txt
@@ -1 +1,2 @@
 Sourdough, rye and seeded loaf.
+Add a cinnamon bun.

Lines beginning with + were added; - means removed; unmarked lines are context. The @@ line says where in the file the change sits. After staging, plain git diff prints nothing, because the working tree and staging area now agree. To see the staged change use:

bash
git add menu.txt
git diff --staged

The output is the same diff as above, now describing what will be committed. Make git diff --staged a habit just before git commit, and the commit never surprises you. Then commit with a message that says what and why:

bash
git commit -m "Add cinnamon bun to the menu"
text
[main 6187226] Add cinnamon bun to the menu
 1 file changed, 1 insertion(+)

Messages and the editor

Using -m supplies a one-line message. Running git commit with no -m opens the editor you configured, where you can write a longer message; save and close to finish. Lesson 19 covers writing good messages.

What a commit really contains

Every commit stores four things: a pointer to a snapshot of your project (called a tree), a pointer to its parent commit, the author and committer with timestamps, and the message. You can inspect one directly:

bash
git cat-file -p HEAD
text
tree b50c5942777ad89a393c6e6fd74e140c307fdae7
parent 5bf5647649f8dacc63c66e14d061be23115fec9f
author Asha Rao <asha@example.com> 1794304800 +0000
committer Asha Rao <asha@example.com> 1794304800 +0000

Add cinnamon bun to the menu

HEAD means "the commit I am on now". The long hexadecimal number is the commit's identity, calculated from its content. That is why hashes differ on your machine: your timestamps and names differ. The timestamp here is a Unix time in seconds.

Removing and renaming files

Deleting or moving a tracked file in your file manager works, but Git then sees a deletion plus a new untracked file. These commands do it for you and stage the result:

bash
git mv menu.txt menu.md
git status -s
text
R  menu.txt -> menu.md

git rm menu.txt deletes a file and stages the deletion. git rm --cached menu.txt stops tracking the file but leaves it on disk, which matters for the .gitignore lesson. Git does not store renames explicitly; it works out from similar content that the file moved.

Small commits

A good first habit is committing each time you finish one small, understandable thing. A commit that changes one idea is easy to review, easy to undo and easy to name. A commit titled "stuff" that touches twenty files is none of those.

A habit worth building early

Before every commit, ask three questions: what changed, does it belong together, and could I explain it in one sentence? Run git status to see the list of files, git diff --staged to read the exact lines, and only then commit. It takes ten seconds and prevents most of the small embarrassments, such as a stray debugging line or a file you never meant to include. Over a week these checks become automatic, and your history becomes something other people can actually read.

Common mistakes

  • Running git init inside a folder that already sits in another repository. You get a repository inside a repository, which causes confusion. Check with git status first.
  • Forgetting git add and wondering why the commit is empty. Commit only records staged content.
  • Committing with no message check. A message like "update" tells future you nothing.
  • Using git add . without reading status. You may stage files that should not be there.
  • Running git init in your home folder. Git will then see every file you own as untracked.

Exercise

  1. Create a folder called practice, run git init, and add a file notes.txt containing one line.
  2. Commit it. Then add a second line and look at git diff.
  3. Stage it, look at git diff --staged, and commit with a clear message.
  4. Rename the file to notes.md with git mv and commit again. Check git status at each step.
Show one possible answer
bash
mkdir practice && cd practice && git init
echo "First note" > notes.txt
git add notes.txt && git commit -m "Add first note"
echo "Second note" >> notes.txt
git diff
git add notes.txt
git diff --staged
git commit -m "Add second note"
git mv notes.txt notes.md
git commit -m "Rename notes to Markdown"
git status

The final status says nothing to commit, working tree clean.

Next: Reading project history with git log, diff and show.

Key points

  • git init turns a folder into a repository by creating the hidden .git directory.
  • The loop is edit, git status, git add, git diff --staged, git commit.
  • git diff shows unstaged changes; git diff --staged shows what the next commit will contain.
  • A commit stores a snapshot, a parent pointer, an author, a date and a message, identified by a hash.
  • git rm and git mv stage deletions and renames.

Check yourself

1. What does git init do?
2. Which command shows exactly what your next commit will contain?
3. After git add menu.txt, plain git diff prints nothing. Why?
4. What does a commit's parent line record?