Git Workflow SOP
Overview
This Skill provides a standardized Git operation workflow, including:
- Check git status and review changes
- Generate appropriate commit messages
- Commit changes using standardized messages
- Synchronize with remote repositories
- Create reusable SOP documents
Usage Scenarios
Invoke this Skill when you need to perform Git operations or record standard workflows to follow best practices.
Operation Steps
1. Check Git Status
First, check the current status of the repository:
2. Review Changes
View the differences between the working directory and the last commit:
3. Stage Changes
Add all changes to the staging area:
4. Generate Commit Message
Create a descriptive commit message explaining what changes were made and why:
bash
git commit -m "<descriptive commit message>"
Commit Message Best Practices:
- Use imperative mood (e.g., "Add feature" instead of "Added feature")
- Limit the first line to 72 characters
- Provide detailed explanations in the body if needed
- Reference relevant issue numbers if applicable
Commit Message Type Prefixes:
- : New feature
- : Bug fix
- : Documentation update
- : Code refactoring
- : Testing-related
- : Build/tool-related
5. Push to Remote Repository
Synchronize local changes to the remote repository:
bash
git push origin <branch-name>
6. Record SOP
Create documents to record the operation process for future reference and team members.
Examples
Example 1: Standard Commit Workflow
Regular changes:
bash
git status
git diff
git add .
git commit -m "docs: update documentation and fix minor issues"
git push origin main
Example 2: Feature Development
Feature development with detailed commit message:
bash
git status
git diff
git add .
git commit -m "feat: implement user authentication feature
- Add login and registration interfaces
- Integrate database for user storage
- Include password hashing for security
- Add JWT token generation for session management"
git push origin feature/user-auth
Example 3: Bug Fix
bash
git status
git diff
git add .
git commit -m "fix: resolve WebSocket disconnection issue
- Add heartbeat mechanism to maintain connection
- Increase reconnection logic
- Optimize error handling"
git push origin main
Best Practices
- Frequent Commits: Make small, focused commits instead of large-scale changes
- Descriptive Messages: Write clear commit messages explaining "what" and "why"
- Consistent Workflow: Follow the same steps for each commit to maintain consistency
- Remote Synchronization: Push changes regularly to avoid losing work
- Documentation: Keep SOP documents updated with current practices
Troubleshooting
Issue: Merge Conflicts
If you encounter merge conflicts:
- Pull the latest changes:
- Manually resolve conflicts in affected files
- Stage resolved files:
- Complete the merge:
Issue: Large Files
If git slows down due to large files:
- Check file sizes:
git ls-files | xargs ls -l | sort -k5 -n -r | head
- Consider adding large files to
- If necessary, use Git LFS to handle large binary files
Issue: Undo Operations
- Undo unstaged changes:
- Unstage staged files:
- Undo the last commit (keep changes):
Common Command Reference
| Command | Description |
|---|
| View the last 10 commit histories |
| View staged changes |
| Unstage files |
| Discard changes in the working directory |
| Temporarily save current changes |
| Restore temporarily saved changes |
| View all branches |
| View remote repository information |