Advanced Commit Strategies
Interactive Rebasing
Understanding Interactive Rebasing
Interactive rebasing allows you to modify commit history before pushing to a shared repository:
## Start interactive rebase for last 3 commits
git rebase -i HEAD~3
graph TD
A[Original Commit History] --> B[Interactive Rebase]
B --> C[Reorder Commits]
B --> D[Squash Commits]
B --> E[Edit Commit Messages]
Rebase Actions
Action |
Description |
Example |
pick |
Use commit as-is |
Default action |
reword |
Modify commit message |
Change description |
squash |
Combine commits |
Merge multiple commits |
fixup |
Combine commits, discard message |
Cleanup history |
drop |
Remove commit |
Delete unnecessary commits |
Commit Signing
GPG Key Setup
## Generate GPG key
gpg --full-generate-key
## Configure Git to use GPG
git config --global user.signingkey YOUR_GPG_KEY
git config --global commit.gpgsign true
Semantic Commit Messages
Conventional Commits Standard
graph LR
A[Type] --> B[Optional Scope]
B --> C[Descriptive Message]
C --> D[Optional Breaking Change]
Commit Type Examples
Type |
Description |
Example |
feat |
New feature |
feat(auth): add two-factor authentication |
fix |
Bug fix |
fix(database): resolve connection leak |
docs |
Documentation |
docs(readme): update installation guide |
refactor |
Code restructuring |
refactor(api): simplify error handling |
test |
Test-related changes |
test(user): add login validation tests |
Advanced Staging Techniques
Partial Staging
## Stage specific hunks of a file
git add -p filename.py
Stashing Changes
## Temporarily store uncommitted changes
git stash save "Work in progress"
## List stashed changes
git stash list
## Apply most recent stash
git stash apply
Commit Hooks
Pre-Commit Validation
## Sample pre-commit hook script
#!/bin/bash
## Validate code style
npm run lint
## Run tests
npm test
Commit Best Practices for LabEx Projects
- Use atomic commits
- Write descriptive messages
- Sign commits for authenticity
- Follow semantic commit conventions
- Utilize interactive rebasing
Commit Workflow Visualization
flowchart LR
A[Working Directory] -->|Stage Changes| B[Staging Area]
B -->|Commit| C[Local Repository]
C -->|Interactive Rebase| D[Refined History]
D -->|Push| E[Remote Repository]
Advanced Git Configuration
## Global Git configurations
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.lg "log --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --graph"
Error Handling and Recovery
Recovering from Mistakes
## Undo last commit, keeping changes
git reset --soft HEAD~1
## Completely remove last commit
git reset --hard HEAD~1