>_
EngineeringNotes
Back to Additional Modules

Git & GitHub Essentials

A beginner's guide to version control and collaboration.

What is Git?

Git is a distributed version control system. Think of it as a "save button" on steroids for your code history.

It tracks changes in your files, so you have a record of what has been done, and you can revert to specific versions should you ever need to.

Why use Git?
  • History: You can see every change ever made.
  • Backup: If you mess up, you can undo changes easily.
  • Experimentation: Try new things in a separate "branch" without breaking your main code.
What is GitHub?

While Git is the tool you use locally, GitHub is a website where you upload your Git repositories.

  • Cloud backup for your code.
  • Collaboration with other developers.
  • Showcase your portfolio.

Basic Commands

1. Setup & Initialization

First, tell Git who you are (run this once per computer):

Terminal
bash
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

To start using Git in your current project folder:

Terminal
bash
git init

2. Saving Changes

Saving in Git is a two-step process: Add (stage) and Commit (save).

Terminal
bash
# Check which files have changed
git status

# Add specific file
git add filename.txt

# OR Add all changed files
git add .

# Save the changes with a message
git commit -m "Added a new feature"

3. Branching

Branches allow you to work on different features simultaneously.

Terminal
bash
# Create a new branch named 'feature-login'
git branch feature-login

# Switch to that branch
git checkout feature-login

# SHORTCUT: Create and switch in one command
git checkout -b feature-login

Once you finish your work on the branch, you can merge it back to main (usually done via Pull Request on GitHub, or locally).

4. Pushing to GitHub

Link your local folder to a repository you created on GitHub.

Terminal
bash
# Link remote repository (do this once)
git remote add origin https://github.com/username/repo-name.git

# Rename main branch to 'main' (if not already)
git branch -M main

# Push your code to GitHub
git push -u origin main

Pro Tip

Always pull the latest changes before you start working if you are collaborating with others:
git pull origin main

Handling Conflicts & Syncing

Scenario 1: Push Rejected (Remote has changes)

! [rejected] main -> main (fetch first)
error: failed to push some refs to '...'

This happens when someone else pushed code while you were working. You need to pull their changes before you can push yours.

Option A: Merge (Default)

Creates a "Merge commit" joining the histories. Simple but messy history.

Terminal
bash
git pull origin main
Option B: Rebase (Cleaner)

Moves your commits on top of the remote changes. Linear history.

Terminal
bash
git pull --rebase origin main

Scenario 2: Merge Conflicts (Same file changed)

If you and your teammate edited the same lines in the same file, Git won't know whose to keep. It will pause and ask you to fix it.

  1. Open the conflicted file(s) (VS Code highlights them).
  2. Look for <<<<<<< HEAD markers.
  3. Choose "Accept Current Change" or "Accept Incoming Change".
  4. Save the file.
  5. Run: git add . to mark as resolved.
  6. Run: git rebase --continue (if rebasing) or commit (if merging).

Maintenance & Cleanup

Remove file from Git (but keep local)

Useful if you accidentally committed `.env` or `node_modules` and then added it to `.gitignore`.

Terminal
bash
git rm --cached filename

For folders: git rm -r --cached foldername

Remove Git completely

To stop tracking the project entirely (un-init). This deletes the history but keeps your files.

Terminal
bash
# Windows (PowerShell)
rm -r -force .git

# Mac/Linux
rm -rf .git

Common Errors

Error: Permission denied (publickey)

Fix: Your SSH keys aren't set up. Use HTTPS url instead or generate SSH keys and add them to GitHub.

Error: src refspec main does not match any

Fix: You haven't made a commit yet! Run git commit -m "init" first. usage of empty repo cannot be pushed.

Error: failed to push some refs... updates were rejected

Fix: Remote has changes you don't have. Run git pull origin main (or --rebase).