Version Control with Git & GitHub
Git is the industry-standard version control system. Every developer job requires Git. It tracks code changes, enables collaboration, and provides a safety net — you can always revert to a previous version.
Git Essentials
- git init — Initialize a repository in your project folder
- git add . — Stage all changes for commit
- git commit -m 'message' — Save staged changes with a description
- git push — Upload commits to remote (GitHub, GitLab)
- git pull — Download latest changes from remote
- git branch / git checkout — Create and switch branches for features
- .gitignore — List files to exclude: node_modules/, .env, dist/
- Pull Request (PR) — Propose changes for review before merging to main branch
Git Workflow Code
// Essential Git commands
// Setup (one time)
// git init // start tracking
// git remote add origin <url> // connect to GitHub
// Daily workflow
// git status // what changed?
// git add . // stage all changes
// git commit -m "Add login feature" // save snapshot
// git push origin main // upload to GitHub
// Branching (feature development)
// git branch feature/login // create branch
// git checkout feature/login // switch to branch
// git checkout -b feature/signup // create AND switch
// After feature is done:
// git checkout main // switch to main
// git merge feature/login // merge feature in
// git push origin main // upload
// Undo mistakes
// git revert HEAD // undo last commit (safe)
// git stash // temporarily save changes
// git stash pop // restore saved changes
// git log --oneline -5 // view last 5 commits
// .gitignore file
// node_modules/
// .env
// dist/
// .DS_Store
// *.log
console.log("Git commands for daily development workflow");
console.log("Learn git: https://learngitbranching.js.org");Tip
Tip
Write descriptive commit messages: 'fix: resolve login redirect loop on expired tokens' is much better than 'fix bug'. Use conventional commits (feat:, fix:, docs:, refactor:) for a clean, searchable history.
Git tracks changes through add → commit → push workflow
Common Mistake
Warning
Committing directly to main/master branch. Always create a feature branch, make changes there, then submit a pull request. This prevents broken code from reaching production and enables code review.
Practice Task
Note
Git workflow: (1) Initialize a repo, create a branch, make 3 commits with good messages. (2) Merge the branch back. (3) Practice resolving a merge conflict. (4) Use git log --oneline to review history.
Quick Quiz
Key Takeaways
- Git is the industry-standard version control system.
- git init — Initialize a repository in your project folder
- git add . — Stage all changes for commit
- git commit -m 'message' — Save staged changes with a description