Git Workflow Skill
Expert knowledge and best practices for git operations, branching strategies, and version control workflows.
Common Git Operations
Creating Feature Branches
bash
# Create and switch to new branch git checkout -b feature/description # Naming conventions # - feature/add-user-auth # - fix/login-validation-bug # - refactor/api-service # - docs/update-readme
Committing Changes
bash
# Stage specific files git add file1.ts file2.ts # Stage all changes git add . # Commit with message git commit -m "feat(auth): add password reset functionality" # Amend last commit (if not pushed) git commit --amend -m "Updated message"
Conventional Commits
Format: type(scope): message
Types:
- •
feat: New feature - •
fix: Bug fix - •
docs: Documentation changes - •
style: Code style (formatting, missing semicolons) - •
refactor: Code refactoring - •
test: Adding or updating tests - •
chore: Build process, dependencies, tooling
Examples:
code
feat(api): add user profile endpoint fix(auth): handle expired token gracefully docs(readme): update installation instructions refactor(services): extract common validation logic test(user): add edge case tests for email validation
Viewing History and Changes
bash
# View commit history git log --oneline -10 # View changes in a file git log -p filename.ts # See what changed in a commit git show commit-hash # View current changes git diff # View staged changes git diff --cached # Compare branches git diff main..feature-branch
Working with Remote
bash
# Fetch latest from remote git fetch origin # Pull changes from remote git pull origin main # Push to remote git push origin branch-name # Push and set upstream git push -u origin branch-name # Force push (use carefully!) git push --force-with-lease origin branch-name
Branch Management
bash
# List all branches git branch -a # Switch to existing branch git checkout branch-name # Delete local branch git branch -d branch-name # Delete remote branch git push origin --delete branch-name # Rename current branch git branch -m new-name
Stashing Changes
bash
# Stash current changes
git stash
# Stash with message
git stash save "WIP: working on feature X"
# List stashes
git stash list
# Apply most recent stash
git stash apply
# Apply and remove stash
git stash pop
# Apply specific stash
git stash apply stash@{2}
# Clear all stashes
git stash clear
Resolving Merge Conflicts
bash
# Start merge git merge feature-branch # If conflicts occur: # 1. Open conflicted files # 2. Look for conflict markers: # <<<<<<< HEAD # Your changes # ======= # Their changes # >>>>>>> branch-name # 3. Edit to resolve conflicts # 4. Remove conflict markers # 5. Stage resolved files git add conflicted-file.ts # Complete merge git commit
Undoing Changes
bash
# Discard changes in working directory git checkout -- filename.ts # Unstage file git reset HEAD filename.ts # Undo last commit (keep changes) git reset --soft HEAD~1 # Undo last commit (discard changes) git reset --hard HEAD~1 # Revert a specific commit (creates new commit) git revert commit-hash # Interactive rebase (rewrite history) git rebase -i HEAD~3
Cherry-picking
bash
# Apply a specific commit from another branch git cherry-pick commit-hash # Cherry-pick without committing git cherry-pick -n commit-hash
Branching Strategies
Git Flow
code
main (production) ├── develop (integration) │ ├── feature/user-auth │ ├── feature/payment │ └── feature/notifications ├── release/v1.2.0 └── hotfix/critical-bug
Trunk-Based Development
code
main (always deployable) ├── feature/short-lived-branch-1 └── feature/short-lived-branch-2
Best Practices
Before Committing
- •Review your changes:
git diff - •Stage only related changes
- •Write clear commit messages
- •Run tests locally
- •Check linter/formatter
Before Pushing
- •Pull latest changes:
git pull --rebase origin main - •Resolve any conflicts
- •Run full test suite
- •Ensure CI will pass
Working with Teams
- •Keep branches up to date with main
- •Make small, focused commits
- •Use descriptive branch names
- •Delete branches after merging
- •Never force push to shared branches (unless you know what you're doing)
Commit Size
- •Small, atomic commits are better
- •Each commit should be a logical unit
- •Should pass tests independently
- •Easy to review and revert if needed
Troubleshooting
Accidentally Committed to Wrong Branch
bash
# On wrong branch, stash the commit git reset --soft HEAD~1 git stash # Switch to correct branch git checkout correct-branch # Apply the stashed commit git stash pop git commit -m "Your message"
Accidentally Committed Secrets
bash
# Remove from history (use with caution!) git filter-branch --force --index-filter \ "git rm --cached --ignore-unmatch path/to/secret/file" \ --prune-empty --tag-name-filter cat -- --all # Or use BFG Repo-Cleaner (recommended) bfg --delete-files secrets.env git reflog expire --expire=now --all git gc --prune=now --aggressive
Merge Conflicts During Rebase
bash
# Resolve conflicts in files # Then continue rebase git add resolved-file.ts git rebase --continue # Or abort rebase git rebase --abort
Advanced Operations
Interactive Rebase
bash
# Rebase last 3 commits git rebase -i HEAD~3 # In editor, you can: # - pick: keep commit # - reword: change commit message # - edit: modify commit # - squash: combine with previous commit # - fixup: squash without editing message # - drop: remove commit
Bisect (Find Bug Introduction)
bash
# Start bisect git bisect start # Mark current as bad git bisect bad # Mark known good commit git bisect good commit-hash # Git checks out middle commit, test it # Then mark as good or bad git bisect good # or git bisect bad # Continue until bug is found # When done git bisect reset
Submodules
bash
# Add submodule git submodule add https://github.com/user/repo path/to/submodule # Clone repo with submodules git clone --recursive https://github.com/user/repo # Update submodules git submodule update --init --recursive # Pull latest for all submodules git submodule update --remote
Useful Aliases
Add to ~/.gitconfig:
ini
[alias] st = status co = checkout br = branch ci = commit unstage = reset HEAD -- last = log -1 HEAD visual = log --graph --oneline --all amend = commit --amend --no-edit
Safety Tips
- •Never rewrite public history (commits that have been pushed)
- •Use
--force-with-leaseinstead of--forcewhen you must force push - •Backup before dangerous operations:
git branch backup-branch - •Test rebases locally before pushing
- •Keep commits atomic and reversible
- •Communicate with team before force pushing to shared branches
When to Ask for Help
When you need to:
- •Perform complex git operations on a project
- •Resolve merge conflicts
- •Understand git history
- •Set up branching strategy
- •Recover from git mistakes
- •Optimize git workflow
Simply mention git operations or issues, and I'll apply this knowledge to help you.