Your First Git Repository: init, add, commit, status and log
Create a repository with git init, check its state with git status, choose files with git add, record snapshots with git commit, read the history with git log, and understand what lives in the hidden .git folder and the everyday edit-add-commit cycle.
kvi5hnuCode Sprout team member10 min readOn this page
This article builds a repository from nothing and records its first few commits. Along the way, it introduces the five commands you'll use more than any others:
| Command | What it does |
|---|---|
git init | turns a folder into a Git repository |
git status | shows what has changed and what will go into the next commit |
git add | chooses changes for the next commit |
git commit | records the chosen changes as a new snapshot |
git log | shows the history of commits |
The examples follow a small project, a website of hiking-trail notes called trail-guide, that grows through the course.
Creating a repository with git init
git init creates a repository in the current folder, or in a new folder if you give it a name. It doesn't touch your files. It adds a hidden .git folder, where Git keeps the history, the settings and everything else it needs:
mkdir trail-guide
cd trail-guide
git init
echo "--- status in a new, empty repository"
git status
echo "# Trail guide" > README.md
echo "body { font-family: sans-serif; }" > style.css
echo "--- status after creating two files"
git status
git add README.md
echo "--- status after adding README.md"
git status
git commit -m "Add README"
echo "--- the history"
git log
Initialized empty Git repository in /workspace/trail-guide/.git/
--- status in a new, empty repository
On branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)
--- status after creating two files
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
style.css
nothing added to commit but untracked files present (use "git add" to track)
--- status after adding README.md
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
style.css
[main (root-commit) 61da1f1] Add README
1 file changed, 1 insertion(+)
create mode 100644 README.md
--- the history
commit 61da1f1bc4c3d2495583c93941ff462706185b77
Author: Sam Lee <sam@example.com>
Date: Mon Mar 2 10:00:00 2026 +0000
Add README
Read the output of each git status carefully, because it's the best way to learn what Git is doing:
- In the new repository, Git is "On branch main" with "No commits yet". The branch exists in name only until the first commit.
- After creating the files, both are untracked: Git sees them but has never recorded them, and won't include them in a commit until you say so.
- After
git add README.md, the README is listed under "Changes to be committed", whilestyle.cssis still untracked.git adddoesn't save anything permanently. It puts the file's current contents into the staging area, the list of changes that the next commit will contain. git commit -m "Add README"records the staged snapshot. The summary line shows the branch,main, the fact that this is the first commit (root-commit), the short commit ID and the message.create mode 100644 README.mdmeans a new, ordinary (non-executable) file.
git log shows the commit's full ID, its author, its date and its message. style.css isn't in the commit, because it was never added. That's the point of the staging area: you decide exactly what each commit contains. Article 4 explains it in detail.
The everyday cycle: edit, add, commit
Once a file is tracked, Git notices every change to it. The everyday rhythm is: change files, check git status, add the changes you want, and commit. git status --short, or -s, gives a compact view, one line per file:
git init -q trail-guide
cd trail-guide
echo "# Trail guide" > README.md
git add README.md
git commit -q -m "Add README"
# Change the tracked README and create two new files
echo "Notes and maps for local hikes." >> README.md
echo "body { font-family: sans-serif; }" > style.css
mkdir trails
echo "Ridge loop: 8 km" > trails/ridge.txt
echo "--- short status"
git status --short
git add .
echo "--- short status after git add ."
git status -s
git commit -m "Add stylesheet and first trail"
echo "--- history, one line per commit"
git log --oneline
echo "--- which files each commit touched"
git log --stat --format="%h %s"
--- short status
M README.md
?? style.css
?? trails/
--- short status after git add .
M README.md
A style.css
A trails/ridge.txt
[main 700592e] Add stylesheet and first trail
3 files changed, 3 insertions(+)
create mode 100644 style.css
create mode 100644 trails/ridge.txt
--- history, one line per commit
700592e Add stylesheet and first trail
61da1f1 Add README
--- which files each commit touched
700592e Add stylesheet and first trail
README.md | 1 +
style.css | 1 +
trails/ridge.txt | 1 +
3 files changed, 3 insertions(+)
61da1f1 Add README
README.md | 1 +
1 file changed, 1 insertion(+)
In the short format, the two columns before each name are the staging area and the working tree:
| Code | Meaning |
|---|---|
?? | untracked |
M | modified in the working tree, not staged yet (the M is in the second column) |
M | modified and staged (the M is in the first column) |
A | new file, staged |
D or D | deleted, staged or not |
MM | staged, then changed again: part staged, part not |
Notice that the untracked trails/ folder was shown as one entry until it was added. Git tracks files, not folders: a folder appears in a commit only because files inside it do (article 8 covers empty folders).
git add . stages every change in the current folder and below: new, modified and deleted files. Run it from the top of the repository and it covers everything. git add -A does the same for the whole repository wherever you run it. Article 5 covers the other ways of choosing what to stage, including parts of a file.
What to add, and what not to
Before your first git add . in an existing project, run git status and read the list. Some files shouldn't be committed:
- Secrets: passwords, API keys, private keys,
.envfiles. Once committed and shared, a secret must be treated as leaked, even if you delete it later, because it stays in the history. - Generated files: build output, compiled code,
node_modules/, log files. They can be recreated from the source, and they clutter the history. - Personal files: editor settings, operating-system files such as
.DS_StoreorThumbs.db.
List them in a .gitignore file, ideally before the first commit, and Git stops offering them. Article 8 covers ignoring files in detail.
Writing commit messages
-m gives the message on the command line. For a longer message, give -m twice, which makes two paragraphs, or leave it out, and Git opens your editor. The usual shape is a short summary line, around 50 characters, then a blank line, then as much explanation as is useful:
git commit -m "Add elevation to trail pages" -m "Hikers asked for total climb; values come from the park's survey data."
Write the summary as a command, as if completing "If applied, this commit will...": "Add elevation to trail pages", not "Added" or "Adding". Article 6 covers good messages in detail.
What's inside .git
Everything Git knows about the repository lives in .git. You'll rarely look inside, and you shouldn't edit it by hand, but a quick look makes Git less mysterious:
git init -q trail-guide
cd trail-guide
echo "# Trail guide" > README.md
git add README.md
git commit -q -m "Add README"
ls -1 .git
echo "--- HEAD says which branch you're on"
cat .git/HEAD
echo "--- the branch is a file holding a commit ID"
cat .git/refs/heads/main
git rev-parse HEAD
echo "--- the repository's top folder, from anywhere inside it"
mkdir -p docs/notes && cd docs/notes
git rev-parse --show-toplevel
COMMIT_EDITMSG
HEAD
config
description
hooks
index
info
logs
objects
refs
--- HEAD says which branch you're on
ref: refs/heads/main
--- the branch is a file holding a commit ID
61da1f1bc4c3d2495583c93941ff462706185b77
61da1f1bc4c3d2495583c93941ff462706185b77
--- the repository's top folder, from anywhere inside it
/workspace/trail-guide
The parts that matter now:
HEADnames the current branch:ref: refs/heads/main.refs/heads/mainis the branch, and it's just a small file containing the ID of its latest commit. Branches really are that cheap (article 11). Repositories using the newer reftable format, planned as the default in Git 3.0, store branches in a compact table instead, so scripts should ask Git, withgit rev-parse main, rather than read the files.objects/holds the stored contents: every version of every file, every folder listing and every commit, each named by its hash (article 48).configis the repository's local settings file, the onegit configwrites without--global.indexis the staging area.
Git commands work from any subfolder of a repository, because Git searches upwards from the current folder until it finds a .git. Outside any repository, they fail with "not a git repository".
Deleting the .git folder deletes the repository's entire history and leaves only the current files. That's sometimes useful, to start over, but there's no undo, so do it deliberately.
Common mistakes with git init
- Running
git initin your home folder. Every file you own becomes a candidate for commits, and every folder below becomes "inside a repository". Ifgit statuslists your whole home folder, that's what happened: remove the stray~/.gitfolder, after checking it's not a repository you meant to keep. - Repositories inside repositories. Running
git initinside a folder that's already in a repository creates a separate, nested repository. The outer one then sees the inner folder as a single, odd entry instead of its files. If you want one project inside another, see submodules and subtrees (articles 41 and 42). Otherwise, keep repositories side by side. - Initializing to "connect to GitHub". To work on a project that already exists on a server, you don't run
git init. You clone it (article 18), which creates the repository and downloads its history in one step.
To create a repository in a new folder in one command, run git init new-folder. To put an existing project under version control, run git init inside it, then add and commit. A third form, git init --bare, creates a repository with no working files, used as a shared server copy (article 17).
What's current
git initwithout aninit.defaultBranchsetting prints a hint about choosing a branch name. Since Git 2.52, the hint also says the default will change frommastertomainin Git 3.0. Settinginit.defaultBranch(article 2) removes it.git statusgets faster in big repositories with the built-in file system monitor,core.fsmonitor=trueon Windows, macOS and, since Git 2.55, Linux, and the untracked cache (article 44).- Most editors and IDEs show the same information as
git statusin a sidebar. The command line remains the clearest way to see exactly what Git will do.
Common questions
What's the difference between git add . and git add -A?
Both stage new, modified and deleted files. git add . covers the current folder and its subfolders, and git add -A covers the whole repository, wherever you run it. From the top folder they do the same thing. In Git 1.x, git add . ignored deletions, which is why older guides recommend -A. Since Git 2.0, both include deletions.
Why doesn't Git track my empty folder?
Git tracks files, and a folder exists in a commit only because files are inside it. The usual workaround is to put a placeholder file, often named .gitkeep, in the folder. Article 8 covers this.
I ran git commit and got "no changes added to commit" or "nothing added to commit but untracked files present". Why?
No changes were staged. Modified or new files have to be added with git add first, or, for files Git already tracks, you can use git commit -a to stage all tracked changes automatically (article 6).
Is it safe to commit with a typo in the message?
Yes, and it's easy to fix while the commit is still only on your computer: git commit --amend -m "Better message" replaces the last commit's message (article 6).
Can I rename or move the project folder after git init?
Yes. The .git folder moves with it, and Git stores paths relative to the repository's top folder, so nothing breaks.
Summary
git initturns a folder into a repository by creating the hidden.gitfolder. Your files are untouched.- New files are untracked until you
git addthem.git addputs their current contents in the staging area, andgit commitrecords the staged snapshot. git statustells you what's staged, what's changed and what's untracked.git status -sis the compact form.git loglists commits, newest first.--onelineand--statare useful views.- Keep secrets and generated files out of commits with a
.gitignore. - A branch is a file in
.git/refs/heads/holding a commit ID, andHEADrecords which branch you're on.
Practice questions
You run these commands in an empty folder. What does git status --short print at the end?
git init -q
echo one > a.txt
echo two > b.txt
git add a.txt
echo more >> a.txt
Answer
AM a.txt
?? b.txt
a.txt was staged as a new file (A in the first column), then changed again, so the working tree differs from what's staged (M in the second column). Committing now would record a.txt with only the line one, because that's what was staged. b.txt was never added, so it's untracked.
After git init, a teammate runs git log and sees "fatal: your current branch 'main' does not have any commits yet". Is the repository broken?
Answer
No. A new repository has a current branch name, but the branch doesn't exist until the first commit, so there's no history to show. After the first git commit, git log works normally.
Why is it a bad idea to commit a .env file containing an API key and then delete it in the next commit?
Answer
Deleting the file in a later commit only removes it from the latest snapshot. The earlier commit still contains it, and anyone with a copy of the repository can see it, for example with git show <commit>:.env. Once shared, the key should be revoked and replaced. Removing it from history needs rewriting (article 47), and even that doesn't help with copies that were already cloned. Ignore such files from the start.
Previous: Installing Git and First-Time Setup: Identity, Editor, Default Branch and Help
Next: The Three Areas of Git: Working Tree, Staging Area and Repository

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.