1.3 Git - Solo UsageGit - 个人使用

Version Control for Individual Developers

🌿 What is Git?

Git is a distributed version control system that tracks changes in source code. It lets you keep a full project history, collaborate effectively, and manage versions efficiently. Git 是一种分布式版本控制系统,用于跟踪源码变更。它帮助你保留完整的项目历史、与他人高效协作,并高效地管理代码版本。

graph TD A[Git Features] --> B[Version Control] A --> C[Branching & Merging] A --> D[Distributed] A --> E[Speed & Efficiency] B --> B1[Track changes] B --> B2[Revert to previous versions] B --> B3[Compare versions] C --> C1[Work on features independently] C --> C2[Merge changes safely] D --> D1[Full local repository] D --> D2[Work offline] D --> D3[Multiple backups] style A fill:#e07a5f,stroke:#333,stroke-width:2px style B fill:#81b29a,stroke:#333,stroke-width:2px style C fill:#f2cc8f,stroke:#333,stroke-width:2px style D fill:#81b29a,stroke:#333,stroke-width:2px

📚 Git Basics

Repository Structure

A Git repository has three main areas: 一个 Git 仓库包含三大区域:

graph LR A[Working Directory] -->|git add| B[Staging Area] B -->|git commit| C[Local Repository] A --> A1[Your files] A --> A2[Current state] B --> B1[Prepared changes] B --> B2[Ready to commit] C --> C1[Committed changes] C --> C2[Project history] style A fill:#e07a5f,stroke:#333,stroke-width:2px style B fill:#81b29a,stroke:#333,stroke-width:2px style C fill:#f2cc8f,stroke:#333,stroke-width:2px

Basic Git Commands

# Initialize a new repository
git init

# Check repository status
git status

# Add files to staging area
git add filename.txt
git add .                    # Add all files
git add *.html               # Add all HTML files

# Commit changes
git commit -m "Your commit message"

# View commit history
git log
git log --oneline           # Compact view
git log --graph             # Visual branch structure

# View differences
git diff                    # Show unstaged changes
git diff --staged          # Show staged changes

🌿 Branching Strategy

Branching lets you work on features or experiments without affecting the main codebase. It is one of Git’s most powerful features. 分支能让你在不影响主分支的前提下开发新特性或进行实验,这是 Git 最强大的特性之一。

graph TD A[main] --> B[feature-branch] A --> C[bugfix-branch] B --> D[Add new feature] C --> E[Fix critical bug] D --> F[Testing] E --> F F --> G[Merge back to main] style A fill:#e07a5f,stroke:#333,stroke-width:2px style B fill:#81b29a,stroke:#333,stroke-width:2px style C fill:#f2cc8f,stroke:#333,stroke-width:2px style G fill:#81b29a,stroke:#333,stroke-width:2px

Branch Commands

# Create a new branch
git branch feature-login

# Switch to a branch
git checkout feature-login
# OR (modern syntax)
git switch feature-login

# Create and switch to branch in one command
git checkout -b feature-login
# OR
git switch -c feature-login

# List branches
git branch
git branch -a              # Show all branches (including remote)

# Delete a branch
git branch -d feature-login    # Safe delete (merged)
git branch -D feature-login    # Force delete (unmerged)

# Merge a branch
git checkout main
git merge feature-login

🔄 Practical Solo Workflow

Here’s a typical workflow for a solo developer working on multiple features: 以下是单人项目开发的常见工作流示例(含多个功能迭代):

# 1. Initialize or clone repository
git init
# OR
git clone https://github.com/username/project.git

# 2. Create main branch if not exists
git checkout -b main

# 3. Start working on a new feature
git checkout -b feature-user-authentication

# 4. Make changes and commit
# ... edit files ...
git add .
git commit -m "Add user authentication structure"

# 5. Continue working on the feature
# ... edit more files ...
git add .
git commit -m "Implement login functionality"

# 6. Switch back to main branch
git checkout main

# 7. Merge the feature branch
git merge feature-user-authentication

# 8. Delete the feature branch
git branch -d feature-user-authentication

# 9. Push to remote repository
git push origin main

⚙️ Git Configuration

Before using Git, configure your identity and basic preferences: 开始使用 Git 之前,先配置个人信息与常用偏好:

# Set your name and email
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# Set default editor
git config --global core.editor "code --wait"  # VS Code
# OR
git config --global core.editor "vim"         # Vim

# Set default branch name
git config --global init.defaultBranch main

# Enable colored output
git config --global color.ui true

# Set up SSH key for GitHub
ssh-keygen -t ed25519 -C "your.email@example.com"
# Then add the public key to your GitHub account

# View configuration
git config --list
git config --global --list

🔧 Common Operations

Undoing Changes

# Undo changes in working directory
git checkout -- filename.txt

# Unstage files
git reset HEAD filename.txt

# Amend last commit
git commit --amend

# Revert a commit (creates new commit that undoes changes)
git revert commit-hash

# Reset to previous commit
git reset --soft HEAD~1    # Keep changes staged
git reset --hard HEAD~1    # Discard all changes

Viewing History

# Show commit history
git log

# Show compact history
git log --oneline

# Show history with graph
git log --oneline --graph --all

# Show changes in a commit
git show commit-hash

# Compare commits
git diff commit1..commit2

# Show file history
git log --follow filename.txt

🌐 Remote Repositories

Remote repositories allow you to backup your code and collaborate with others. GitHub, GitLab, and Bitbucket are popular hosting services.

# Add remote repository
git remote add origin https://github.com/username/project.git

# View remotes
git remote -v

# Push to remote
git push origin main

# Pull from remote
git pull origin main

# Clone repository
git clone https://github.com/username/project.git

# Fetch changes without merging
git fetch origin

# Set upstream branch
git push -u origin main

✅ Best Practices

Commit Messages

  • Use present tense ("Add feature" not "Added feature")
  • Be descriptive but concise (50-72 characters)
  • Start with capital letter
  • No period at the end
  • Include issue numbers when applicable

Branch Management

  • Use descriptive branch names
  • Keep branches focused on single features
  • Regularly merge main branch into feature branches
  • Delete merged branches to keep repository clean
  • Use feature/ prefix for feature branches

Example Good Practices

# Good commit messages
git commit -m "Add user authentication system"
git commit -m "Fix login validation bug"
git commit -m "Update README with installation instructions"
git commit -m "Refactor database connection logic"

# Good branch names
git checkout -b feature/user-login
git checkout -b bugfix/password-validation
git checkout -b hotfix/critical-security-patch
git checkout -b experiment/new-ui-design

🔧 Troubleshooting Common Issues

Common Problems and Solutions

# Problem: Accidentally committed to wrong branch
git reset --soft HEAD~1           # Undo commit but keep changes
git stash                         # Stash changes
git checkout correct-branch       # Switch to correct branch
git stash pop                     # Apply stashed changes
git commit -m "Correct commit"    # Commit on correct branch

# Problem: Merge conflict
git pull origin main
# Resolve conflicts in files
git add .
git commit -m "Resolve merge conflicts"

# Problem: Want to undo a pushed commit
git revert commit-hash           # Create new commit that undoes changes
git push origin main

# Problem: Repository is in detached HEAD state
git checkout main               # Switch back to a branch
git branch new-branch-name     # Create branch from current state

📋 Summary总结

💭 Thinking Thinking & Practice Practice思考与实践