Skip to main content

Best Practices for Git and GitHub Workflows

Now that you've mastered Git and GitHub fundamentals, let's bring everything together. This final lesson synthesizes all you've learned into practical workflows and best practices that professional teams use daily.

Learning Goals

  • Apply Git best practices for commit hygiene and branching strategies
  • Implement effective GitHub collaboration workflows
  • Establish code quality and security practices
  • Optimize your development workflow with automation

Commit Best Practices

Atomic Commits

Make each commit a single logical change. Atomic commits are easier to understand, review, and revert if needed.

Good: Separate commits for different changes
# Fix bug in authentication
git add auth-fix.js
git commit -m "Fix null pointer in user authentication"

# Add new feature
git add new-feature.js
git commit -m "Implement user profile export"
Avoid: Multiple unrelated changes in one commit
git add auth-fix.js new-feature.js config-update.js
git commit -m "Various updates and fixes"

Meaningful Commit Messages

Write commit messages that clearly explain what changed and why.

Good commit message structure
git commit -m "Refactor user authentication

- Extract authentication logic into separate service
- Remove duplicate validation code
- Improve error handling for expired tokens

Fixes #123"
tip

Use the imperative mood in commit messages ("Add feature" not "Added feature") to match Git's own style. The first line should be under 50 characters, with detailed explanation following after a blank line.

Branching Strategies

Feature Branch Workflow

Keep main always deployable by developing features in separate branches.

Creating and managing feature branches
# Create feature branch from main
git checkout main
git pull origin main
git checkout -b feature/user-dashboard

# Develop and commit your changes
git add .
git commit -m "Implement user dashboard layout"

# Push to remote
git push -u origin feature/user-dashboard

Git Flow Strategy

Many teams use a structured branching model:

Git flow branch types
main          # Production-ready code
develop # Integration branch for features
feature/* # New functionality
release/* # Preparation for releases
hotfix/* # Critical production fixes

Code Review Practices

Effective Pull Requests

Make PRs easy to review with clear descriptions and manageable size.

Pull request template example
## Description
Briefly describe what this PR accomplishes

## Changes
- Added user profile export functionality
- Updated API documentation
- Fixed minor UI alignment issue

## Testing
- [ ] Manual testing completed
- [ ] Unit tests added/updated
- [ ] Integration tests passing

## Screenshots
<!-- Add relevant screenshots if UI changes -->
note

Keep PRs focused - if a PR addresses multiple unrelated issues, consider splitting it. A good rule of thumb: if you can't describe the changes in 2-3 sentences, the PR might be too large.

Repository Hygiene

.gitignore Strategy

Protect sensitive information and avoid clutter.

Comprehensive .gitignore
# Dependencies
node_modules/
vendor/

# Environment variables
.env
.env.local

# IDE files
.vscode/
.idea/

# Build outputs
dist/
build/

# OS files
.DS_Store
Thumbs.db

# Logs
*.log

Protecting Main Branch

Configure branch protection rules to require:

  • Pull request reviews
  • Status checks to pass
  • Linear commit history

Automation with GitHub Actions

Quality Gates

Automate code quality checks on every PR.

.github/workflows/quality-checks.yml
name: Quality Checks

on: [push, pull_request]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Install dependencies
run: npm ci
- name: Run ESLint
run: npm run lint
- name: Run tests
run: npm test

Security Scanning

Integrate security checks into your workflow:

Security scanning in CI
- name: Security audit
run: npm audit

- name: Dependency review
uses: actions/dependency-review-action@v3

Collaboration Patterns

Issue-Driven Development

Link all changes to tracked issues.

Linking commits to issues
git commit -m "Add user export feature

- Implement CSV export functionality
- Add export button to user interface
- Include unit tests for export service

Closes #45"

Regular Maintenance

Keep your repository healthy with routine tasks:

Repository maintenance
# Update main regularly
git checkout main
git pull origin main

# Clean up merged branches
git branch --merged main | grep -v "main" | xargs git branch -d

# Prune remote tracking branches
git fetch --prune

Common Pitfalls

  • Giant commits: Breaking changes into smaller commits makes review and debugging easier
  • Ignoring CI failures: Never merge PRs with failing checks, even for "small" changes
  • Commit messages like "fix" or "update": These become meaningless when looking at history
  • Long-lived feature branches: Regularly merge main into feature branches to avoid massive conflicts
  • Pushing directly to main: Always use PRs, even for solo projects - it creates valuable history
  • Forgetting to test merge conflicts: Always test that your branch merges cleanly before creating PR
  • Ignoring .gitignore: Accidentally committing secrets or large files can be hard to undo

Summary

Effective Git and GitHub workflows combine technical skills with team discipline. Remember: atomic commits, descriptive messages, protected main branch, and comprehensive automation create a foundation for successful collaboration. Your workflow should make code review easy, catch issues early, and maintain a clean project history.

Collaborative Git Best Practices – Quick Check

What characterizes an "atomic commit"?

Question 1/5