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.
- Updated Nov 27, 2026
- Markdown
Create a repository, make a commit, and use status, log and diff to see what changed.
- Git installed with your name and email set
Git keeps your work in three areas
- git add copies the chosen changes from your working files into the staging area.
- git commit saves everything staged as a permanent snapshot in the repository.
- git restore --source=HEAD brings the last committed version back into your working files.
- git restore --staged takes a change out of staging, and your edit stays in the working tree.
- You can edit, stage and commit again as often as you like.
Static diagram
- Working treegit addStaging area
- Staging areagit commitRepository
- Repositorygit restoreWorking tree
- Staging areaunstageWorking tree
- 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:
- Working tree. The files you see and edit in your folder.
- Staging area (the index). A list of changes you picked for the next commit.
- Repository. The history of commits stored in the
.gitdirectory.
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#
mkdir hello && cd hello
git initGit 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:
echo "# hello" > README.mdStep 2: Check the state with git status#
git statusGit 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#
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#
git log
git log --onelinegit 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:
echo "A small demo." >> README.md
git diffLines 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:
git add README.md
git diff # prints nothing, nothing is unstaged
git diff --staged # shows what the next commit will containUse 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
.gitkeepin it. - Editing after add. If you stage a file and then edit it again, the staged version is the old one. Run
git addagain to stage the new edit. git add -Aand.can pick up junk. Logs,node_modulesand.envfiles 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
.gitattributesfile orcore.autocrlfsettles it, but ask your team before changing it. - Detached from the log.
git logshows only the history reachable from where you are. After switching branches the list can look different, which is normal.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
fatal: not a git repository (or any of the parent directories): .git | You are not inside a repository | cd into the folder, or run git init |
nothing added to commit but untracked files present (use "git add" to track) | You ran commit before add | git add <file>, then commit again |
Author identity unknown | Name and email are not configured | git config --global user.name and user.email |
error: pathspec 'readme.md' did not match any file(s) known to git | Wrong file name or case | Check ls, file names are case sensitive |
Aborting commit due to empty commit message. | You closed the editor without text | Run commit again and write a message, or use -m |
fatal: your current branch 'main' does not have any commits yet | You ran git log before the first commit | Make a commit first |
git diff prints nothing though you edited a file | The change is already staged | Use git diff --staged |
fatal: detected dubious ownership in repository | The folder belongs to a different user | Fix ownership, or add it to safe.directory if you trust it |
Quick reference#
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 detailRelated lessons#
- Install and configure Git: set the identity each commit records.
- Undo mistakes in Git: fix a wrong commit or unstage a file.
- Git branches and merging: work on changes without touching main.
- Gitignore and keeping secrets out of Git: stop junk and secrets from being committed.
- Essential Linux commands for servers: the shell commands used around Git.
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.
Want a real machine? Free browser labs: Killercoda
Check what you learned
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
Keep going: guides and tutorials
- Install a Self-Hosted PaaS on Your Own Server in Five Minutes One installer download, one setup token, one sample app. The full Levelrail install on a fresh Linux server, with the checks and the fixes.
- Deploy a Docker Compose App Self-Hosted: Web, Redis and Rollback Deploy a Docker Compose app on your own server with Levelrail: a web service and Redis, a bad release that does not take traffic, and a compose rollback.