git-workflow
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGit Workflow
Git 工作流
When to use this skill
何时使用此技能
- Creating meaningful commit messages
- Managing branches
- Merging code
- Resolving conflicts
- Collaborating with team
- Git best practices
- 撰写有意义的提交信息
- 管理分支
- 代码合并
- 解决冲突
- 团队协作
- Git最佳实践
Instructions
操作指南
Step 1: Branch management
步骤1:分支管理
Create feature branch:
bash
undefined创建功能分支:
bash
undefinedCreate and switch to new branch
Create and switch to new branch
git checkout -b feature/feature-name
git checkout -b feature/feature-name
Or create from specific commit
Or create from specific commit
git checkout -b feature/feature-name <commit-hash>
**Naming conventions**:
- `feature/description`: New features
- `bugfix/description`: Bug fixes
- `hotfix/description`: Urgent fixes
- `refactor/description`: Code refactoring
- `docs/description`: Documentation updatesgit checkout -b feature/feature-name <commit-hash>
**命名规范**:
- `feature/description`: 新功能
- `bugfix/description`: Bug修复
- `hotfix/description`: 紧急修复
- `refactor/description`: 代码重构
- `docs/description`: 文档更新Step 2: Making changes
步骤2:进行修改
Stage changes:
bash
undefined暂存修改:
bash
undefinedStage specific files
Stage specific files
git add file1.py file2.js
git add file1.py file2.js
Stage all changes
Stage all changes
git add .
git add .
Stage with patch mode (interactive)
Stage with patch mode (interactive)
git add -p
**Check status**:
```bashgit add -p
**查看状态**:
```bashSee what's changed
See what's changed
git status
git status
See detailed diff
See detailed diff
git diff
git diff
See staged diff
See staged diff
git diff --staged
undefinedgit diff --staged
undefinedStep 3: Committing
步骤3:提交代码
Write good commit messages:
bash
git commit -m "type(scope): subject
Detailed description of what changed and why.
- Change 1
- Change 2
Fixes #123"Commit types:
- : New feature
feat - : Bug fix
fix - : Documentation
docs - : Formatting, no code change
style - : Code refactoring
refactor - : Adding tests
test - : Maintenance
chore
Example:
bash
git commit -m "feat(auth): add JWT authentication
- Implement JWT token generation
- Add token validation middleware
- Update user model with refresh token
Closes #42"撰写优质提交信息:
bash
git commit -m "type(scope): subject
Detailed description of what changed and why.
- Change 1
- Change 2
Fixes #123"提交类型:
- : 新功能
feat - : Bug修复
fix - : 文档更新
docs - : 格式调整,无代码变更
style - : 代码重构
refactor - : 添加测试
test - : 维护工作
chore
示例:
bash
git commit -m "feat(auth): add JWT authentication
- Implement JWT token generation
- Add token validation middleware
- Update user model with refresh token
Closes #42"Step 4: Pushing changes
步骤4:推送修改
bash
undefinedbash
undefinedPush to remote
Push to remote
git push origin feature/feature-name
git push origin feature/feature-name
Force push (use with caution!)
Force push (use with caution!)
git push origin feature/feature-name --force-with-lease
git push origin feature/feature-name --force-with-lease
Set upstream and push
Set upstream and push
git push -u origin feature/feature-name
undefinedgit push -u origin feature/feature-name
undefinedStep 5: Pulling and updating
步骤5:拉取与更新
bash
undefinedbash
undefinedPull latest changes
Pull latest changes
git pull origin main
git pull origin main
Pull with rebase (cleaner history)
Pull with rebase (cleaner history)
git pull --rebase origin main
git pull --rebase origin main
Fetch without merging
Fetch without merging
git fetch origin
undefinedgit fetch origin
undefinedStep 6: Merging
步骤6:合并代码
Merge feature branch:
bash
undefined合并功能分支:
bash
undefinedSwitch to main branch
Switch to main branch
git checkout main
git checkout main
Merge feature
Merge feature
git merge feature/feature-name
git merge feature/feature-name
Merge with no fast-forward (creates merge commit)
Merge with no fast-forward (creates merge commit)
git merge --no-ff feature/feature-name
**Rebase instead of merge**:
```bashgit merge --no-ff feature/feature-name
**使用变基替代合并**:
```bashOn feature branch
On feature branch
git checkout feature/feature-name
git checkout feature/feature-name
Rebase onto main
Rebase onto main
git rebase main
git rebase main
Continue after resolving conflicts
Continue after resolving conflicts
git rebase --continue
git rebase --continue
Abort rebase
Abort rebase
git rebase --abort
undefinedgit rebase --abort
undefinedStep 7: Resolving conflicts
步骤7:解决冲突
When conflicts occur:
bash
undefined发生冲突时:
bash
undefinedSee conflicted files
See conflicted files
git status
git status
Open files and resolve conflicts
Open files and resolve conflicts
Look for markers:
Look for markers:
<<<<<<< HEAD Current branch code
Incoming branch code
feature-branch
<<<<<<< HEAD Current branch code
Incoming branch code
feature-branch
After resolving
After resolving
git add <resolved-files>
git commit # For merge
git rebase --continue # For rebase
undefinedgit add <resolved-files>
git commit # For merge
git rebase --continue # For rebase
undefinedStep 8: Cleaning up
步骤8:清理操作
bash
undefinedbash
undefinedDelete local branch
Delete local branch
git branch -d feature/feature-name
git branch -d feature/feature-name
Force delete
Force delete
git branch -D feature/feature-name
git branch -D feature/feature-name
Delete remote branch
Delete remote branch
git push origin --delete feature/feature-name
git push origin --delete feature/feature-name
Clean up stale references
Clean up stale references
git fetch --prune
undefinedgit fetch --prune
undefinedAdvanced workflows
高级工作流
Interactive rebase
交互式变基
bash
undefinedbash
undefinedRebase last 3 commits
Rebase last 3 commits
git rebase -i HEAD~3
git rebase -i HEAD~3
Commands in editor:
Commands in editor:
pick: use commit
pick: use commit
reword: change commit message
reword: change commit message
edit: amend commit
edit: amend commit
squash: combine with previous
squash: combine with previous
fixup: like squash, discard message
fixup: like squash, discard message
drop: remove commit
drop: remove commit
undefinedundefinedStashing changes
暂存修改
bash
undefinedbash
undefinedStash current changes
Stash current changes
git stash
git stash
Stash with message
Stash with message
git stash save "Work in progress on feature X"
git stash save "Work in progress on feature X"
List stashes
List stashes
git stash list
git stash list
Apply most recent stash
Apply most recent stash
git stash apply
git stash apply
Apply and remove stash
Apply and remove stash
git stash pop
git stash pop
Apply specific stash
Apply specific stash
git stash apply stash@{2}
git stash apply stash@{2}
Drop stash
Drop stash
git stash drop stash@{0}
git stash drop stash@{0}
Clear all stashes
Clear all stashes
git stash clear
undefinedgit stash clear
undefinedCherry-picking
拣选提交
bash
undefinedbash
undefinedApply specific commit
Apply specific commit
git cherry-pick <commit-hash>
git cherry-pick <commit-hash>
Cherry-pick multiple commits
Cherry-pick multiple commits
git cherry-pick <hash1> <hash2> <hash3>
git cherry-pick <hash1> <hash2> <hash3>
Cherry-pick without committing
Cherry-pick without committing
git cherry-pick -n <commit-hash>
undefinedgit cherry-pick -n <commit-hash>
undefinedBisect (finding bugs)
二分查找(定位Bug)
bash
undefinedbash
undefinedStart bisect
Start bisect
git bisect start
git bisect start
Mark current as bad
Mark current as bad
git bisect bad
git bisect bad
Mark known good commit
Mark known good commit
git bisect good <commit-hash>
git bisect good <commit-hash>
Git will checkout commits to test
Git will checkout commits to test
Test and mark each:
Test and mark each:
git bisect good # if works
git bisect bad # if broken
git bisect good # if works
git bisect bad # if broken
When found, reset
When found, reset
git bisect reset
undefinedgit bisect reset
undefinedExamples
示例
Example 1: Feature development workflow
示例1:功能开发工作流
bash
undefinedbash
undefined1. Create feature branch
1. Create feature branch
git checkout main
git pull origin main
git checkout -b feature/user-profile
git checkout main
git pull origin main
git checkout -b feature/user-profile
2. Make changes
2. Make changes
... edit files ...
... edit files ...
3. Commit changes
3. Commit changes
git add src/profile/
git commit -m "feat(profile): add user profile page
- Create profile component
- Add profile API endpoints
- Add profile tests"
git add src/profile/
git commit -m "feat(profile): add user profile page
- Create profile component
- Add profile API endpoints
- Add profile tests"
4. Keep up to date with main
4. Keep up to date with main
git fetch origin
git rebase origin/main
git fetch origin
git rebase origin/main
5. Push to remote
5. Push to remote
git push origin feature/user-profile
git push origin feature/user-profile
6. Create Pull Request on GitHub/GitLab
6. Create Pull Request on GitHub/GitLab
... after review and approval ...
... after review and approval ...
7. Merge and cleanup
7. Merge and cleanup
git checkout main
git pull origin main
git branch -d feature/user-profile
undefinedgit checkout main
git pull origin main
git branch -d feature/user-profile
undefinedExample 2: Hotfix workflow
示例2:紧急修复工作流
bash
undefinedbash
undefined1. Create hotfix branch from production
1. Create hotfix branch from production
git checkout main
git pull origin main
git checkout -b hotfix/critical-bug
git checkout main
git pull origin main
git checkout -b hotfix/critical-bug
2. Fix the bug
2. Fix the bug
... make fixes ...
... make fixes ...
3. Commit
3. Commit
git add .
git commit -m "hotfix: fix critical login bug
Fixes authentication bypass vulnerability
Fixes #999"
git add .
git commit -m "hotfix: fix critical login bug
Fixes authentication bypass vulnerability
Fixes #999"
4. Push and merge immediately
4. Push and merge immediately
git push origin hotfix/critical-bug
git push origin hotfix/critical-bug
After merge:
After merge:
5. Cleanup
5. Cleanup
git checkout main
git pull origin main
git branch -d hotfix/critical-bug
undefinedgit checkout main
git pull origin main
git branch -d hotfix/critical-bug
undefinedExample 3: Collaborative workflow
示例3:协作工作流
bash
undefinedbash
undefined1. Update main branch
1. Update main branch
git checkout main
git pull origin main
git checkout main
git pull origin main
2. Create feature branch
2. Create feature branch
git checkout -b feature/new-feature
git checkout -b feature/new-feature
3. Regular updates from main
3. Regular updates from main
git fetch origin
git rebase origin/main
git fetch origin
git rebase origin/main
4. Push your work
4. Push your work
git push origin feature/new-feature
git push origin feature/new-feature
5. If teammate made changes to your branch
5. If teammate made changes to your branch
git pull origin feature/new-feature --rebase
git pull origin feature/new-feature --rebase
6. Resolve any conflicts
6. Resolve any conflicts
... resolve conflicts ...
... resolve conflicts ...
git add .
git rebase --continue
git add .
git rebase --continue
7. Force push after rebase
7. Force push after rebase
git push origin feature/new-feature --force-with-lease
undefinedgit push origin feature/new-feature --force-with-lease
undefinedBest practices
最佳实践
- Commit often: Small, focused commits
- Meaningful messages: Explain what and why
- Pull before push: Stay updated
- Review before commit: Check what you're committing
- Use branches: Never commit directly to main
- Keep history clean: Rebase feature branches
- Test before push: Run tests locally
- Write descriptive branch names: Easy to understand
- Delete merged branches: Keep repository clean
- Use .gitignore: Don't commit generated files
- 频繁提交: 小而专注的提交
- 有意义的信息: 说明变更内容及原因
- 推送前拉取: 保持代码更新
- 提交前检查: 确认提交内容
- 使用分支: 绝不直接提交到主分支
- 保持历史整洁: 对功能分支使用变基
- 推送前测试: 本地运行测试
- 分支名称描述性强: 便于理解
- 删除已合并分支: 保持仓库整洁
- 使用.gitignore: 不提交生成文件
Common patterns
常见操作
Undo last commit (keep changes)
撤销最后一次提交(保留变更)
bash
git reset --soft HEAD~1bash
git reset --soft HEAD~1Undo last commit (discard changes)
撤销最后一次提交(丢弃变更)
bash
git reset --hard HEAD~1bash
git reset --hard HEAD~1Amend last commit
修改最后一次提交
bash
undefinedbash
undefinedChange commit message
Change commit message
git commit --amend -m "New message"
git commit --amend -m "New message"
Add files to last commit
Add files to last commit
git add forgotten-file.txt
git commit --amend --no-edit
undefinedgit add forgotten-file.txt
git commit --amend --no-edit
undefinedView history
查看历史
bash
undefinedbash
undefinedDetailed log
Detailed log
git log
git log
One line per commit
One line per commit
git log --oneline
git log --oneline
With graph
With graph
git log --oneline --graph --all
git log --oneline --graph --all
Last 5 commits
Last 5 commits
git log -5
git log -5
Commits by author
Commits by author
git log --author="John"
git log --author="John"
Commits in date range
Commits in date range
git log --since="2 weeks ago"
undefinedgit log --since="2 weeks ago"
undefinedFind commits
查找提交
bash
undefinedbash
undefinedSearch commit messages
Search commit messages
git log --grep="keyword"
git log --grep="keyword"
Search code changes
Search code changes
git log -S "function_name"
git log -S "function_name"
Show file history
Show file history
git log --follow -- path/to/file
undefinedgit log --follow -- path/to/file
undefinedTroubleshooting
故障排除
Accidentally committed to wrong branch
意外提交到错误分支
bash
undefinedbash
undefined1. Create correct branch from current state
1. Create correct branch from current state
git branch feature/correct-branch
git branch feature/correct-branch
2. Reset current branch
2. Reset current branch
git reset --hard HEAD~1
git reset --hard HEAD~1
3. Switch to correct branch
3. Switch to correct branch
git checkout feature/correct-branch
undefinedgit checkout feature/correct-branch
undefinedNeed to undo a merge
需要撤销合并
bash
undefinedbash
undefinedIf not pushed yet
If not pushed yet
git reset --hard HEAD~1
git reset --hard HEAD~1
If already pushed (creates revert commit)
If already pushed (creates revert commit)
git revert -m 1 <merge-commit-hash>
undefinedgit revert -m 1 <merge-commit-hash>
undefinedRecover deleted branch
恢复已删除分支
bash
undefinedbash
undefinedFind lost commit
Find lost commit
git reflog
git reflog
Create branch from lost commit
Create branch from lost commit
git checkout -b recovered-branch <commit-hash>
undefinedgit checkout -b recovered-branch <commit-hash>
undefinedSync fork with upstream
同步复刻仓库与上游仓库
bash
undefinedbash
undefinedAdd upstream remote
Add upstream remote
git remote add upstream https://github.com/original/repo.git
git remote add upstream https://github.com/original/repo.git
Fetch upstream
Fetch upstream
git fetch upstream
git fetch upstream
Merge upstream main
Merge upstream main
git checkout main
git merge upstream/main
git checkout main
git merge upstream/main
Push to your fork
Push to your fork
git push origin main
undefinedgit push origin main
undefinedGit configuration
Git 配置
User setup
用户设置
bash
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"bash
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"Aliases
别名设置
bash
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.lg 'log --oneline --graph --all'bash
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.lg 'log --oneline --graph --all'Editor
编辑器设置
bash
git config --global core.editor "code --wait" # VS Code
git config --global core.editor "vim" # Vimbash
git config --global core.editor "code --wait" # VS Code
git config --global core.editor "vim" # Vim