Common Git Commands Explained
🎯 Introduction
Git is essential for managing code and collaborating on frontend projects. Reviewing changes, undoing mistakes, ignoring files, and writing clear commit messages all help teams work more efficiently. This article explains common Git commands through practical frontend examples to help you get comfortable with version control.
🔍 Inspecting changes: git status and git diff
During development, you need to know the current state of your code. Two fundamental commands are git status and git diff.
git status: shows the repository's current state, including modified, staged, and untracked files. It helps you decide what to do next.git diff: compares changes in your code. By default, it shows differences between the working tree and the staging area. You can also use it to compare two commits.
Example
git status
You will see output similar to:
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
modified: index.js
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
git diff
This shows unstaged changes in index.js so you can review the edits.
git diff --staged to view changes that have been staged but not yet committed.🔄 Undoing changes: restoring files, discarding edits, and undoing commits
Mistakes happen. Git provides several ways to undo changes and recover your code.
1. Discard unstaged changes in the working tree
git checkout -- filename
This restores the file from the staging area, discarding its unstaged changes. If the file has no staged changes, this restores its state from the last commit.
2. Unstage changes
git reset HEAD filename
This unstages the file while preserving its changes in the working tree.
3. Delete newly created untracked files
git clean -f
This deletes newly created, untracked files from the working tree, which can be useful for removing temporary files.
git clean cannot be recovered through Git. Use it carefully!4. Undo commits
- Use
git resetto return to a specific commit.
git reset --hard commit_id
This resets the current branch to the specified commit, discarding subsequent commits from that branch's history and local changes to tracked files.
- Use
git revertto create a new commit that reverses an earlier commit (a safer option).
git revert commit_id
🚫 Ignoring node_modules: writing a .gitignore file
The node_modules folder is usually large, and dependencies can be reinstalled from package.json, so you should not commit it.
Example .gitignore for a frontend project
# Ignore node_modules
node_modules/
# Ignore log files
*.log
# Ignore build output
dist/
build/
# Ignore operating system files
.DS_Store
Thumbs.db
git rm --cached filename to stop tracking it before the ignore rule can take effect.📝 Writing clear commit messages (frontend examples)
Good commit messages improve collaboration and make history and code reviews easier to follow. A recommended format is:
<type>(<scope>): <short description>
<detailed description, possibly spanning multiple lines>
Common types
- feat: a new feature
- fix: a bug fix
- docs: documentation changes
- style: formatting changes (spaces, semicolons, and similar changes that do not affect behavior)
- refactor: code restructuring
- test: added or updated tests
- chore: changes to the build process or supporting tools
Frontend examples
feat(login): add WeChat login
- Integrate the WeChat SDK
- Redirect to the home page after successful login
fix(api): handle empty user data responses
Check for null values when the API returns no data to prevent page errors
❓ Knowledge check
Q1: What is the difference between git diff and git diff --staged?
git diff shows differences between the working tree and the staging area. git diff --staged shows differences between the staging area and the last commit.
Q2: How can I undo a commit?
Use git revert to create a new commit that reverses earlier changes, or use git reset --hard to return to a specific commit. The latter removes subsequent commits from the branch's history, so use it carefully.
Q3: Why ignore node_modules?
node_modules is large and can be reinstalled from package.json. Committing it increases repository size and makes maintenance harder.
🧪 Exercises
Exercise 1: Inspect current changes
Use these commands to inspect the working tree and staging area and identify modified files.
git status
git diff
Exercise 2: Ignore build output
Create a .gitignore file at the project root that ignores dist and .log files.
dist/
*.log
🎉 You now know how to inspect changes, undo operations, ignore files, and write clear commit messages! These skills help you manage code efficiently and collaborate more effectively. Keep practicing to become more confident with Git.
