読み物Git

Level: Basic

Git Fundamentals: Commits, Staging, History, and .gitignore

The commands you'll use every day, with real output: status, add, commit, log, and diff, plus writing useful commit messages and keeping files out with .gitignore.

Edeeson Opina7 min read

A Basic lesson card: lesson 3 of the Git & Version Control series, Git Fundamentals: Commits, Staging, History, and .gitignore, with the Miso fox thinking.

What you’ll learn

  • Use status, diff, add, commit, and log in the daily cycle
  • Stage part of a file with git add -p
  • Write focused commits with clear messages
  • Ignore files with .gitignore and stop tracking files already committed
On this page
  1. Before you start
  2. The basic cycle
  3. Seeing where you are: git status
  4. Seeing what changed: git diff
  5. Choosing what to commit: git add
  6. Recording history: git commit
  7. Writing good commit messages
  8. Small, focused commits
  9. Looking back: git log
  10. Keeping files out: .gitignore
  11. Ignoring a file that's already tracked
  12. Common mistakes and troubleshooting
  13. Practical exercise: a clean first history
  14. Key takeaways
  15. References
  16. What's next

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:

  1. Edit files in the working tree.
  2. Review what changed with git status and git diff.
  3. Stage the changes you want with git add.
  4. Commit them with a message using git commit.
  5. Look back with git log when 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:

Shell
$ 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:

Shell
$ git status --short
M  soup.md
?? ramen.md

The 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:

Shell
$ 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:

Shell
git add soup.md          # one file
git add src/             # everything changed under a folder
git add -p               # choose individual changes interactively

git 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

Shell
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.
Text
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

Shell
$ git log --oneline
26142a7 Add wakame
c7a0a9b Add miso soup
123c8d2 Add README and .gitignore

The newest commit is at the top. Useful variations:

Shell
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 commit

Short 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/ or vendor/.
  • Build output such as dist/, build/, or target/.
  • Local configuration and secrets such as .env files with passwords or API keys.
  • Editor and system files such as .DS_Store or .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:

.gitignoreShell
# 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_Store

Pattern 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:

Shell
$ git check-ignore -v .env node_modules/x
.gitignore:2:.env	.env
.gitignore:1:node_modules/	node_modules/x

Ignoring 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:

Shell
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 commit without staging. Run git add first.
  • 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 .. Review git status and git diff --staged first, and set up .gitignore before the first commit.
  • .gitignore "not working". The file is already tracked; use git rm --cached.
  • Vague messages. Say what and why, in the imperative.
  • Wrong author on commits. Check git config user.email in that repository.

Practical exercise: a clean first history

In a new practice repository:

  1. Create .gitignore for a JavaScript project (dependencies, build output, .env), and commit it alone with a clear message.
  2. Create notes.md and .env with a fake value. Run git status and confirm .env doesn't appear.
  3. Edit notes.md in two unrelated places. Use git add -p to stage only one, commit it, then commit the other separately.
  4. Run git log --oneline and git show on each commit.
  5. Commit a file called secret.txt, then add it to .gitignore and use git rm --cached. Run git log --all -- secret.txt and 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 --staged shows it before you commit.
  • Write small, focused commits with imperative subject lines and a "why" in the body.
  • .gitignore keeps dependencies, build output, and secrets out, but only for untracked files.
  • Anything committed stays in history; a committed secret is a leaked secret.

References

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.

See every lesson in Git & Version Control: From Fundamentals to Advanced Workflows