Git init, add and commit

Create a repository, stage changes with git add, save them with git commit and check progress with git status.

Lesson 2 of 30Beginner18 min read

The everyday Git workflow is a short loop: change some files, choose which changes to save, then save them as a commit with a message. You'll repeat this loop dozens of times a day once it becomes a habit.

The key idea is that Git doesn't save every change automatically. You decide what goes into each commit and when. That lets you make small, focused commits like "Add contact form" or "Fix typo in footer", which makes the history easy to read and easy to undo.

In this lesson you'll turn a folder into a repository, learn Git's three areas, and make your first commits with git add and git commit.

Creating a repository

In a terminal, go into your project folder and run:

Terminal
git init

This creates a hidden .git folder where Git stores the history. Your files aren't changed, and nothing is tracked until you add it. You only run git init once per project. To start from a repository that already exists online, you'll use git clone instead, covered in the GitHub course.

The three areas

Git moves your changes through three places:

  1. Working directory: the files you see and edit.
  2. Staging area (also called the index): the changes you've chosen for the next commit.
  3. Repository: the commits Git has saved permanently.

git add moves changes from the working directory to the staging area. git commit takes everything in the staging area and saves it as one commit. This two-step process lets you commit some changes now and leave others for later.

git status

git status is the command you'll run most. It tells you:

  • which branch you're on,
  • which files are staged (green, "Changes to be committed"),
  • which files are modified but not staged (red, "Changes not staged"),
  • which files are untracked: new files Git isn't following yet.

Run it before and after every add and commit until the workflow feels natural.

git add and git commit

Terminal
git add index.html          # stage one file
git add css/                # stage a whole folder
git add .                   # stage every change in this folder
git commit -m "Add home page"
  • -m gives the commit message directly in the command. Without it, Git opens a text editor for you to write one.
  • A good message is short and says what the commit does, in the present tense: "Add search box", "Fix broken link in footer".
  • Each commit gets a unique ID called a hash, such as a1b2c3d.

Longer messages and skipping the staging step

Running git commit without -m opens your editor so you can write a fuller message. Use this when a one-line summary can't explain the change. The convention is a short subject line, a blank line, then a body that explains why the change was made:

Text
Fix total when the basket is empty

The checkout page divided by the number of items, which
crashed when the basket was empty. Show 0.00 instead.

Git and GitHub treat the first line as the title, so git log --oneline shows only "Fix total when the basket is empty". Save and close the editor to finish the commit. If you close it with an empty message, Git cancels the commit with "Aborting commit due to empty commit message", which is a handy way to back out.

You can also pass -m twice. The first becomes the subject and the second becomes a separate paragraph underneath it: git commit -m "Fix empty basket total" -m "Show 0.00 instead of crashing."

git commit -a -m "message", often written git commit -am, stages every change to files Git already tracks and commits them in one step. It saves typing for quick edits, but it has two catches. New files are skipped: an untracked file stays untracked until you git add it. And you lose the pause where you check what's going in, so run git status first.

Git Commits Syntax

The basic workflow

git init
git status
git add <file>   # or: git add .
git commit -m "Describe the change"

Git Commits Summary

  • git init turns a folder into a repository (once per project).
  • Changes move from the working directory, to the staging area, to the repository.
  • git status shows what's staged, modified and untracked.
  • git add <file> or git add . stages changes; git commit -m "message" saves them.
  • Write short, present-tense commit messages and commit one change at a time.

Git Commits Examples

Each example shows the code, the output it produces and what each part does.

Example 1

A brand-new repository

Terminal
mkdir my-site
cd my-site
git init
git status
Output
Initialized empty Git repository in /home/sam/my-site/.git/
On branch main

No commits yet

nothing to commit (create/copy files and use "git add" to track)
Line-by-line explanation
  1. Lines 1 to 2Create a folder and move into it.
  2. Line 3git init creates the hidden .git folder.
  3. Line 4Nothing to commit yet, because the folder is empty.
Example 2

Stage and commit a file

After creating index.html in the folder:

Terminal
git status
git add index.html
git status
Output
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	index.html

nothing added to commit but untracked files present (use "git add" to track)

On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   index.html
Line-by-line explanation
  1. Line 1The new file is "untracked": Git sees it but isn't following it yet.
  2. Line 2git add stages it.
  3. Line 3Now it's listed under "Changes to be committed".
Example 3

Make the commit

Terminal
git commit -m "Add home page"
git status
Output
[main (root-commit) 3f9c2ab] Add home page
 1 file changed, 12 insertions(+)
 create mode 100644 index.html

On branch main
nothing to commit, working tree clean
Line-by-line explanation
  1. Line 1The commit is saved with the hash 3f9c2ab (yours will differ) and the message.
  2. Line 2"working tree clean" means everything is committed.

Common Git Commits Mistakes

Committing before adding

Don't
git commit -m "Update styles"
# no changes added to commit
Do
git add styles.css
git commit -m "Update styles"

Only staged changes go into a commit. Use git status to check what's staged.

Vague commit messages

Don't
git commit -m "stuff"
Do
git commit -m "Add dark mode toggle to header"

In six months, "stuff" tells you nothing about what changed or why.

Running git init in the wrong folder

Don't
cd ~
git init        # turns your whole home folder into a repo
Do
cd ~/projects/my-site
git init

Check where you are with pwd first. If it happens, delete the stray .git folder.

Git Commits in Real Projects

Commits in real projects

Open-source projects often have tens of thousands of commits, each with a message that explains one change. Teams agree on message styles, such as starting with a verb, so the history reads like a changelog: "Add password reset", "Fix crash on empty cart", "Update dependencies".

Git Commits FAQ

How do I undo git add before committing?

Run git restore --staged index.html to take that file out of the staging area. Your edits stay in the file; they just won't go into the next commit. On very old versions of Git, git reset index.html does the same job.

What is the difference between git add . and git add -A?

In current versions of Git, both stage new, modified and deleted files. The difference is scope: git add . covers only the folder you're in and the folders inside it, while git add -A covers the whole repository wherever you run it. From the project's top folder they do the same thing.

Why does git status say "nothing added to commit but untracked files present"?

You have new files that Git has noticed but isn't tracking yet, and nothing is staged. Run git add on the files you want to commit. If they should never be committed, such as log files or build output, add them to .gitignore instead.

Can I make a commit without changing any files?

Git normally refuses with "nothing to commit". If you really need one, for example to trigger an automated build or to mark a starting point, run git commit --allow-empty -m "Trigger build". The commit records a message but no file changes.

How do I remove Git from a project folder?

Delete the hidden .git folder in the project's top folder, for example with rm -rf .git on macOS or Linux. Your files stay exactly as they are, but all of the commit history is gone and the folder is no longer a repository. Make sure it's the right folder and that you don't need the history first.

Git Commits Exercises

Answer each one and check it instantly. If you get stuck, open the hint before the answer.

Fill in the blankseasy

Complete the commands to stage every change and commit it.

git  .
git  -m "Add about page"
Multiple choiceeasy

Which command shows which files are staged, modified or untracked?

Multiple choicemedium

You changed index.html and style.css but only want to commit style.css. What do you run?

Predict the outputmedium

You create notes.txt and run git status without adding it. Where is it listed?

Multiple choicemedium

You changed three files but only want to commit one. What do you do?

Git Commits Quiz

5 questions to check what you learned.

  1. What does git init do?

    • Downloads a repository
    • Makes the first commit
    • Installs Git
    • Creates a new repository in the current folder
  2. What is the staging area?

    • The folder where you edit files
    • A copy of the repository online
    • The set of changes chosen for the next commit
    • The list of branches
  3. What does -m do in git commit -m "Fix typo"?

    • Gives the commit message on the command line
    • Merges the commit
    • Commits to the main branch
    • Commits only modified files
  4. What does "working tree clean" mean?

    • The repository has no commits
    • Git deleted your files
    • There are no changes that haven't been committed
    • All branches are merged
  5. What are Git's three areas?

    • Draft, final and published
    • The working directory, the staging area and the repository
    • Local, remote and cloud
    • Files, folders and branches