Introduction
Git is a distributed version control system used to track and manage source code. GitHub is a platform for hosting Git repositories online, used for collaboration, issues, pull requests, and project management.
Setup & Configuration
To install Git:
- Windows: Download from git-scm.com
- Mac: Use
brew install git
- Linux:
sudo apt install git
Configure your identity:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
Essential Git Commands
git init
– Start a repositorygit clone URL
– Copy a remote repogit add file
– Stage filesgit commit -m "msg"
– Save snapshotgit push/pull
– Sync with remotegit log
– View commit historygit status
– Show working tree state
Git File Lifecycle
Files move through these stages:
- Untracked → Staged → Committed → Modified
Branching & Merging
git branch
– List branchesgit branch name
– Create branchgit switch name
– Move to branchgit merge
– Merge two branchesgit checkout -b feature
– Create and switch
Git Stash
git stash
– Temporarily save uncommitted workgit stash list
– View stashed itemsgit stash apply
– Reapply changes
Restore & Reset
git restore file
– Discard changesgit reset --soft HEAD~1
– Undo last commitgit reset --hard
– Full reset to last commit
Rebase vs Merge
Merge: Preserves history and creates a merge commit.
Rebase: Rewrites history for a cleaner, linear timeline.
git rebase main
git merge feature
Git Squash
Combine multiple commits into one:
git rebase -i HEAD~n
→ Change pick
to squash
Tags & Releases
git tag v1.0
– Mark a releasegit tag -a v1.0 -m "release note"
git push origin v1.0
– Push tag
Git Workflows
Git Flow: Uses branches like main
, develop
, feature/
, release/
, hotfix/
.
GitHub Flow: Create branch → Work → PR → Review → Merge
GitHub Usage
- Fork repos to your account
- Clone using
git clone URL
- Push changes:
git push origin branch
- Create PR from GitHub UI
Open Source Contribution
- Fork the repo
- Create new branch
- Make changes and commit
- Push to your fork
- Create Pull Request (PR)
- Get reviewed & merged