Git Basic Operations
This chapter introduces the basic Git commands, which are the most frequently used commands in daily Git usage. Mastering these is the foundation of using Git.
File Status Management
Check Repository Status
bash
# Check current repository status
git status
# Check concise status
git status -s
# Or
git status --shortStatus output explanation:
M modified_file.txt # Modified and staged
M modified_file2.txt # Modified but not staged
A new_file.txt # New file staged
D deleted_file.txt # Deleted and staged
R renamed_file.txt # Renamed
C copied_file.txt # Copied
U unmerged_file.txt # Unmerged (Conflict)
?? untracked_file.txt # Untracked file
!! ignored_file.txt # Ignored fileAdd Files to Staging Area
bash
# Add a single file
git add filename.txt
# Add multiple files
git add file1.txt file2.txt file3.txt
# Add all files in current directory
git add .
# Add all modifications to tracked files
git add -u
# Add all files (including new and modified)
git add -A
# Interactive add
git add -i
# Partial add (patch mode)
git add -p filename.txtPractical Example
bash
# Create demo project
mkdir git-basic-ops
cd git-basic-ops
git init
# Create some files
echo "Main program" > main.py
echo "Config file" > config.json
echo "Documentation" > README.md
mkdir utils
echo "Helper functions" > utils/helpers.py
# Check status
git status
# Add files
git add main.py config.json
git status
# Add directory
git add utils/
git status
# Add remaining files
git add README.md
git statusCommit Operations
Basic Commit
bash
# Commit staged content
git commit -m "Commit message"
# Commit and add all modifications to tracked files
git commit -am "Commit message"
# Empty commit (used to trigger CI/CD)
git commit --allow-empty -m "Trigger build"Commit Message Best Practices
bash
# ✅ Good commit messages
git commit -m "feat: Add user login feature"
git commit -m "fix: Fix password validation error"
git commit -m "docs: Update API documentation"
git commit -m "style: Fix code formatting"
git commit -m "refactor: Refactor user service module"
# ❌ Bad commit messages
git commit -m "Modify"
git commit -m "Update"
git commit -m "bug fix"Modify Commit
bash
# Modify the last commit message
git commit --amend -m "New commit message"
# Modify the last commit content (add missed files)
git add forgotten_file.txt
git commit --amend --no-edit
# Interactive commit modification
git commit --amendCommit Operation Example
bash
# Continue from previous example
git commit -m "Initial commit: Add basic project files"
# Modify files
echo "Updated main program" >> main.py
echo "New helper function" >> utils/helpers.py
# Check changes
git status
git diff
# Commit changes
git add main.py utils/helpers.py
git commit -m "feat: Update main program and helpers"
# Realized config file change was missed
echo "New config item" >> config.json
git add config.json
git commit --amend --no-editView Changes
View Working Directory Changes
bash
# View differences between working directory and staging area
git diff
# View changes of a specific file
git diff filename.txt
# View differences between staging area and last commit
git diff --staged
# Or
git diff --cached
# View differences between working directory and last commit
git diff HEAD
# View differences between two commits
git diff commit1 commit2
# View differences of a specific file between two commits
git diff commit1 commit2 -- filename.txtInterpreting Diff Output
diff
diff --git a/main.py b/main.py
index 1234567..abcdefg 100644
--- a/main.py
+++ b/main.py
@@ -1,3 +1,4 @@
def main():
print("Hello, World!")
+ print("New feature")
return 0Explanation:
---represents old file+++represents new file@@ -1,3 +1,4 @@represents line number range of changes