Git Getting Started

Getting started with Git means learning one loop you will repeat thousands of times: change files, stage them with git add, and save a snapshot with git commit. In this chapter you will create a repository and make your first commits.

The Three Areas of Git

  • Working directory — the files you see and edit.
  • Staging area (index) — changes you have marked to go into the next commit.
  • Repository (.git folder) — the saved history of commits.

Create a Repository with git init

git init creates a hidden .git folder that holds all history and settings. Deleting that folder removes Git tracking but leaves your files.

mkdir portfolio && cd portfolio
git init
Initialized empty Git repository in /home/naeem/portfolio/.git/

Check git status

git status is your best friend. It tells you which branch you are on and which files are untracked, modified or staged. Run it often.

echo "# My Portfolio" > README.md
git status
On branch main

No commits yet

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

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

Stage and Commit

git add README.md
git commit -m "Add README"
[main (root-commit) a1b2c3d] Add README
 1 file changed, 1 insertion(+)
 create mode 100644 README.md

git add copies the current version of the file into the staging area. git commit saves everything staged as a new commit, with the message given by -m. The short code a1b2c3d is the start of the commit's unique hash.

Staging Shortcuts

git add .                 # stage all changes in this folder and below
git add -p                # review and stage changes piece by piece
git commit -am "Fix typo" # stage tracked modified files and commit in one step
git diff                  # see unstaged changes
git diff --staged         # see what will be committed
diff --git a/README.md b/README.md
-# My Portfolio
+# My Developer Portfolio

Writing Good Commit Messages

  • Use the imperative mood: "Add login form", not "Added login form".
  • Keep the first line under about 50 characters.
  • Explain why in the body if the change is not obvious.
  • Make small, focused commits — one logical change each.

Frequently asked questions

What does git add do?

It moves changes from your working directory into the staging area, choosing exactly what goes into the next commit. It does not save history by itself.

Why does Git have a staging area?

It lets you build commits deliberately, for example committing a bug fix separately from unrelated formatting changes in the same file.

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

In modern Git both stage new, modified and deleted files. git add . is limited to the current folder and below, while git add -A covers the whole repository.