Intermediate · Async JavaScript, modules and tools · topic 44 of 64 · 40 min
Git and GitHub
Explanation
Why version control?
Without it, projects end up as folders named site-final, site-final-2 and site-REAL-final. Version control is a tool that records every saved change: who changed what, when and why. Git is the version control system almost every company uses. GitHub is a website that hosts Git projects online.
Set up once, then the daily loop
git --version # check Git is installed
git config --global user.name "Asha Rao"
git config --global user.email "asha@example.com"
git config --global init.defaultBranch maincd kirana-shop
git init # start tracking this folder (creates .git)
git status # what changed? what is staged?
git add index.html css/ # stage chosen files
git add . # or stage everything
git commit -m "Add product list page"
git log --oneline # short history: one commit per line
git diff # changes not staged yet| Area | What it is | Moved there by |
|---|---|---|
| Working folder | Your files as you edit them | Saving in the editor |
| Staging area | Changes chosen for the next commit | git add |
| Repository (history) | Saved snapshots with messages | git commit |
Good commit messages and .gitignore
- Small commits: one idea per commit.
- First line (the subject): a short summary in the imperative, like a command, such as "Add cart total" or "Fix PIN code check". Keep it under about 72 characters, with no full stop at the end.
- If you need more, leave one blank line, then explain why in a body.
- Bad: "changes", "update", "final fix 2".
# .gitignore
node_modules/
dist/
.env
.env.local
.DS_StoreBranches, GitHub and pull requests
A branch is a separate line of work. main holds the working code. For each new feature you make a branch, commit there, and merge it into main when it is ready.
git switch -c feature/dark-mode # create a branch and move to it
# ...edit, add, commit...
git switch main # back to main
git merge feature/dark-mode # bring the feature into main
git branch -d feature/dark-mode # delete the finished branch# after creating an empty repository on GitHub:
git remote add origin https://github.com/asha/kirana-shop.git
git push -u origin main # send your commits (the first time)
git push # later pushes
git pull # get your teammates' new commits
git clone https://github.com/asha/kirana-shop.git # copy a project to a new computerIn a team, you do not push straight to main. You push your branch and open a pull request (PR) on GitHub: a request to merge it into main. Teammates read the changes, comment on lines, and approve. Automatic checks (tests, linting) can run on every PR. When all is good, the PR is merged.
Resolving a merge conflict
<<<<<<< HEAD
<h1>Sharma Kirana Store</h1>
=======
<h1>Sharma General Store</h1>
>>>>>>> feature/new-name- Open the file and decide what the final code should be (one side, the other, or a mix).
- Delete the <<<<<<<, ======= and >>>>>>> marker lines.
- Save, then git add the file.
- git commit to finish the merge, and test that the page still works.
Watch the lesson
Git and GitHub · 1/7
Git records every saved version of your project. GitHub keeps a copy online.
Prefer a video by a teacher? Find free videos on “git and github tutorial for beginners branches pull requests” on YouTube ↗
In real life: A fresher's portfolio on GitHub
A final-year student keeps every practice project on GitHub. Each project has small commits with clear messages, a .gitignore that hides node_modules and .env, and a README with screenshots and a live link. When she applies for a front-end internship, the interviewer opens her GitHub, sees steady work over months, and asks about a pull request where a friend reviewed her code. Her commit history is proof that she really built it.
Practice (0/3 done)
Install Git (git-scm.com), set your name and email, and turn one of your earlier projects into a repository. Add a .gitignore, then make at least three small commits with clear messages (for example: add the page, add the styles, fix a bug). Look at your history with git log --oneline and at an unstaged change with git diff.
Write commitProblems(message) that checks a commit message and returns a list of problems, in this order: "too-long" if the first line (the subject, trimmed) is longer than 72 characters; "ends-with-period" if the subject ends with "."; "no-blank-line" if there is a second line and it is not empty. If the subject is empty, return just ["empty"]. A good message returns []. Example: "Fix login bug\nThe OTP box was hidden" gives ["no-blank-line"].
Put your project on GitHub and practise the team flow. Create a repository on GitHub, add it as origin and push main. Then make a branch, change the page heading, push the branch and open a pull request; review your own changes on the Files changed tab and merge it. Finally, cause a merge conflict on purpose: change the same heading differently on main and on a new branch, merge, and resolve it by hand.
Quick quiz
Read more (free)
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.