Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

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)

  1. Create a branch → commit → git push -u origin feature/x.
  2. Open a pull request on GitHub; teammates review and comment.
  3. CI runs tests; fix and push more commits.
  4. 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

More Web Development notes