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 code
  • develop - Integration branch for features

Supporting Branches

  • feature/* - New features
  • bugfix/* - Bug fixes
  • hotfix/* - 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 feature
  • fix: Bug fix
  • docs: Documentation
  • style: Formatting
  • refactor: Code restructuring
  • test: Adding tests
  • chore: Maintenance

Example:

feat: add user authentication

- Implement JWT-based auth
- Add login/logout endpoints
- Create auth middleware

Closes #123

Pull Request Workflow

  1. Create feature branch
  2. Make commits
  3. Push to remote
  4. Create pull request
  5. Code review
  6. Address feedback
  7. 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

  1. Commit often - Small, focused commits
  2. Write clear messages - Describe what and why
  3. Pull before push - Keep local branch updated
  4. Review before merge - Use pull requests
  5. Don’t commit secrets - Use .gitignore
  6. Test before commit - Ensure code works
  7. 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

  1. Sync with team daily
  2. Keep branches short-lived
  3. Delete merged branches
  4. Use meaningful branch names
  5. 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.