Sub topic 9
Making Your First Commit
This chapter walks through creating a repository, staging a file, and committing it. It assumes you have read the earlier chapters. You should have Git installed, git --version working, and user.name, user.email, and init.defaultBranch configured. You should also be comfortable opening a terminal and running simple shell commands. The chapter also covers what each command changes on disk, the errors beginners most often hit, and how to read the output.
What you will do
You will do four things:
- Create an empty directory and turn it into a repository with
git init. - Create a file in the working tree and inspect it with
git status. - Copy the file into the staging area with
git add. - Record a snapshot with
git commitand read it back withgit log.
Each step moves your work between the three areas introduced earlier: the working tree, the staging area (also called the index), and the repository.
Step 1: Create a repository
A repository is a directory that Git tracks, plus a hidden .git directory where Git keeps the history. Create a directory and initialize it:
mkdir demo && cd demo
git initGit prints a message like this:
Initialized empty Git repository in /home/you/demo/.git/If you set init.defaultBranch to main, the first branch is named main. Without that setting, newer Git versions may print a hint about the default branch name, and the branch may be master.
What `git init` created
Look inside the new directory, including hidden entries:
ls -a
ls .gitYou will see the .git directory. Inside it are files and folders such as HEAD, config, objects, and refs. A short guide to the ones you will meet first:
| Entry in `.git` | Purpose |
|---|---|
| `HEAD` | Points to the branch you are currently on |
| `config` | Settings that apply only to this repository (the local scope) |
| `objects/` | The database that stores file contents, directory trees, and commits |
| `refs/` | Names, such as branches and tags, that point to commits |
The .git directory is the repository. The files outside it are your working tree. If you delete .git, you delete the whole history and the directory becomes an ordinary folder again. Do not edit files inside .git by hand while you are learning.
Nesting warning
Do not run git init inside a directory that is already inside another repository. You would create a repository within a repository, and the outer one will treat the inner one as an opaque entry. To check whether you are already inside a repository, run:
git rev-parse --is-inside-work-treeIt prints true inside a repository. Outside one, it prints an error that says not a git repository.
Step 2: Create a file and check status
Create a file in the working tree:
echo "Hello, Git" > hello.txt
git statusTypical output:
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
hello.txt
nothing added to commit but untracked files present (use "git add" to track)Read it line by line:
On branch mainnames the current branch.No commits yettells you the history is empty. This is normal for a new repository.Untracked fileslists files that exist in the working tree but that Git has never been told to record.
A file is in one of two broad states: tracked or untracked. A tracked file is one Git knows about from the last commit or from the staging area. Everything else is untracked. Creating a file does not make Git track it. You must stage it.
Step 3: Stage the file
Staging selects what goes into the next commit. Run:
git add hello.txt
git statusThe status output changes:
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: hello.txtThe heading is now Changes to be committed. The file is in the staging area, and it will be part of the next commit.
Why a staging area exists
A new learner often asks why Git does not just commit everything in the working tree. The staging area lets you build a commit deliberately. If you have edited five files but only two belong to one logical change, you stage those two and commit them. You then stage and commit the other three separately. The result is a clear history where each commit has one purpose.
Common ways to stage
| Command | What it stages |
|---|---|
| `git add hello.txt` | Only the named file |
| `git add docs/` | Everything under the `docs` directory |
| `git add .` | All changes under the current directory |
| `git add -p` | Chosen pieces of changes, interactively |
For a first commit, name the file explicitly. git add . is convenient, but it also stages files you did not intend to commit, such as editor backups, build output, or files with passwords in them. Run git status before you commit so you see exactly what is staged.
Step 4: Commit
A commit is a snapshot of everything in the staging area, together with a message, your name and email, and a timestamp. Create one:
git commit -m "Add hello.txt"Typical output:
[main (root-commit) 3f2a9c1] Add hello.txt
1 file changed, 1 insertion(+)
create mode 100644 hello.txtHow to read it:
mainis the branch that received the commit.(root-commit)appears only for the very first commit in a repository, because it has no parent.3f2a9c1is the abbreviated commit ID. Yours will differ.1 file changed, 1 insertion(+)summarizes the change.create mode 100644records the file permission mode.100644means a regular, non-executable file.
The commit ID is a hash computed from the commit's content, including the parent commit, the author, the message, and the snapshot. It is unique to that content. Git uses it to identify the commit.
Writing a good message
The -m option supplies the message on the command line. Keep the first line short, about 50 characters, and write it as an instruction: "Add hello.txt", "Fix login timeout". For larger changes, leave a blank line after the first line and then explain why the change was made. To write a longer message, run git commit without -m. Git opens your configured editor.
If the editor opens and you are stuck
When you run git commit without -m, Git opens the editor named in core.editor, or a default such as Vim or nano. If Vim opens and you do not know it, press i to start typing, type the message, press Esc, then type :wq and press Enter. To abandon the commit instead, press Esc, then type :q! and press Enter. An empty message aborts the commit.
Step 5: Read the history
Show the history:
git log --onelineOutput:
3f2a9c1 (HEAD -> main) Add hello.txtHEAD -> main means you are on the main branch and HEAD points to it. Run the full form to see the author and date:
git logcommit 3f2a9c1e5b7d0a4c8e91f6b2d3a7c5e0f1b49a8d
Author: Your Name <[email protected]>
Date: Fri Dec 4 14:06:04 2026 +0000
Add hello.txtThe author line comes from the user.name and user.email values you set earlier. This is why the identity configuration matters. It is written into every commit.
Confirm that the working tree and the repository agree:
git statusOn branch main
nothing to commit, working tree cleanworking tree clean means no differences exist between your files, the staging area, and the last commit.
Seeing a second change
One commit does not show the full cycle. Modify the file and watch the status change:
echo "A second line" >> hello.txt
git status
git diffgit status now reports Changes not staged for commit and lists modified: hello.txt. git diff compares the working tree with the staging area and prints the changed lines, with a + before added lines. Stage and commit the change:
git add hello.txt
git commit -m "Add a second line to hello.txt"
git log --onelineThe log now shows two commits, newest first. The second commit has a parent, so it does not say root-commit.
A related command, git diff --staged, compares the staging area with the last commit. Use git diff to see what you have not yet staged and git diff --staged to see what you are about to commit.
A shortcut and a caution
git commit -a -m "message" stages every modified tracked file and commits in one step. It does not include untracked files, so a brand-new file still needs git add. Use it carefully. It skips the review that comes from staging deliberately.
Common errors
| Message | Cause | Fix |
|---|---|---|
| `fatal: not a git repository (or any of the parent directories): .git` | You are in a directory that is not a repository | Run `cd` into the repository, or run `git init` |
| `Author identity unknown` and `*** Please tell me who you are.` | `user.name` and `user.email` are not set | Run the `git config --global` commands for both values |
| `nothing to commit, working tree clean` | No changes exist to record | Edit a file first, or confirm you meant to commit |
| `nothing added to commit but untracked files present` | You created files but did not stage them | Run `git add <file>` |
| `pathspec 'hello.txt' did not match any files` | The file name is wrong or you are in the wrong directory | Run `ls` and `pwd`, then correct the name or path |
When you see an error, read the whole message. Git messages often include the exact command to run next.
Fixing a mistake in the last commit
If you made a typo in the last commit message and have not shared the commit with anyone, you can replace it:
git commit --amend -m "Add hello.txt greeting"This creates a new commit and gives it a new ID, replacing the old one. Amending rewrites history, so avoid it once the commit has been pushed to a shared repository. Later chapters cover safer ways to undo changes.
Fixing an accidental `git add`
If you staged a file by mistake, remove it from the staging area without touching the file itself:
git restore --staged hello.txtOn Git versions before 2.23, use git reset HEAD hello.txt instead. The file stays in your working tree and becomes untracked or modified again.
Ignoring files
Some files should never be committed, such as logs, compiled output, and local settings. A file named .gitignore in the repository root lists patterns Git should skip:
printf "*.log\nbuild/\n" > .gitignore
git statusGit now reports .gitignore as untracked, and any .log file or build/ directory stops appearing in the untracked list. Commit .gitignore itself so others get the same rules. Ignoring has no effect on files that are already tracked.
Checking your understanding
Exercise
You create notes.txt in a repository and run git commit -m "Add notes" without running git add. Git reports nothing added to commit but untracked files present. Why, and what do you run next?
Show answer
A commit records only what is in the staging area, and notes.txt is untracked, so nothing was staged. Run git add notes.txt, check with git status that it appears under Changes to be committed, then run git commit -m "Add notes" again.
Exercise
After your first commit, you edit hello.txt and run git diff. It prints the change. You then run git add hello.txt and git diff again, and it prints nothing. Where did the change go, and which command shows it now?
Show answer
The change moved from the working tree to the staging area. git diff compares the working tree with the staging area, and the two now match, so it prints nothing. git diff --staged compares the staging area with the last commit and shows the change.
Exercise
In the output [main (root-commit) 3f2a9c1] Add hello.txt, what does (root-commit) tell you, and when will you not see it again in this repository?
Show answer
It tells you this commit has no parent, so it is the first commit in the repository. Every later commit has a parent and will print only the branch name and ID, such as [main 8d1e0b4] Add a second line.