Document

Git & GitHub Mastery

Branching, commits, pull requests, and advanced Git techniques for professional collaboration.

🧩 Git & GitHub Mastery

This guide covers advanced Git usage and Pull Request workflows for professional collaboration and project management.

1️⃣ Branching Strategy

Why a development branch is useful

Recommended branch flow

main               # Always stable
   \
    development    # Active development branch
       \
        feature/<name>   # Feature or fix branches

Creating a branch safely

git checkout development
git pull origin development
git checkout -b feature/<name>

Creating the development branch

git checkout main
git pull origin main
git checkout -b development
git push -u origin development

2️⃣ Commit Best Practices

Use descriptive prefixes:

Keep commits small and focused.

Stage changes selectively:

git add -p

Amend commits if necessary:

git commit --amend

Reword commits:

git commit --amend -m "new message"

3️⃣ Pull Requests (PRs)

Push your branch:

git push origin feature/<name>

Open a PR:

Code review:

Merge PR:

Rebase or squash commits for clean history:

git fetch origin
git rebase origin/main

Typical flow:

  1. Create a feature branch from development.
  2. Open a PR from feature/<name> into development.
  3. Test and validate changes on development.
  4. Open a PR from development into main when ready to release.

Example merge flow:

git checkout -b feature/login development
# work, commit, push
git push origin feature/login

git checkout development
git pull origin development
git merge --no-ff feature/login
git push origin development

git checkout main
git pull origin main
git merge --no-ff development
git tag v0.1.0
git push origin main --tags

4️⃣ Advanced Git Techniques

Interactive rebase to clean up commit history:

git rebase -i HEAD~5

Cherry-pick a single commit across branches:

git cherry-pick <commit-hash>

Stashing changes when switching branches:

git stash
git checkout main
git stash pop

Reset to undo changes safely:

git reset --soft HEAD~1   # keep changes staged
git reset --hard HEAD~1   # discard changes

Safety note:

5️⃣ Collaboration & Hygiene

git fetch origin
git rebase origin/development

6️⃣ Summary