Technology

Git & GitHub

A comprehensive guide to version control with Git and collaboration on GitHub.

TL;DR

  • Git: A distributed version control system that tracks changes and enables safe collaboration.
  • GitHub: A hosted platform for Git repos + collaboration tools (PRs, Issues, Actions).
  • Core Flow: Branch -> Commit -> Push -> Pull Request -> Review -> Merge.
  • Power Move: Use forks for open source, branches for features, and PRs for clean reviews.

Meaning & Purpose

Git (Meaning)

Git is a distributed version control system that records snapshots of your project so you can track changes, roll back safely, and collaborate without overwriting others.

  • Keep history of every change
  • Work offline with full project history
  • Resolve conflicts with confidence

GitHub (Meaning)

GitHub is a cloud platform that hosts Git repositories and adds collaboration features like pull requests, issues, code review, and automation.

  • Share code with teams or the public
  • Review changes with PRs
  • Automate tests & deployments

Use Cases (When & Why)

Solo Developer

Track versions, experiment with branches, and revert mistakes safely.

Team Collaboration

Everyone works on their own branches, submits PRs, and merges only reviewed code.

Open Source

Fork a project, contribute via PRs, and sync your fork with upstream changes.

Backup & History

Even if your laptop crashes, your repo and full history live on GitHub.


Git vs GitHub (Quick Comparison)

Git

  • Version control system
  • Runs locally on your machine
  • Manages commits, branches, merges
  • Works offline

GitHub

  • Hosted platform for Git repositories
  • Adds collaboration features
  • Pull requests, code review, issues
  • CI/CD and automation

Core Git Commands (Syntax + Examples)

git init

Syntax: git init Example: git init

Start a new Git repository in the current folder.

git clone

Syntax: git clone <url> Example: git clone https://github.com/user/repo.git

Download a remote repository to your machine.

git status

Syntax: git status Example: git status

See modified files and current branch state.

git add

Syntax: git add <file|folder> Example: git add src/

Stage changes so they can be committed.

git commit

Syntax: git commit -m "message" Example: git commit -m "Add login page"

Save a snapshot of staged changes.

git log

Syntax: git log Example: git log --oneline --graph

Show commit history in detail or compact form.

git diff

Syntax: git diff Example: git diff HEAD~1

View changes between commits or working tree.

git branch

Syntax: git branch <name> Example: git branch feature/auth

Create or list branches.

git switch

Syntax: git switch <name> Example: git switch feature/auth

Move to another branch (newer alternative to checkout).

git merge

Syntax: git merge <branch> Example: git merge feature/auth

Combine changes from another branch into current.

git pull

Syntax: git pull Example: git pull origin main

Fetch + merge changes from a remote branch.

git push

Syntax: git push <remote> <branch> Example: git push origin main

Upload your local commits to a remote repository.


GitHub CLI Commands (Syntax + Examples)

gh auth login

Syntax: gh auth login Example: gh auth login

Authenticate the GitHub CLI with your account.

gh repo create

Syntax: gh repo create <name> [flags] Example: gh repo create notes --public --source=. --push

Create a GitHub repo and optionally push local code.

gh repo fork

Syntax: gh repo fork <repo> [flags] Example: gh repo fork owner/project --clone

Fork a repo to your account and optionally clone it.

gh repo sync

Syntax: gh repo sync <repo> [flags] Example: gh repo sync myuser/project-fork -b main

Sync your fork with the upstream repository.

gh pr create

Syntax: gh pr create [flags] Example: gh pr create --title "Fix nav" --body "Updates links"

Create a pull request from your current branch.

gh pr merge

Syntax: gh pr merge <number> [flags] Example: gh pr merge 42 --squash --delete-branch

Merge a PR with a chosen merge strategy.


GitHub Features & How to Use Them

Forking

Create your own copy of someone else's repo.

Web UI Steps

  1. Open the repo on GitHub.
  2. Click Fork (top-right).
  3. Choose your account and create the fork.

CLI Steps

  1. gh repo fork owner/project --clone
  2. Start working in the cloned fork.

Branching

Isolate work in a separate line of development.

CLI Steps

  1. git branch feature/login
  2. git switch feature/login

Web UI Steps

  1. In GitHub, open the branch dropdown.
  2. Type a new branch name, press Enter.

Pull Requests (PRs)

Request review before merging changes.

CLI Steps

  1. git push origin feature/login
  2. gh pr create --title "Add login" --body "Implements auth UI"

Web UI Steps

  1. Click Compare & pull request.
  2. Add title/description, click Create PR.

Merging

Combine PR changes into the target branch.

CLI Steps

  1. gh pr merge 42 --squash
  2. git pull origin main

Web UI Steps

  1. Open the PR.
  2. Click Merge pull request.

Collaboration

Work together with reviews, issues, and teams.

Best Practice

  1. Use feature branches for new work.
  2. Require PR reviews before merging.
  3. Keep main stable and protected.

Syncing a Fork

Bring updates from the original repo into your fork.

CLI Steps

  1. gh repo sync myuser/project-fork -b main
  2. git pull in your local fork if needed.

Web UI Steps

  1. Open your fork on GitHub.
  2. Click Sync fork -> Update branch.

.gitignore (What & Why)

.gitignore

.gitignore is a file that tells Git which files or folders to ignore, so you don't accidentally commit build output, secrets, or local config.

Example .gitignore

node_modules/
.env
.DS_Store
/dist

Common Patterns

  • folder/ ignores a directory
  • *.log ignores all .log files
  • !important.log re-includes a file

Detailed Workflow 1: New Project -> GitHub

git init

Create a new Git repository locally.

git add .

Stage all files for commit.

git commit -m "Initial commit"

Save the first snapshot.

gh repo create my-project --public --source=. --push

Creates the remote repo and pushes your code.

Open GitHub and confirm the files appear.


Detailed Workflow 2: Fork -> Branch -> PR -> Merge -> Sync

gh repo fork owner/project --clone

Creates your fork and downloads it locally.

git switch -c fix/typo

Work in a clean, isolated branch.

git add .
git commit -m "Fix typo"

Save your update.

git push origin fix/typo
gh pr create --title "Fix typo" --body "Small text correction"

Submit your changes for review.

gh pr merge 123 --squash

Merge after review or approvals.

gh repo sync myuser/project -b main

Bring upstream changes back into your fork.


Other Git Hosting Platforms

GitLab

All-in-one DevOps platform with Git hosting.

Bitbucket

Git hosting with deep Atlassian integration.

Azure DevOps

Microsoft's repo + CI/CD + boards suite.

SourceHut

Minimalist, developer-focused Git hosting.

Gitea

Lightweight self-hosted Git service.

Forgejo

Community-driven fork of Gitea.


Sources (Official Docs)


Key Takeaways

  • Git manages history; GitHub manages collaboration.
  • Branches isolate work, PRs review it, merges finalize it.
  • Forks power open source contributions and safe experimentation.

External Resources & Deep Dives

Looking to master Git and GitHub? Explore these highly recommended external resources:

On this page