Master Git workflows to collaborate effectively and maintain clean project history.
Basic Git Workflow
# Clone repository
git clone https://github.com/username/repo.git
# Create feature branch
git checkout -b feature/new-feature
# Make changes and commit
git add .
git commit -m "Add new feature"
# Push to remote
git push origin feature/new-feature
Branching Strategy
Main Branches
main- Production-ready codedevelop- Integration branch for features
Supporting Branches
feature/*- New featuresbugfix/*- Bug fixeshotfix/*- Emergency fixes
Feature Development
# Start new feature
git checkout develop
git pull origin develop
git checkout -b feature/user-auth
# Work on feature
git add src/auth/
git commit -m "feat: add user authentication"
# Keep feature branch updated
git checkout develop
git pull origin develop
git checkout feature/user-auth
git rebase develop
# Push feature
git push origin feature/user-auth
Commit Message Convention
<type>: <subject>
<body>
<footer>
Types:
feat: New featurefix: Bug fixdocs: Documentationstyle: Formattingrefactor: Code restructuringtest: Adding testschore: Maintenance
Example:
feat: add user authentication
- Implement JWT-based auth
- Add login/logout endpoints
- Create auth middleware
Closes #123
Pull Request Workflow
- Create feature branch
- Make commits
- Push to remote
- Create pull request
- Code review
- Address feedback
- Merge to develop
Useful Commands
# Undo last commit (keep changes)
git reset --soft HEAD~1
# Discard local changes
git checkout -- <file>
# Stash changes
git stash
git stash pop
# View history
git log --oneline --graph
# Rebase interactive
git rebase -i HEAD~3
# Cherry-pick commit
git cherry-pick <commit-hash>
Merge vs Rebase
Merge
git checkout main
git merge feature/new-feature
- Preserves history
- Creates merge commit
- Safe for shared branches
Rebase
git checkout feature/new-feature
git rebase main
- Linear history
- Cleaner log
- Don’t rebase shared branches
Handling Conflicts
# During merge/rebase
git status # See conflicted files
# Edit files to resolve conflicts
# Then:
git add <resolved-files>
git rebase --continue
# or
git merge --continue
Best Practices
- Commit often - Small, focused commits
- Write clear messages - Describe what and why
- Pull before push - Keep local branch updated
- Review before merge - Use pull requests
- Don’t commit secrets - Use .gitignore
- Test before commit - Ensure code works
- Use branches - Never commit directly to main
Gitignore Example
# Dependencies
node_modules/
vendor/
# Build output
dist/
build/
# Environment
.env
.env.local
# IDE
.vscode/
.idea/
# OS
.DS_Store
Thumbs.db
Collaboration Tips
- Sync with team daily
- Keep branches short-lived
- Delete merged branches
- Use meaningful branch names
- Document workflow in README
Conclusion
Good Git workflow improves team collaboration and code quality. Practice these techniques and adapt them to your team’s needs.