Git basics: init, add, commit, log and diff
Updated · Every command on this page ran in our practice shell
Short answer
Run git init once to start a repository. Then the loop is: change files, git status to see what changed, git add file to stage it, and git commit -m "message" to save a snapshot. git log shows the history and git diff shows unsaved changes.
Git keeps a history of your project as a series of snapshots called commits. You'll use the same handful of commands every day: make a change, check it, stage it, commit it. Each example runs in a practice repository, so you can try them below.
Start a repository with git init
git init turns the current folder into a repository by creating a hidden .git folder. That folder holds the entire history, so never delete it. On a new machine, first tell git who you are, or your first commit fails with Author identity unknown: git config --global user.name "Your Name" and git config --global user.email "you@example.com". git init calls the first branch master unless you use git init -b main or set git config --global init.defaultBranch main.
learner@shellstreak:~/garden$ git init Initialized empty Git repository in /home/learner/garden/.git/
See what's changed with git status
git status tells you which files are new (untracked), changed, or staged for the next commit. It also suggests the commands to move between those states. git status -s gives a compact version.
learner@shellstreak:~/garden$ git status On branch main No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) plan.md plants.txt tools.txt nothing added to commit but untracked files present (use "git add" to track)
Stage changes with git add
git add file puts the file's current version in the staging area: the list of changes for your next commit. git add . stages everything in the current folder. Staging lets you commit related changes together and leave the rest for later.
learner@shellstreak:~/garden$ git add plan.md && git status -s A plan.md ?? plants.txt ?? tools.txt
Save a snapshot with git commit
git commit -m "message" saves everything staged as a new commit. Write messages that say what the change does, like Add plant list. git commit -am "message" stages all changes to already-tracked files and commits in one go (new files still need git add).
learner@shellstreak:~/garden$ git add . && git commit -m "Start garden plan" [main (root-commit) 034cf38] Start garden plan 3 files changed, 6 insertions(+) create mode 100644 plan.md create mode 100644 plants.txt create mode 100644 tools.txt
Read the history with git log
git log lists commits newest first, with author, date and message. git log --oneline shows one line per commit: a short hash and the message. git log -n 3 limits the output, and git log -- file shows only commits that touched that file.
learner@shellstreak:~/garden$ git log --oneline 8958d5e (HEAD -> main) Add tools 579282f Add plant list e522d70 Start garden plan
See unstaged changes with git diff
git diff shows what you've changed but not staged yet. Lines starting with + were added and lines starting with - were removed.
learner@shellstreak:~/garden$ git diff diff --git a/plants.txt b/plants.txt index e40df41..0c8ef23 100644 --- a/plants.txt +++ b/plants.txt @@ -1,2 +1,3 @@ basil mint +parsley
See staged changes with git diff --staged
Once a change is staged, plain git diff no longer shows it. git diff --staged (also spelled --cached) shows exactly what the next commit will contain. Check it before every commit.
learner@shellstreak:~/garden$ git diff --staged diff --git a/plan.md b/plan.md index 803d325..0d74b73 100644 --- a/plan.md +++ b/plan.md @@ -1,2 +1,3 @@ # Garden plan Raised beds along the fence. +Compost bin by the shed.
Look at one commit with git show
git show prints the latest commit's details and its diff. Pass a hash or HEAD~1 to see an older one, and add --stat for a per-file summary instead of the full diff. To list only the names of the files a commit changed, compare it with its parent: git diff HEAD~1 HEAD --name-only.
learner@shellstreak:~/garden$ git show --stat commit 8958d5ee8e575a1a2aecd63e2c6f4510b06b2729 (HEAD -> main) Author: You <you@example.com> Date: Sun Mar 1 09:35:00 2026 +0000 Add tools tools.txt | 2 ++ 1 file changed, 2 insertions(+)
Write a commit message a reviewer can use
One commit, one change. Start with a short subject in the imperative (Add plant list, Fix watering time), about 50 characters or less, then a blank line and a body that explains why. The diff already says what changed.
Fix the last commit with --amend
git commit --amend -m "Better message" replaces the last commit's message (and adds anything you've staged since). It rewrites that commit, so only amend what you haven't pushed.
learner@shellstreak:~/garden$ git commit -q --amend -m "Add the tool list" && git log --oneline -n 2 b4e680d (HEAD -> main) Add the tool list 579282f Add plant list
Split a commit that does two things
git reset HEAD~1 undoes the last commit but keeps its changes in your files. Then git add one part, commit it, and repeat for the rest. Real git's git add -p goes further and lets you stage some changes inside a file, hunk by hunk.
Check yourself
One real exercise from the “Git basics” unit. No account needed.Stage every file in this folder, in one go.
What's checked: the repository's staged changes, untracked files.
Common mistakes
Committing without checking
git diff --stagedfirst, and shipping a debug line or a stray file.Running
git initinside a folder that's already part of a repository, creating a confusing nested repo.Expecting
git commit -ato include brand-new files. It only picks up files git already tracks.Writing messages like
fixorstuff. Six months later,git logis only as useful as your messages.
Questions
What's the staging area for?+
It lets you choose what goes into each commit. You can edit five files but commit two of them now and the rest separately.
What is HEAD?+
`HEAD` points to the commit you're on, usually the latest commit of the current branch. `HEAD~1` is the one before it.
Do I need GitHub to use git?+
No. Git works entirely on your computer. GitHub and similar services host a copy so you can share it and back it up.
Reading is step one. Now make it stick.
Practise this in a five-minute lesson with instant feedback. No account needed.