Chapter 6 · Lesson 3 of 10
Git for deployers

Your first Git repository: init, add, commit and log

A Git repository starts with git init, and you save work by staging files with git add and recording them with git commit, then review it with git log and git diff.

By the end you will

Create a repository, make a commit, and use status, log and diff to see what changed.

Time
15 min
Level
Beginner
Check
4 questions
You need
  • Git installed with your name and email set

Git keeps your work in three areas

Git has three areas: the working tree where you edit, the staging area where you pick changes, and the repository where commits are stored.
  1. git add copies the chosen changes from your working files into the staging area.
  2. git commit saves everything staged as a permanent snapshot in the repository.
  3. git restore --source=HEAD brings the last committed version back into your working files.
  4. git restore --staged takes a change out of staging, and your edit stays in the working tree.
  5. You can edit, stage and commit again as often as you like.
Static diagram
  1. Working treegit addStaging area
  2. Staging areagit commitRepository
  3. Repositorygit restoreWorking tree
  4. Staging areaunstageWorking tree
  5. Working treeadd againStaging area

To start a Git repository, run git init in your project folder. After that you save work in two steps: git add puts files in the staging area, and git commit -m "message" records them in history. git status tells you where things stand, git log lists the history, and git diff shows what changed.

How it works#

Git moves your changes through three areas, and understanding them explains almost every command:

  1. Working tree. The files you see and edit in your folder.
  2. Staging area (the index). A list of changes you picked for the next commit.
  3. Repository. The history of commits stored in the .git directory.

git add copies changes from the working tree into the staging area. git commit turns the staging area into a permanent commit in the repository. The middle step feels like extra work at first, but it lets you commit only part of what you changed, so each commit holds one logical change. If you want the background first, read what Git is.

Step 1: Create a repository#

bash
mkdir hello && cd hello
git init

Git prints Initialized empty Git repository in .../hello/.git/. The .git folder is the whole repository. Delete it and the history is gone, so do not remove it by accident. With init.defaultBranch set as in the setup lesson, the first branch is called main.

Add a file to track:

bash
echo "# hello" > README.md

Step 2: Check the state with git status#

bash
git status

Git lists files in three groups: untracked (new, Git has never seen them), changes not staged for commit (tracked files you edited), and changes to be committed (already staged). Run git status constantly. It also prints the next command you probably want, such as use "git add <file>..." to include in what will be committed. For a compact view use git status -s, where ?? marks untracked files, M a modified file and A an added one.

Step 3: Stage and commit#

bash
git add README.md
git commit -m "first commit"

git add . stages everything under the current folder, which is convenient but also stages things you did not mean to add, so look at git status first. Do not stage secrets or build output. The gitignore lesson shows how to keep them out.

Write commit messages that finish the sentence “This commit will…”. A short first line (about 50 characters) in the imperative works well: fix login redirect, add health check endpoint. If you run git commit with no -m, Git opens your editor so you can write a longer message with a body.

Step 4: Read the history with git log#

bash
git log
git log --oneline

git log shows the full hash, author, date and message of each commit. --oneline shows one line per commit with an abbreviated hash. Handy extras: git log -n 5 limits the list, and git log --stat adds which files changed. Press q to leave the pager.

Step 5: See changes with git diff#

Edit the file, then compare:

bash
echo "A small demo." >> README.md
git diff

Lines starting with + were added and lines starting with - were removed. The @@ lines mark where in the file the change sits. Now stage it and compare again:

bash
git add README.md
git diff            # prints nothing, nothing is unstaged
git diff --staged   # shows what the next commit will contain

Use git diff --staged right before every commit as a last check. It answers “what am I about to record?” and often catches a stray debug line.

Once commits pile up, a CI/CD pipeline can test and deploy them for you. The blog post Deploy an Express app with Docker on a VPS shows where Git fits, and a bad commit can be fixed with reset and revert. Keep any secret out of your commits.

Check it worked#

After the commit, git status should say nothing to commit, working tree clean, and git log --oneline should show your commit with (HEAD -> main) next to it. To see the commit itself, run git show. If both look right, the repository works.

Edge cases and gotchas#

  • Empty folders are not tracked. Git stores files, not directories. To keep an empty folder, put a placeholder file such as .gitkeep in it.
  • Editing after add. If you stage a file and then edit it again, the staged version is the old one. Run git add again to stage the new edit.
  • git add -A and . can pick up junk. Logs, node_modules and .env files end up in history unless ignored first.
  • Commit often, but keep each commit coherent. A commit that mixes a bug fix and a rename is hard to review and hard to undo.
  • Line endings. Files edited on Windows and Linux can show every line as changed. A .gitattributes file or core.autocrlf settles it, but ask your team before changing it.
  • Detached from the log. git log shows only the history reachable from where you are. After switching branches the list can look different, which is normal.

Troubleshooting#

SymptomLikely causeFix
fatal: not a git repository (or any of the parent directories): .gitYou are not inside a repositorycd into the folder, or run git init
nothing added to commit but untracked files present (use "git add" to track)You ran commit before addgit add <file>, then commit again
Author identity unknownName and email are not configuredgit config --global user.name and user.email
error: pathspec 'readme.md' did not match any file(s) known to gitWrong file name or caseCheck ls, file names are case sensitive
Aborting commit due to empty commit message.You closed the editor without textRun commit again and write a message, or use -m
fatal: your current branch 'main' does not have any commits yetYou ran git log before the first commitMake a commit first
git diff prints nothing though you edited a fileThe change is already stagedUse git diff --staged
fatal: detected dubious ownership in repositoryThe folder belongs to a different userFix ownership, or add it to safe.directory if you trust it

Quick reference#

bash
git init                        # create a repository here
git status                      # what changed
git add README.md               # stage one file
git add .                       # stage everything under here
git commit -m "message"         # record staged changes
git log --oneline               # compact history
git diff                        # unstaged changes
git diff --staged               # staged changes
git show                        # the latest commit in detail

Next#

Made a mistake? Undo mistakes in Git shows how to fix it safely.

Try it: Make your first commit

Create a repository, stage a file, commit it and read the log. The output below is typical, hashes will differ on your machine.

practice, nothing runs on your machine

Want a real machine? Free browser labs: Killercoda

Check what you learned4 questions
1Which command records the staged changes in history?
2You edited a file but did not run git add. Which command shows the change?
3What are the three areas in Git's basic model?
4What does git log --oneline show?

Frequently asked questions

What does git init do?

It creates a hidden .git directory in the current folder. That directory is the repository, and from then on Git can track the files in the folder.

What is the difference between git add and git commit?

git add copies the current version of a file into the staging area. git commit takes everything in the staging area and records it permanently in history.

What is the staging area in Git?

It is a holding area, also called the index, where you assemble the next commit. It lets you commit only some of your changes instead of everything at once.

How do I see what I changed before committing?

Run git diff for changes not yet staged and git diff --staged for changes that are staged and will go into the next commit.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios