Learn GitHub
$ gh ghfoundations.ghfundamentals

Git Fundamentals

6 learning objectives · 10 key terms · 9 commands · 3 diagrams · 5 quiz questions

$objectives

Git Fundamentals

Foundations

Module 1 of 5 · lesson 2 in this module

What you'll be able to do

  • Initialise a repository and read git status confidently
  • Stage changes deliberately and commit them as one logical unit
  • Write commit messages a colleague can understand six months later
  • Inspect history with git log and compare states with git diff
  • Rename and remove tracked files the Git-aware way
  • Keep generated and secret files out of history with .gitignore
$concept

The idea, in plain language

Almost all local Git work is one short loop: change files, choose what to record, record it, repeat. git status tells you where you are in that loop by sorting every file into one of three buckets — untracked (Git has never seen it), modified (tracked and changed since the last commit), and staged (queued for the next commit). Learning to read that output is more valuable than memorising flags, because it always tells you the next command to run.

The staging area is the part people skip, and skipping it is why their history is unreadable. If you always run git add . and commit, every commit becomes "whatever I had open at 5pm" — a mixture of a bug fix, a half-finished feature, a formatting pass and an accidental debug print. Staging lets you split that into three clean commits: stage the fixes, commit them with a message about the fix, then move on. A commit is not a save button; it is a paragraph in the story of the project.

git log and git diff are the two ways of reading that story. git log walks backwards through snapshots and answers "what happened, when and why". git diff compares two states and answers "what exactly is different". Everything else in this module — git rm, git mv, .gitignore — exists to keep your working directory tidy so that status, diff and log stay easy to read.

$key terms

Key terms

git init
Creates the .git directory, turning a folder into a repository.
Untracked
A file Git can see but is not yet recording — new files start here.
Tracked
A file Git already knows about, from a previous git add.
Staged / index
Changes queued for the next commit.
Commit hash
The unique SHA-1 identifier of a snapshot, shown abbreviated as seven characters or more.
HEAD
The commit currently checked out; your position in history.
git diff
Unstaged change report — working directory compared with the staging area.
git diff --staged
Staged change report — what git commit would record right now.
.gitignore
A list of paths Git should never track, such as build output, caches and .env files.
Working tree clean
No uncommitted changes — status has nothing to report.
$steps

How it works, step by step

  1. 1
    Initialise the repository

    Run this once per project, in the folder that should become the repository root. Re-running it in an existing repository is harmless — it simply reinitialises the metadata.

    once per projectbash
    git init
  2. 2
    Create or edit files as usual

    Write files with your normal editor. Git only notices them the next time you ask, so save your work first.

  3. 3
    Read git status

    The output is a to-do list. "Untracked files" means git add. "Changes not staged for commit" means git add. "Changes to be committed" means git commit.

    your most-used commandbash
    git status
  4. 4
    Stage exactly what belongs in this commit

    Stage a single file, a folder, or everything. Prefer naming files when the changes are unrelated — that is how one session becomes several readable commits.

    stagebash
    git add README.md
    # or everything in the current directory
    git add .
  5. 5
    Review before you commit

    git diff --staged shows precisely what the commit will contain. Thirty seconds here prevents the classic "I committed my API key" moment.

    reviewbash
    git diff --staged
  6. 6
    Commit with a message that explains why

    The subject line stays under about 50 characters and reads as an instruction. If you need more, leave a blank line and write a body explaining the reasoning.

    commitbash
    git commit -m "Fix off-by-one error in pagination"
    # or open an editor for a subject + body
    git commit
  7. 7
    Keep noise out with .gitignore

    Ignore build output, dependency folders, editor settings, logs and secrets. Commit the .gitignore file itself so the whole team gets the same rules.

    .gitignorebash
    node_modules/
    dist/
    .env
    *.log
    .DS_Store
$commands

Commands worth memorising

Every block below is copy-ready — use the Copy button, then paste it into your terminal. Expected output is shown underneath so you know what “working” looks like.

start a repositorybash
git init
Output
Initialized empty Git repository in /home/ada/recipe-book/.git/

Creates the hidden .git directory. The folder is now a repository.

what is going on?bash
git status
Output
On branch main
Changes to be committed:
  modified:   README.md
Changes not staged for commit:
  modified:   app.js

Always run this before staging or committing — it tells you the next step.

stage everythingbash
git add .

Stages every change under the current directory, including deletions and new files.

commitbash
git commit -m "Add initial project files"
Output
[main 8c1d2ab] Add initial project files
 3 files changed, 42 insertions(+)

Records the staged snapshot. The bracketed value is the branch and the new short hash.

historybash
git log --oneline --graph --decorate
Output
* 8c1d2ab (HEAD -> main) Add initial project files
* 4f1c2ab Add project README

One line per commit with the branch/HEAD labels. Drop --oneline for full detail.

what changed?bash
git diff
Output
diff --git a/app.js b/app.js
@@ -1,4 +1,4 @@
-console.log("hello")
+console.log("hello, world")

Unstaged changes only. Add --staged to review what the next commit would capture.

rename a tracked filebash
git mv old-name.js new-name.js

Renames the file and stages the rename in one command — clearer than a manual mv plus git add.

remove a tracked filebash
git rm notes.txt

Deletes the file and stages the deletion. Use --cached to stop tracking it while keeping it on disk.

who changed that line?bash
git blame app.js
Output
8c1d2ab (Ada Lovelace 2026-03-04 10:12:07 +0000 12) console.log("hello, world")

Shows the last commit that touched each line — the fastest route to the message that explains it.

$diagrams

Workflow diagrams

Diagram 1The local commit loop

Everything in this module moves along these two arrows. Push only enters the picture in Module 3.

Reading the diagram
  1. Files you edit sit in the working directory; git status reports them as modified or untracked.
  2. git add copies the current content of those files into the staging area.
  3. git commit wraps the staged content into a snapshot and moves HEAD forward to it.
  4. The working directory is never emptied by committing — it stays exactly as you left it.
Diagram 2Reading git status like a checklist

Whatever status prints, there is exactly one sensible next command.

Reading the diagram
  1. git status first — never guess what is pending.
  2. Untracked or unstaged changes: stage them with git add, naming files when the changes are unrelated.
  3. Review with git diff --staged, then commit with a message explaining the intent.
  4. If status reports a clean working tree, there is nothing to record and you can move on.
Diagram 3History versus difference

One command answers "what happened", the others answer "what is different right now".

Reading the diagram
  1. Use git log to find the commit; use git show to read it.
  2. Use git diff before staging to check your work; git diff --staged before committing to check the snapshot.
$example

Worked example

One small feature, split into two commits the way you would in a real project. Read the status output between every step.

a tidy two-commit sessionbash
git status
# On branch main
# nothing to commit, working tree clean

printf 'def add(a, b):\n    return a + b\n' > calc.py
printf 'node_modules/\n__pycache__/\n' > .gitignore

git status
# Untracked files:
#   .gitignore
#   calc.py

git add .gitignore
git commit -m "Ignore dependencies and Python cache"
# [main 1a2b3c4] Ignore dependencies and Python cache

git add calc.py
git diff --staged
# +def add(a, b):
# +    return a + b

git commit -m "Add add() helper"
# [main 5d6e7f8] Add add() helper

git log --oneline --graph
# * 5d6e7f8 (HEAD -> main) Add add() helper
# * 1a2b3c4 Ignore dependencies and Python cache
Output
Two commits, each with a single purpose, and a clean working tree at the end.
1The session starts with a clean tree — git status has nothing to report, so we know any change from here is ours.
2Two new files appear. Both show as untracked: Git can see them but is not yet recording their history.
3The .gitignore is committed first, on its own, because ignoring files is a separate idea from adding a function. One idea, one commit.
4git add calc.py stages only that file, leaving nothing else queued.
5git diff --staged prints exactly what the next commit will contain — two added lines, nothing else. This is the last chance to catch a mistake cheaply.
6The second commit records the helper. git log --oneline --graph shows the two commits in order, with HEAD -> main on the newest.
$use cases

Where you'll use it

▸Reconstructing why a line exists by reading its commit message with git blame.
▸Undoing one bad commit without throwing away a day of unrelated work.
▸Splitting a messy afternoon of edits into three reviewable commits before pushing.
▸Keeping build output, caches and editor settings out of the repository entirely.
▸Reviewing your own work in git diff --staged before anyone else sees it.
▸Producing a changelog straight from commit history at release time.
$mistakes

Common mistakes

  • Committing everything with git add . regardless of what changed, producing commits nobody can describe.
  • Writing commit messages like "fix", "update" or "asdf" — the message is the only explanation future-you gets.
  • Committing a .env file or credentials because .gitignore was never created.
  • Expecting .gitignore to stop tracking a file that is already committed — it only affects untracked files.
  • Deleting a file with rm and forgetting to stage the deletion, so the commit misses it.
  • Treating git commit as a save button and committing every few minutes with meaningless messages.
$best practices

Best practices

  • Run git status before and git diff --staged after staging — two seconds each, saves a lot of rework.
  • One commit, one idea: a bug fix and a refactor belong in separate commits even if you did them in the same hour.
  • Subject line in the imperative, under about 50 characters; put the reasoning in the body, not the code.
  • Create .gitignore before the first commit, and commit it so the rules travel with the project.
  • Stage files by name when changes are unrelated; reserve git add . for genuinely cohesive work.
  • Never commit generated files — if it can be rebuilt, it does not belong in history.
$exercise

Hands-on exercise

Build a commit history you would be proud to show

Practise the local loop on a throwaway project and produce three clean commits.

  1. Create a folder, run git init inside it, and confirm git status reports an empty repository.
  2. Create a .gitignore that excludes a logs/ directory and any .env file, then commit it alone with the message "Add gitignore for logs and env files".
  3. Add two or three small files (a README and one script is plenty) and commit them with a descriptive subject line.
  4. Modify one file, then run git diff to see the unstaged change and git diff --staged to see that nothing is staged yet — explain the difference to yourself in one sentence.
  5. Stage the modified file, run git status to confirm it moved into "Changes to be committed", then commit it.
  6. Add a file called secrets.env, confirm git status does not list it, and write one sentence explaining why.

💡 Hint: If a file you expected to ignore still appears in status, check the spelling and the path in .gitignore, and remember that already-tracked files must be removed with git rm --cached first.

Done when: git log --oneline shows three commits with meaningful messages, and git status is clean apart from files you deliberately ignored.

$quiz

Checkpoint quiz

0 of 5 answered · 0 correct

  1. Multiple choiceQ1

    Which command stages every change under the current directory?

  2. Command outputQ2

    What does this report?

    git diff --staged
  3. True or falseQ3

    True or false: adding a pattern to .gitignore stops Git tracking a file that was already committed.

  4. TroubleshootingQ4

    You deleted stale.txt with your file manager and committed. Reviewers say the deletion is missing from the commit. Why?

  5. Multiple choiceQ5

    What belongs in a commit message subject line?

$summary

Module summary

  • The local loop is edit, git status, git add, git diff --staged, git commit.
  • git status sorts changes into untracked, unstaged and staged — it tells you the next command to run.
  • The staging area is what turns a messy session into several clean, reviewable commits.
  • git log reads history; git diff compares states; git show and git blame go deeper into one commit or one line.
  • Use git mv and git rm so renames and deletions are staged consistently.
  • .gitignore keeps build output, caches and secrets out of history from the start.

Next up: Your history is now excellent and entirely local. Module 3 puts it on GitHub: creating a repository, connecting the one you already have, and understanding exactly what pulls, pushes and fetches do.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Put the GitHub course to work: open one of the company-wise repositories and follow the workflow you just learned, branch to release.