Skip
Arish's avatar

49. Best Practices for Git and GitHub


Best Practices for Git and GitHub

A comprehensive guide to professional Git and GitHub workflows.

Commit Best Practices

Write Good Commit Messages

bash
1# Format
2<type>: <subject>
3
4<body>
5
6# Examples
7feat: Add user authentication
8fix: Resolve login timeout issue
9docs: Update API documentation
10refactor: Simplify validation logic

Commit Often, Commit Small

bash
1# Good: Focused commits
2git commit -m "Add user model"
3git commit -m "Add user controller"
4git commit -m "Add user routes"
5
6# Bad: Giant commits
7git commit -m "Add entire user system"

Use Present Tense

bash
1# Good
2git commit -m "Add feature"
3
4# Avoid
5git commit -m "Added feature"

Branch Best Practices

Naming Conventions

feature/user-authentication bugfix/login-error hotfix/security-patch release/v1.0.0

Keep Branches Short-Lived

  • Merge within days, not weeks
  • Regularly sync with main
  • Delete after merging

Protect Main Branch

Settings → Branches → Add rule: ☑ Require pull request ☑ Require reviews ☑ Require status checks

Pull Request Best Practices

Keep PRs Small

  • Under 400 lines when possible
  • Single purpose
  • Easier to review

Write Good Descriptions

markdown
1## What
2Brief description
3
4## Why
5Reason for change
6
7## How
8Implementation details
9
10## Testing
11How to test
12
13Closes #123

Request Appropriate Reviews

  • 1-2 reviewers usually enough
  • Use CODEOWNERS for automation

Repository Best Practices

Essential Files

README.md # Project overview LICENSE # Usage terms .gitignore # Ignored files CONTRIBUTING.md # How to contribute CHANGELOG.md # Version history

Use Issues and Projects

  • Track all work in issues
  • Use project boards
  • Link PRs to issues

Automate with Actions

yaml
1# .github/workflows/ci.yml
2name: CI
3on: [push, pull_request]
4jobs:
5  test:
6    runs-on: ubuntu-latest
7    steps:
8      - uses: actions/checkout@v3
9      - run: npm test

Security Best Practices

Never Commit Secrets

gitignore
1# .gitignore
2.env
3*.pem
4secrets/

Use Environment Variables

bash
1# Not in code
2DB_PASSWORD=secret
3
4# In code
5process.env.DB_PASSWORD

Enable Security Features

  • Dependabot alerts
  • Code scanning
  • Secret scanning

Team Workflow

Standard Flow

1. Create issue 2. Create branch from main 3. Make changes 4. Open PR 5. Get reviews 6. Merge to main 7. Delete branch

Stay Synchronized

bash
1# Daily routine
2git checkout main
3git pull
4git checkout my-feature
5git rebase main

Common Git Commands Cheatsheet

bash
1# Setup
2git init
3git clone <url>
4git config --global user.name "Name"
5
6# Daily Work
7git status
8git add .
9git commit -m "message"
10git push origin branch
11
12# Branching
13git checkout -b branch
14git switch branch
15git merge branch
16git branch -d branch
17
18# Remote
19git remote -v
20git fetch origin
21git pull origin main
22git push origin branch
23
24# History
25git log --oneline
26git diff
27git show commit
28
29# Undo
30git reset HEAD~1
31git revert commit
32git stash

Summary

Golden Rules

  1. Commit often with clear messages
  2. Branch for every feature
  3. Review all code through PRs
  4. Never commit secrets
  5. Automate testing and deployment
  6. Document your project

Congratulations! You've completed the Git and GitHub Essentials course! 🎉