Git Fundamentals
6 learning objectives · 10 key terms · 9 commands · 3 diagrams · 5 quiz questions
Git Fundamentals
FoundationsWhat 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
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
- 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.
How it works, step by step
- 1Initialise 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 projectbashgit init
- 2Create 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.
- 3Read 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 commandbashgit status
- 4Stage 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.
stagebashgit add README.md # or everything in the current directory git add . - 5Review 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.
reviewbashgit diff --staged - 6Commit 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.
commitbashgit commit -m "Fix off-by-one error in pagination" # or open an editor for a subject + body git commit
- 7Keep 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.
.gitignorebashnode_modules/ dist/ .env *.log .DS_Store
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.
git init
Initialized empty Git repository in /home/ada/recipe-book/.git/
Creates the hidden .git directory. The folder is now a repository.
git status
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.
git add .
Stages every change under the current directory, including deletions and new files.
git commit -m "Add initial project files"
[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.
git log --oneline --graph --decorate
* 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.
git diff
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.
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.
git rm notes.txt
Deletes the file and stages the deletion. Use --cached to stop tracking it while keeping it on disk.
git blame app.js
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.
Workflow diagrams
git add .Local repositorygit commitgit commit -m "..."Everything in this module moves along these two arrows. Push only enters the picture in Module 3.
- Files you edit sit in the working directory; git status reports them as modified or untracked.
- git add copies the current content of those files into the staging area.
- git commit wraps the staged content into a snapshot and moves HEAD forward to it.
- The working directory is never emptied by committing — it stays exactly as you left it.
Whatever status prints, there is exactly one sensible next command.
- git status first — never guess what is pending.
- Untracked or unstaged changes: stage them with git add, naming files when the changes are unrelated.
- Review with git diff --staged, then commit with a message explaining the intent.
- If status reports a clean working tree, there is nothing to record and you can move on.
| Command | Compares | Answers |
|---|---|---|
| git log | Commits over time | What happened, when, in what order, and why |
| git show | One commit against its parent | What exactly did this individual commit change |
| git diff | Working directory against staging area | What have I changed but not staged yet |
| git diff --staged | Staging area against last commit | What will this commit contain |
| git blame | Each line against its last commit | Which commit and author introduced this line |
One command answers "what happened", the others answer "what is different right now".
- Use git log to find the commit; use git show to read it.
- Use git diff before staging to check your work; git diff --staged before committing to check the snapshot.
Worked example
One small feature, split into two commits the way you would in a real project. Read the status output between every step.
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
Two commits, each with a single purpose, and a clean working tree at the end.
Where you'll use it
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
- 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.
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.
- Create a folder, run git init inside it, and confirm git status reports an empty repository.
- 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".
- Add two or three small files (a README and one script is plenty) and commit them with a descriptive subject line.
- 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.
- Stage the modified file, run git status to confirm it moved into "Changes to be committed", then commit it.
- 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.
Checkpoint quiz
0 of 5 answered · 0 correct
- Multiple choiceQ1
Which command stages every change under the current directory?
- Command outputQ2
What does this report?
git diff --staged
- True or falseQ3
True or false: adding a pattern to .gitignore stops Git tracking a file that was already committed.
- TroubleshootingQ4
You deleted stale.txt with your file manager and committed. Reviewers say the deletion is missing from the commit. Why?
- Multiple choiceQ5
What belongs in a commit message subject line?
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.
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
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.