Notes › Web Development
Git and GitHub Essentials
Git tracks changes to your files so you can go back in time, work on features in parallel and collaborate. GitHub hosts Git repositories online and adds pull requests, issues and CI.
The three areas
working directory ──git add──► staging area ──git commit──► repository (history)
First-time setup
git config --global user.name "Asha Sharma"
git config --global user.email "asha@example.com"
Everyday commands
git init # start tracking a folder
git clone <url> # copy a repo from GitHub
git status # what changed?
git add file.js # stage one file
git add . # stage everything
git commit -m "Add login form" # save a snapshot
git log --oneline --graph # history
git diff # unstaged changes
git diff --staged # staged changes
Good commit messages: short, imperative, explain what and why — "Fix crash when cart is empty", not "changes".
Branches
A branch is a movable pointer to a commit — work on a feature without touching main.
git branch # list
git switch -c feature/login # create + switch (older: git checkout -b)
git switch main
git merge feature/login # bring the feature into main
git branch -d feature/login # delete after merging
Merge vs rebase
| Merge | Rebase | |
|---|---|---|
| History | keeps both lines + a merge commit | replays your commits on top — straight line |
| Safe on shared branches | ✅ | ❌ never rebase commits others already pulled |
| Command | git merge main |
git rebase main |
Merge conflicts
When two branches change the same lines, Git marks them:
<<<<<<< HEAD
const limit = 10;
=======
const limit = 20;
>>>>>>> feature/limits
Edit the file to the correct version, delete the markers, then git add and git commit (or git rebase --continue).
Remotes
git remote add origin https://github.com/you/codify.git
git push -u origin main # first push sets the upstream
git push # later pushes
git pull # fetch + merge (or --rebase)
git fetch # download without merging
Undoing things
| Situation | Command |
|---|---|
| Discard changes in a file | git restore file.js |
| Unstage a file | git restore --staged file.js |
| Change the last commit message / add a forgotten file | git commit --amend (only if not pushed) |
| Undo a commit safely (creates a new commit) | git revert <hash> |
| Move the branch back, keep changes | git reset --soft HEAD~1 |
| Move back and throw away changes | git reset --hard HEAD~1 ⚠️ |
| Save work temporarily | git stash / git stash pop |
| Find a lost commit | git reflog |
Use revert on shared branches; reset rewrites history. |
.gitignore
node_modules/
.env
dist/
*.log
.DS_Store
Never commit secrets. If you did, rotate the secret — removing it from the latest commit is not enough, it stays in history.
GitHub workflow (pull requests)
- Create a branch → commit →
git push -u origin feature/x. - Open a pull request on GitHub; teammates review and comment.
- CI runs tests; fix and push more commits.
- Merge (merge commit, squash, or rebase) and delete the branch.
Open source: fork the repo, clone your fork, branch, push, and open a PR to the original.
Interview one-liners
- Git is distributed — every clone has the full history.
git fetchdownloads;git pulldownloads and merges.revertis safe for shared history;resetrewrites it.HEADis the commit you currently have checked out.- More: Git documentation and Git cheat sheet.