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
- Open the repo on GitHub.
- Click Fork (top-right).
- Choose your account and create the fork.
CLI Steps
gh repo fork owner/project --clone- Start working in the cloned fork.
Branching
Isolate work in a separate line of development.
CLI Steps
git branch feature/logingit switch feature/login
Web UI Steps
- In GitHub, open the branch dropdown.
- Type a new branch name, press Enter.
Pull Requests (PRs)
Request review before merging changes.
CLI Steps
git push origin feature/logingh pr create --title "Add login" --body "Implements auth UI"
Web UI Steps
- Click Compare & pull request.
- Add title/description, click Create PR.
Merging
Combine PR changes into the target branch.
CLI Steps
gh pr merge 42 --squashgit pull origin main
Web UI Steps
- Open the PR.
- Click Merge pull request.
Collaboration
Work together with reviews, issues, and teams.
Best Practice
- Use feature branches for new work.
- Require PR reviews before merging.
- Keep
mainstable and protected.
Syncing a Fork
Bring updates from the original repo into your fork.
CLI Steps
gh repo sync myuser/project-fork -b maingit pullin your local fork if needed.
Web UI Steps
- Open your fork on GitHub.
- 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
/distCommon Patterns
folder/ignores a directory*.logignores all .log files!important.logre-includes a file
Detailed Workflow 1: New Project -> GitHub
git initCreate 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=. --pushCreates 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 --cloneCreates your fork and downloads it locally.
git switch -c fix/typoWork 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 --squashMerge after review or approvals.
gh repo sync myuser/project -b mainBring 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)
- Git overview
- Git commands
- gitignore
- GitHub basics
- Branches
- Pull request merges
- Syncing forks
- GitHub CLI manual
- Alternatives: GitLab, Bitbucket, Azure DevOps, SourceHut, Gitea, Forgejo
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:
- GitHub Official Getting Started Guide - The primary source for all things GitHub.
- Pro Git Book - The ultimate, free, and comprehensive book on Git.
- Learn Git Branching - A highly visual and interactive tutorial for understanding Git branches and merges.
- GitHub Cheat Sheet - A handy reference guide for Git commands.
- Atlassian Git Tutorials - Excellent step-by-step guides covering advanced Git workflows.