# 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.

URL: https://levelrail.com/learn/git/your-first-git-repository/
Updated: 2026-11-27

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](https://levelrail.com/learn/git/what-is-git-and-why-servers-use-it/)).

## 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](https://levelrail.com/learn/git/install-and-configure-git/)), 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](https://levelrail.com/learn/git/gitignore-and-keeping-secrets-out-of-git/)) 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](https://levelrail.com/learn/glossary/#ci-cd)) pipeline can test and deploy them for you. The blog post [Deploy an Express app with Docker on a VPS](https://levelrail.com/blog/deploy-express-app-docker-vps/)) shows where Git fits, and a bad commit can be fixed with [reset and revert](https://levelrail.com/learn/git/undo-mistakes-in-git/)). Keep any [secret](https://levelrail.com/learn/glossary/#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

| 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

```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
```

## Related lessons

- [Install and configure Git](https://levelrail.com/learn/git/install-and-configure-git/)): set the identity each commit records.
- [Undo mistakes in Git](https://levelrail.com/learn/git/undo-mistakes-in-git/)): fix a wrong commit or unstage a file.
- [Git branches and merging](https://levelrail.com/learn/git/git-branches-and-merging/)): work on changes without touching main.
- [Gitignore and keeping secrets out of Git](https://levelrail.com/learn/git/gitignore-and-keeping-secrets-out-of-git/)): stop junk and secrets from being committed.
- [Essential Linux commands for servers](https://levelrail.com/learn/server-basics/essential-linux-commands-for-servers/)): the shell commands used around Git.

## Next

Made a mistake? [Undo mistakes in Git](https://levelrail.com/learn/git/undo-mistakes-in-git/)) shows how to fix it safely.

## 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

- [git-init manual](https://git-scm.com/docs/git-init)
- [git-add manual](https://git-scm.com/docs/git-add)
- [git-diff manual](https://git-scm.com/docs/git-diff)
