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

Alternatively, pull with merge to preserve history:

git fetch origin
git pull --no-ff 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️⃣ Rebasing: When It's Safe and When It Isn't

Rebasing IS SAFE:

Rebasing IS DANGEROUS:

Why? Rebasing rewrites history. On shared branches, this breaks everyone's local copies and causes merge conflicts and lost work.

Safe rule: Rebase your feature branch before merging. Once merged into a shared branch, don't rebase it—use merges instead.

5️⃣ 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:

6️⃣ Collaboration & Hygiene

git fetch origin
git rebase origin/development

7️⃣ Summary

8️⃣ One-command migration (Fish shell)

If your repository has no stable release history yet, this command block moves current main into development and resets main to a clean start.

begin
  git fetch origin
  and git checkout main
  and git pull origin main
  and git checkout -B development
  and git push -u origin development
  and git checkout --orphan main-empty
  and git rm -rf . >/dev/null 2>&1
  and printf "# %s\n" (basename "$PWD") > README.md
  and git add README.md
  and git commit -m "chore: reset main to clean start"
  and git branch -M main
  and git push -f origin main
end

Safety notes:

git fetch origin
git checkout main
git reset --hard origin/main