Version Control for Individual Developers
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 是一种分布式版本控制系统,用于跟踪源码变更。它帮助你保留完整的项目历史、与他人高效协作,并高效地管理代码版本。
A Git repository has three main areas: 一个 Git 仓库包含三大区域:
# 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 lets you work on features or experiments without affecting the main codebase. It is one of Git’s most powerful features. 分支能让你在不影响主分支的前提下开发新特性或进行实验,这是 Git 最强大的特性之一。
# 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
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
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
# 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
# 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 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
# 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
# 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