Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 78 ยท Getting Started

Your First Repository

Create a Git repository from scratch with git init, track files with git add, record commits with git commit and read history with git log. Includes a tour of the .git folder.

It is time to build a repository on purpose, one command at a time, and to understand what each command did. By the end of this lesson you will have created a repository, made several commits, written good commit messages, read the history, and looked inside the .git folder.

Everything here runs for real in the editors below. If you have not yet read How Git Works, the short version is: you edit files in the working tree, put the changes you want into the staging area with git add, and record them as a commit with git commit.

Every example starts with the same few lines that fix the author and date. In real life you set your identity once (Installing and Configuring Git) and let Git use the current time; we pin them so the hashes you see here are exactly the ones you will get.

Step 1: git init

git init makes the current folder a repository. It creates a hidden .git directory and nothing else; your files are untouched.

Bash git init and first git status

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Git prints Initialized empty Git repository in recipes/.git/. Then git status tells you three things: you are On branch main, there are No commits yet, and there is nothing to commit. That sentence "(create/copy files and use "git add" to track)" is Git's way of suggesting the next step. Read git status messages: they are written to teach you.

You can also give git init a folder name (git init recipes) to create the folder and the repository in one go. If you run it in a folder that is already a repository, it is harmless: it just reports "Reinitialized existing Git repository".

Do not run git init inside another repository (for example inside your home folder when it is already a repository), unless you want a nested one. Check with git status first. If you ever create one by mistake, deleting the inner .git folder undoes it.

Step 2: tracked and untracked files

Git sorts every file in the folder into one of two groups:

  • Untracked: Git has never been told about it. It is ignored in commits until you add it.
  • Tracked: Git knows it, because it was in the last commit or has been staged.

A tracked file is then either unmodified, modified or staged. The following diagram shows the life cycle:

TEXT
  untracked ---- git add ---->  staged ---- git commit ---->  unmodified
                                  ^                              |
                                  |                           edit file
                        git add   |                              v
                                  +----------------------- modified

Create two files and watch how git status reports them before and after staging. The full version of the command is verbose and friendly; -s (short) is compact and what experienced users type.

Bash Untracked, staged and committed

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Look at the three states. At first both files are ??, untracked. After git add pasta.txt it becomes A (added to the index) while salad.txt is still ??. After committing, only salad.txt remains untracked: it was not part of the commit, because we never staged it. This is the staging area doing its job: the commit contains exactly what you chose.

The commit command printed [main (root-commit) 1b6438a] Add pasta recipe. root-commit marks the very first commit, which has no parent. Then it summarizes the changes: 1 file changed, 2 insertions(+).

Step 3: git add in its common forms

You will stage in a few ways. All of them copy the current content of the files into the index at that moment.

Command Stages
git add file.txt One file
git add dir/ Everything inside a folder
git add . Everything under the current folder (new, modified, deleted)
git add -A The same, but for the whole repository regardless of where you are
git add -u Only files Git already tracks (modified and deleted), no new files

git add . is the beginner's favourite, and it is fine, but run git status first so you do not commit things you did not mean to, such as passwords or build output. .gitignore and Untracked Files shows how to keep such files out for good.

Step 4: git commit and good messages

git commit -m "message" records the staged snapshot. Without -m, Git opens your editor so you can write a longer message; our sandbox cannot do that, so we always use -m.

A commit message has a subject (first line) and, when needed, a body after a blank line. You can give several -m options; each becomes a paragraph. Here are the conventions that professionals follow:

  • Subject in the imperative mood: "Add pasta recipe", not "Added" or "Adds". A good test: it should complete the sentence "If applied, this commit will ... ".
  • Keep the subject at about 50 characters or less, no trailing period.
  • Use the body to explain why the change was made, not what; the diff already shows what.
  • One logical change per commit.

Making Good Commits goes deeper, including Conventional Commits and amending.

Let us make a small history with proper messages, then read it with git log. We also use the commit helper to bump the date, so the three hashes are stable.

Bash A first history with git log

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

git log lists commits newest first. Each entry shows the full hash, author, date and message (the body paragraph is indented under the subject). The hash is not random: it is a SHA-1 of the commit's content, so identical input gives an identical hash. --oneline abbreviates the hash to seven characters and shows only the subject. --stat adds which files changed and how many lines. You will learn all log options in Viewing History.

The first line of each full entry also says (HEAD -> main) when it is the tip of the current branch and the output goes to a terminal; piped output (like ours) hides decorations unless you ask with --decorate.

Step 5: seeing what a commit changed

Two commands answer "what did this commit do?": git show prints the commit and its diff, and git diff compares any two points. For the latest commit:

Bash git show for the latest commit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The diff uses the unified format: lines starting with - were removed, lines starting with + were added, and the @@ line says which line numbers the hunk covers. Status, Diff and Seeing Changes teaches reading it fluently.

A tour of the .git folder

All the history you just created lives in .git. You never edit it by hand, but knowing the layout removes the mystery.

Bash What is inside .git

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The key parts:

Path What it is
HEAD A text file naming the current branch (ref: refs/heads/main)
refs/heads/ One tiny file per branch, containing a commit hash
objects/ The database: every commit, folder listing (tree) and file version (blob)
index The staging area (a binary file)
config The repository's local configuration
hooks/ Optional scripts Git runs at certain moments (Git Hooks)

Our single commit produced three objects: the blob (the file content), the tree (a folder listing that points to the blob) and the commit (which points to the tree and carries the message). git cat-file -p HEAD decodes the commit. These ideas return in Git Objects.

The .git folder is your history. If you delete it, the project becomes a plain folder again and the history is gone. If you copy the folder somewhere else with .git included, you have a full backup.

Common first-day questions

I committed but want to change the message. Run git commit --amend -m "better message" right away, before sharing the commit. See Making Good Commits.

I added a file by mistake. git restore --staged file.txt removes it from the staging area without touching the file. See Undoing Things.

Can I commit an empty folder? No. Git tracks files, not folders. The usual trick is a placeholder file such as .gitkeep.

Should I commit often? Yes. Small commits are easy to review, easy to undo and easy to understand later.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does git init create?

    git init only creates the hidden .git directory. There are no commits until you stage and commit something.
  2. 2. You create a.txt and b.txt, then run git add a.txt and git commit. What is in the commit?

    A commit contains exactly what is in the staging area. b.txt was never added, so git status still lists it as untracked.
  3. 3. Which commit subject follows the usual convention?

    Use the imperative mood, be specific, keep it short and omit the final period. A good subject completes 'If applied, this commit will ...'.
  4. 4. What does [main (root-commit) 1b6438a] mean in the output of git commit?

    root-commit is shown for a commit without a parent, which is the first one in a repository.
  5. 5. Which command shows the history, newest first, one line per commit?

    git log --oneline prints an abbreviated hash and the subject of each commit. status shows the current state, not history.

Summary

  • git init creates the repository (the .git folder); git status reports the state of your files and suggests next steps.
  • Files are untracked until you git add them; a commit contains only what is staged.
  • git commit -m "Imperative subject" records a snapshot; use a body to explain why.
  • git log (with --oneline, --stat) and git show read history.
  • The .git folder holds HEAD, branch files under refs/heads, the objects database, the index and config.
  • Commit often, keep each commit to one logical change and check git status before committing.

Next: Getting Help and Learning Git