| name | git |
| description | Git 版本控制操作与最佳实践。Use when (1) 初始化或配置 Git 仓库, (2) 执行 commit/push/pull 等操作, (3) 处理分支和合并冲突, (4) 配置 .gitignore, (5) 解决 Git 相关问题 |
Git Version Control
Objectives
- Initialize and configure Git repositories
- Execute common Git operations (commit, push, pull, branch, merge)
- Handle merge conflicts and resolve issues
- Configure .gitignore and Git workflows
- Follow Git best practices and conventions
Initial Setup
Check Git Status
Before any Git operation, check the current state:
git status
git remote -v
git branch -a
git log --oneline -10
Initialize Repository
git init
git remote add origin <repository-url>
git remote -v
Clone Existing Repository
git clone https://github.com/user/repo.git
git clone git@github.com:user/repo.git
git clone -b branch-name https://github.com/user/repo.git
Configuration
User Identity
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
git config user.name "Your Name"
git config user.email "your.email@example.com"
git config --list
Common Settings
git config --global init.defaultBranch main
git config --global core.autocrlf true
git config --global core.autocrlf input
git config --global core.editor "code --wait"
git config --global credential.helper cache
Basic Workflow
1. Check Status
git status
2. Stage Changes
git add file1.py file2.py
git add .
git add *.py
git add -p
3. Commit Changes
git commit -m "feat: add user authentication"
git commit -m "feat: add user authentication" -m "- Implement JWT token generation
- Add login/logout endpoints
- Create user session management"
git commit --amend -m "Updated message"
git commit -am "fix: resolve login bug"
4. Push Changes
git push origin main
git push -u origin feature-branch
git push --force-with-lease origin main
5. Pull Changes
git pull origin main
git pull --rebase origin main
git fetch origin
Branch Management
Create and Switch Branches
git branch feature-name
git checkout feature-name
git checkout -b feature-name
git switch feature-name
git switch -c feature-name
List and Delete Branches
git branch
git branch -a
git branch -d feature-name
git branch -D feature-name
git push origin --delete feature-name
Merge Branches
git merge feature-name
git merge --no-ff feature-name
git merge --abort
Rebase
git rebase main
git rebase -i HEAD~3
git rebase --continue
git rebase --abort
Conflict Resolution
When Conflicts Occur
-
Identify conflicted files:
git status
-
Open conflicted files and look for conflict markers:
<<<<<<< HEAD
Your changes
=======
Incoming changes
>>>>>>> branch-name
-
Resolve conflicts by editing the file
-
Stage resolved files:
git add resolved-file.py
-
Complete the merge/rebase:
git commit
git rebase --continue
Undoing Changes
Discard Local Changes
git checkout -- file.py
git reset --hard HEAD
git clean -fd
Unstage Files
git reset HEAD file.py
git reset HEAD
Revert Commits
git revert HEAD
git revert <commit-hash>
git reset --hard HEAD~1
git reset --soft HEAD~1
.gitignore Configuration
Common Patterns
# Python
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
env/
venv/
.venv/
*.egg-info/
dist/
build/
# IDEs
.vscode/
.idea/
*.swp
*.swo
*~
# OS
.DS_Store
Thumbs.db
# Environment
.env
.env.local
*.log
# Dependencies
node_modules/
package-lock.json
# Build outputs
dist/
build/
*.min.js
*.min.css
Apply .gitignore to Existing Files
git rm -r --cached .
git add .
git commit -m "chore: apply .gitignore rules"
Commit Message Conventions
Format
<type>(<scope>): <subject>
<body>
<footer>
Types
- feat: New feature
- fix: Bug fix
- docs: Documentation changes
- style: Code style changes (formatting, no logic change)
- refactor: Code refactoring
- test: Adding or updating tests
- chore: Maintenance tasks (dependencies, config)
- perf: Performance improvements
Examples
git commit -m "feat(auth): add JWT authentication"
git commit -m "fix(api): resolve null pointer in user endpoint"
git commit -m "docs: update installation instructions"
git commit -m "refactor(utils): simplify date formatting logic"
Common Issues and Solutions
Issue: Not a Git Repository
git init
Issue: Remote Already Exists
git remote remove origin
git remote add origin <new-url>
Issue: Diverged Branches
git pull origin main
git pull --rebase origin main
Issue: Detached HEAD
git checkout -b recovery-branch
git checkout main
Issue: Large Files
git lfs install
git lfs track "*.psd"
git add .gitattributes
Git Workflows
Feature Branch Workflow
- Create feature branch from main
- Make changes and commit
- Push feature branch
- Create pull request
- Review and merge
- Delete feature branch
git checkout main
git pull origin main
git checkout -b feature/new-feature
git add .
git commit -m "feat: implement new feature"
git push -u origin feature/new-feature
Gitflow Workflow
Branches:
- main: Production-ready code
- develop: Integration branch
- feature/*: New features
- release/*: Release preparation
- hotfix/*: Emergency fixes
Trunk-Based Development
- Single main branch
- Short-lived feature branches
- Frequent integration
- Feature flags for incomplete features
Inspection and History
View History
git log
git log --oneline
git log --graph --oneline --all
git show <commit-hash>
git log --follow file.py
Compare Changes
git diff
git diff --staged
git diff main..feature-branch
git diff main feature-branch -- file.py
Search History
git log --grep="bug fix"
git log --author="John"
git log -S "function_name"
Stashing Changes
git stash
git stash save "work in progress"
git stash list
git stash apply
git stash pop
git stash apply stash@{2}
git stash drop stash@{0}
git stash clear
Validation Checklist
Before pushing:
Best Practices
- Commit often: Small, focused commits are easier to review and revert
- Write clear messages: Follow commit message conventions
- Pull before push: Avoid conflicts by staying up-to-date
- Use branches: Keep main branch stable
- Review before commit: Use
git diff to verify changes
- Don't commit secrets: Use .gitignore and environment variables
- Keep history clean: Use rebase for local branches, merge for shared branches
- Tag releases: Use
git tag v1.0.0 for version markers
Quick Reference
git status
git log --oneline -10
git diff
git add .
git commit -m "message"
git push origin main
git pull origin main
git checkout -b feature
git merge feature
git branch -d feature
git reset HEAD file
git checkout -- file
git revert HEAD
git remote -v
git fetch origin
git push -u origin branch