A handful of commands make up most of daily Git use: status, add, commit, log, and diff. This lesson explains each one through a small recipe project, shows real output so you know what to expect, and covers the .gitignore file that keeps dependencies, build output, and secrets out of your history. You'll also learn what makes a commit useful to your future self and your teammates.
Before you start
You should have Git installed and configured, from Getting Started with Git, and know the three areas from lesson 1: the working tree, the staging area, and the repository. Create a practice repository to follow along.
The basic cycle
Nearly all Git work follows the same loop:
- Edit files in the working tree.
- Review what changed with
git statusandgit diff. - Stage the changes you want with
git add. - Commit them with a message using
git commit. - Look back with
git logwhen you need to.
Seeing where you are: git status
git status is the command to run whenever you're unsure. Here, one tracked file has been edited and a new file has been created:
$ git status
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: soup.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
ramen.md
no changes added to commit (use "git add" and/or "git commit -a")Read it top to bottom: which branch you're on, what's changed but not staged, and which files Git isn't tracking yet. Git even suggests the next commands. For a compact view, use git status --short:
$ git status --short
M soup.md
?? ramen.mdThe two columns are the staging area (left) and the working tree (right). M in the left column means a staged modification, ?? means untracked, A means a new file staged, and UU (lesson 4) means a merge conflict.
Seeing what changed: git diff
git diff shows changes in the working tree that aren't staged yet. After staging, those changes move, and git diff --staged shows what will go into the next commit:
$ git diff --staged
diff --git a/soup.md b/soup.md
index 44616a6..49db6a3 100644
--- a/soup.md
+++ b/soup.md
@@ -1,2 +1,3 @@
Miso soup
-with wakame
+with tofu and wakame
+Serve hot.Lines starting with - were removed, lines with + were added, and unmarked lines are context. The @@ -1,2 +1,3 @@ header says the change covers lines 1 to 2 of the old file and lines 1 to 3 of the new one.
A good habit: always run git diff --staged before committing. It catches debugging code, stray edits, and files you didn't mean to include.
Choosing what to commit: git add
git add copies the current state of a file into the staging area:
git add soup.md # one file
git add src/ # everything changed under a folder
git add -p # choose individual changes interactivelygit add -p (patch mode) shows each block of changes and asks whether to stage it. It's the tool for splitting unrelated edits into separate commits, for example a typo fix and a new feature you edited in the same file.
Staging records a file as it is at that moment. If you edit it again after git add, the new edits aren't staged until you add again; git status will show the file as both staged and modified.
Avoid git add . or git add -A without checking git status first. They stage everything, including files you meant to leave out.
Recording history: git commit
git commit -m "Add tofu to the miso soup recipe"Without -m, Git opens your editor for a longer message. A commit captures exactly what's staged, not everything in your working tree.
Writing good commit messages
History is only useful if you can understand it later. A common convention:
- A short subject line (around 50 characters, no trailing period) that says what the commit does, in the imperative: "Add", "Fix", "Remove", not "Added" or "Fixes".
- A blank line.
- An optional body explaining why the change was needed and anything non-obvious, wrapped at about 72 characters.
Fix total when a cart has only free items
The total skipped rounding when every item cost zero, so the
checkout page showed "-0.00". Round in every case and add a test.Messages like "update", "fix stuff", or "wip" force every future reader to open the diff to find out what happened.
Small, focused commits
Each commit should be one logical change that you could describe in one sentence. Small commits are easier to review, easier to revert if something goes wrong (lesson 9), and make tools like git bisect (lesson 13) far more useful. If your message needs the word "and" to join two unrelated changes, consider two commits.
Looking back: git log
$ git log --oneline
26142a7 Add wakame
c7a0a9b Add miso soup
123c8d2 Add README and .gitignoreThe newest commit is at the top. Useful variations:
git log # full details: ID, author, date, message
git log -p soup.md # every change to one file, with diffs
git log --since="2 weeks ago" # recent history
git log --author="Ada" # one person's commits
git log --oneline --graph --all # branches as a graph (lesson 4)
git show c7a0a9b # one commit and its changes
git blame soup.md # who last changed each line, and in which commitShort IDs like c7a0a9b are abbreviations of the full 40-character hash. Git accepts any unambiguous prefix.
Keeping files out: .gitignore
Some files should never be committed:
- Dependencies that can be reinstalled, such as
node_modules/orvendor/. - Build output such as
dist/,build/, ortarget/. - Local configuration and secrets such as
.envfiles with passwords or API keys. - Editor and system files such as
.DS_Storeor.idea/. - Logs and caches.
List patterns in a .gitignore file at the root of the repository, and commit it so everyone shares the same rules:
# Dependencies
node_modules/
# Build output
dist/
# Environment files with secrets; keep an example file instead
.env
.env.*
!.env.example
# Editors and operating systems
.idea/
.DS_StorePattern rules worth knowing: a trailing / matches only directories, * matches anything except /, ** matches across directories, and a leading ! re-includes something an earlier pattern excluded. A leading / anchors the pattern to the folder of the .gitignore file.
To see which rule ignores a file, ask Git:
$ git check-ignore -v .env node_modules/x
.gitignore:2:.env .env
.gitignore:1:node_modules/ node_modules/xIgnoring a file that's already tracked
.gitignore only affects untracked files. If a file was committed before you added the rule, Git keeps tracking it. Remove it from tracking (but keep it on disk) and commit:
git rm --cached .env
git commit -m "Stop tracking .env"If that file contained a secret, removing it now doesn't remove it from history: the secret is still in older commits and in every clone. Treat it as leaked, revoke it, and issue a new one. Lesson 12 covers cleaning history.
Common mistakes and troubleshooting
- "nothing added to commit but untracked files present." You ran
git commitwithout staging. Rungit addfirst. - A change missing from the commit. You edited after staging. Check
git status, add again, then commit; to fix the last commit, see lesson 9. - Committing secrets or huge files with
git add .. Reviewgit statusandgit diff --stagedfirst, and set up.gitignorebefore the first commit. .gitignore"not working". The file is already tracked; usegit rm --cached.- Vague messages. Say what and why, in the imperative.
- Wrong author on commits. Check
git config user.emailin that repository.
Practical exercise: a clean first history
In a new practice repository:
- Create
.gitignorefor a JavaScript project (dependencies, build output,.env), and commit it alone with a clear message. - Create
notes.mdand.envwith a fake value. Rungit statusand confirm.envdoesn't appear. - Edit
notes.mdin two unrelated places. Usegit add -pto stage only one, commit it, then commit the other separately. - Run
git log --onelineandgit showon each commit. - Commit a file called
secret.txt, then add it to.gitignoreand usegit rm --cached. Rungit log --all -- secret.txtand explain why the file is still in history.
Key takeaways
- The daily loop is edit, review (
status,diff), stage (add), commit, and look back (log). - A commit contains exactly what's staged;
git diff --stagedshows it before you commit. - Write small, focused commits with imperative subject lines and a "why" in the body.
.gitignorekeeps dependencies, build output, and secrets out, but only for untracked files.- Anything committed stays in history; a committed secret is a leaked secret.
References
- Pro Git: Recording Changes to the Repository (opens in a new tab)
- Pro Git: Viewing the Commit History (opens in a new tab)
- Git documentation: gitignore (opens in a new tab)
- Git documentation: git add (opens in a new tab)
- Git documentation: git log (opens in a new tab)
- GitHub: gitignore templates (opens in a new tab)
What's next
So far, history has been a straight line. Understanding Git Branches, Merging, and Merge Conflicts shows how to work on several lines of development at once and combine them.

