Imported from pengfeip908/git-skill-test (
external/skills/git/SKILL.md). Install upstream withnpx skills add pengfeip908/git-skill-test --skill git. Copyright stays with the author.
Complete Git Workflow
Overview
This skill provides comprehensive Git workflow guidance from repository initialization to advanced collaboration features.
When to Use
Use this skill when:
- Setting up a new repository
- Cloning an existing repository
- Managing branches and merges
- Working with remotes
- Resolving conflicts
- Collaborating with a team
- Learning Git best practices
Repository Setup
1. Initialize New Repository
Create new repository:
# Create directory
mkdir my-project
cd my-project
# Initialize git
git init
# Add files
git add .
# Create initial commit
git commit -m "feat: initial commit"
With README:
# Create README
echo "# My Project" > README.md
# Initialize and commit
git init
git add README.md
git commit -m "docs: add README"
2. Clone Existing Repository
Basic clone:
git clone https://github.com/user/repo.git
cd repo
Clone with specific branch:
git clone -b develop https://github.com/user/repo.git
Clone to specific directory:
git clone https://github.com/user/repo.git my-directory
Shallow clone (last commit only):
git clone --depth 1 https://github.com/user/repo.git
Daily Workflow
1. Check Status
# See current status
git status
# See branch information
git branch -v
# See untracked files
git ls-files --others --exclude-standard
2. Make Changes
# Create/edit files
vim file.py
# See what changed
git diff
# See specific file
git diff file.py
# See staged changes
git diff --staged
3. Stage Changes
# Stage specific file
git add file.py
# Stage all changes
git add .
# Stage interactively
git add -i
# Stage by patch
git add -p
4. Commit Changes
# Simple commit
git commit -m "feat: add new feature"
# Commit with body
git commit -m "feat: add new feature
Detailed description of what was added
and why it was necessary."
# Commit all staged and unstaged
git commit -am "fix: resolve bug"
5. Push Changes
# Push to current branch
git push
# Push to specific branch
git push origin feature-branch
# Push with upstream tracking
git push -u origin feature-branch
# Push all branches
git push --all
Branching
1. Create Branch
# Create and switch to branch
git checkout -b feature-branch
# Or (newer syntax)
git switch -c feature-branch
# Create branch without switching
git branch feature-branch
2. List Branches
# List local branches
git branch
# List remote branches
git branch -r
# List all branches
git branch -a
# List with last commit
git branch -v
3. Switch Branches
# Switch to existing branch
git checkout main
# Or (newer syntax)
git switch main
# Switch to previous branch
git checkout -
4. Delete Branches
# Delete local branch
git branch -d feature-branch
# Force delete (unmerged)
git branch -D feature-branch
# Delete remote branch
git push origin --delete feature-branch
5. Rename Branch
# Rename current branch
git branch -m new-name
# Rename specific branch
git branch -m old-name new-name
6. Branch Comparison
# Show commits in branch A not in branch B
git log branch-B..branch-A
# Show diff between branches
git diff branch-A branch-B
# Show merged branches
git branch --merged
Merging
1. Basic Merge
# Switch to target branch
git checkout main
# Merge feature branch
git merge feature-branch
# Push
git push
2. Merge Strategies
Fast-forward (default):
git merge feature-branch
# Branch pointer moves forward
No fast-forward (creates merge commit):
git merge --no-ff feature-branch
# Always creates merge commit
Squash merge:
git merge --squash feature-branch
# Combines all commits into one
3. Merge Conflicts
When conflicts occur:
# 1. Git will pause merge
# 2. Check conflicts
git status
# 3. Edit conflicted files
# Files will show conflict markers:
# <<<<<<< HEAD
# Your changes
# =======
# Their changes
# >>>>>>> feature-branch
# 4. Resolve conflicts manually
# Edit file to remove markers and keep desired code
# 5. Mark as resolved
git add resolved-file.py
# 6. Complete merge
git commit
Conflict resolution tools:
# Use merge tool
git mergetool
# Accept yours
git checkout --ours file.py
# Accept theirs
git checkout --theirs file.py
Rebasing
1. Basic Rebase
# Rebase current branch onto main
git checkout feature-branch
git rebase main
2. Interactive Rebase
# Rebase last 3 commits
git rebase -i HEAD~3
# Commands:
# pick - keep commit
# reword - edit commit message
# edit - pause for changes
# squash - merge with previous
# fixup - merge with previous (discard message)
# drop - remove commit
3. Rebase vs Merge
Rebase:
- Linear history
- Rewrites history
- Use for local cleanup
Merge:
- Preserves history
- Non-destructive
- Use for public branches
WARNING: Never rebase pushed commits!
Remotes
1. Add Remote
# Add remote
git remote add origin https://github.com/user/repo.git
# Add multiple remotes
git remote add upstream https://github.com/original/repo.git
git remote add fork https://github.com/yourfork/repo.git
2. View Remotes
# List remotes
git remote -v
# Show remote details
git remote show origin
3. Fetch from Remote
# Fetch all branches
git fetch
# Fetch specific remote
git fetch origin
# Fetch specific branch
git fetch origin main
4. Pull from Remote
# Pull (fetch + merge)
git pull
# Pull with rebase
git pull --rebase
# Pull specific branch
git pull origin main
5. Push to Remote
# Push current branch
git push
# Push specific branch
git push origin feature-branch
# Push with upstream
git push -u origin feature-branch
# Force push (use with caution!)
git push --force
# Force push with lease (safer)
git push --force-with-lease
6. Remove Remote
# Remove remote
git remote remove origin
History Management
1. View History
# Simple log
git log --oneline
# Detailed log
git log
# Graph log
git log --oneline --graph --all
# View specific file history
git log --follow file.py
# View changes in commit
git show abc1234
2. Search History
# Search commits
git log --grep="search term"
# Search by author
git log --author="John"
# Search by date
git log --since="2024-01-01" --until="2024-01-31"
# Search by content
git log -S"function_name"
3. Reset History
# Soft reset (keep changes)
git reset --soft HEAD~1
# Mixed reset (unstage changes)
git reset HEAD~1
# Hard reset (discard changes)
git reset --hard HEAD~1
# Reset to specific commit
git reset --hard abc1234
4. Revert Commits
# Revert commit (creates new commit)
git revert abc1234
# Revert multiple commits
git revert HEAD~3..HEAD
Stashing
1. Stash Changes
# Stash current changes
git stash
# Stash with message
git stash save "work in progress"
# Stash including untracked files
git stash -u
# Stash all files
git stash -a
2. View Stashes
# List stashes
git stash list
# Show stash contents
git stash show stash@{0}
# Show stash diff
git stash show -p stash@{0}
3. Apply Stash
# Apply most recent stash
git stash pop
# Apply specific stash
git stash pop stash@{1}
# Apply without removing from stash
git stash apply stash@{0}
4. Drop Stash
# Drop specific stash
git stash drop stash@{0}
# Drop all stashes
git stash clear
Tagging
1. Create Tag
# Lightweight tag
git tag v1.0.0
# Annotated tag
git tag -a v1.0.0 -m "Release version 1.0.0"
# Tag specific commit
git tag -a v0.9.0 abc1234 -m "Beta release"
2. View Tags
# List tags
git tag
# Show tag details
git show v1.0.0
# List tags with commits
git log --tags --simplify-by-decoration --pretty="format:%ai %d"
3. Push Tags
# Push specific tag
git push origin v1.0.0
# Push all tags
git push --tags
# Delete tag locally
git tag -d v1.0.0
# Delete tag remotely
git push origin --delete v1.0.0
Best Practices
✅ DO
- Commit frequently: Small, focused commits
- Pull before pushing: Avoid conflicts
- Use branches: One branch per feature
- Write good messages: Conventional commits format
- Review changes: Check diff before committing
- Test before committing: Ensure code works
- Use .gitignore: Exclude generated files
- Document changes: Reference issues in commits
❌ DON'T
- Don't commit secrets: Never commit passwords/tokens
- Don't commit broken code: Test first
- Don't amend pushed commits: Disrupts collaborators
- Don't force push publicly: Rewrites shared history
- Don't ignore merge conflicts: Resolve them properly
- Don't commit everything: Use .gitignore
- Don't commit large files: Use Git LFS for binaries
- Don't mix changes: One logical change per commit
Common Workflows
Feature Branch Workflow
# 1. Create feature branch
git checkout -b feature/new-feature
# 2. Make changes
git add .
git commit -m "feat: add new feature"
# 3. Push to remote
git push -u origin feature/new-feature
# 4. Create pull request on GitHub
# 5. After review and merge
git checkout main
git pull
git branch -d feature/new-feature
Hotfix Workflow
# 1. Create hotfix branch from release
git checkout -b hotfix/critical-bug v1.0.0
# 2. Fix bug
git add .
git commit -m "fix: resolve critical bug"
# 3. Merge to main and release
git checkout main
git merge hotfix/critical-bug
git tag v1.0.1
# 4. Merge back to develop
git checkout develop
git merge hotfix/critical-bug
Fork and Contribute Workflow
# 1. Fork repository on GitHub
# 2. Clone your fork
git clone https://github.com/yourusername/repo.git
cd repo
# 3. Add upstream remote
git remote add upstream https://github.com/original/repo.git
# 4. Create feature branch
git checkout -b feature/new-feature
# 5. Make changes
git add .
git commit -m "feat: add new feature"
# 6. Push to your fork
git push -u origin feature/new-feature
# 7. Create pull request on GitHub
# 8. Keep your fork updated
git fetch upstream
git checkout main
git merge upstream/main
git push origin main
Troubleshooting
Issue: Merge Conflict
# 1. See conflicts
git status
# 2. Edit files to resolve conflicts
# Remove <<<<<<< ======= >>>>>>> markers
# Keep desired code
# 3. Mark as resolved
git add resolved-file.py
# 4. Complete merge
git commit
Issue: Accidental Commit to Wrong Branch
# 1. Create branch from current commit
git branch feature-branch
# 2. Reset original branch
git checkout main
git reset --hard HEAD~1
# 3. Continue on feature-branch
git checkout feature-branch
Issue: Undo Last Commit
# Keep changes
git reset --soft HEAD~1
# Unstage changes
git reset HEAD~1
# Discard changes
git reset --hard HEAD~1
Issue: Recover Lost Commits
# Find lost commits
git reflog
# Reset to lost commit
git reset --hard abc1234
Tips
- Pull often: Stay in sync with remote
- Push frequently: Back up your work
- Check status: Know what's changed
- Review diffs: See what you're committing
- Write good messages: Explain what and why
- Use branches: Isolate your work
- Test before pushing: Avoid broken builds
- Resolve conflicts: Don't ignore them
- Keep history clean: Rebase local commits
- Document changes: Link commits to issues
Related Resources
- conventional-commits: Conventional Commits specification
- github-cli: GitHub-specific operations
Resources
Sources:
- Official Git Documentation
- Pro Git Book
- GitHub Git Guide
- Atlassian Git Tutorial
- Learn Git Branching
- Git Best Practices
#github的仓库地址 url:https://github.com/nerver111/git-skill-test.git