By the end of this lesson you will be able to
- Create a project folder with separate places for pages, styles, scripts and images.
- Name a code editor suited to the job and say what it adds over a plain text editor.
- Test a page locally in a browser and refresh to see changes.
- Explain repository, commit and branch in plain words.
- Create a Git repository and save a commit using git status, add and commit.
A single page is easy to manage. A website with several pages, a stylesheet, a script and some images quickly becomes a pile of files, and one wrong edit can break something that worked yesterday. Professionals deal with this using two habits: a tidy folder structure and version control. This lesson teaches both, using small, safe steps in a practice folder.
A tidy project folder
A website project is just a Folder of files. What matters is that you can find things and that the links between files keep working. A common simple layout puts the home page at the top, called index.html because web servers show that file first when someone visits a folder, and gives other kinds of file their own subfolders.
- my-siteProject folder
- index.htmlHome page
- about.htmlSecond page
- css
- style.cssLooks
- js
- script.jsBehaviour
- images
- photo.jpgPictures
Read this figure as text
A folder tree for a website project with index.html, an about page, and css, js and images subfolders.
A folder named my-site contains index.html, the home page, and about.html, a second page. It also contains three subfolders. css holds style.css. js holds script.js. images holds photo.jpg. In index.html the stylesheet is linked as css/style.css, the script as js/script.js and the photo as images/photo.jpg. Those paths start from the folder that holds the page.
Follow a few naming rules from the start: use lower-case letters, no spaces (use a hyphen, as in my-photo.jpg), and no accents or odd symbols. Web addresses and Linux treat Photo.JPG and photo.jpg as different files, so a link that works on a Windows laptop can break after publishing. A path such as css/style.css is a relative path: it starts from the page that contains it.
<link rel="stylesheet" href="css/style.css">
<img src="images/photo.jpg" alt="Amina holding a basket of tomatoes">
<a href="about.html">About me</a>
<script src="js/script.js" defer></script>Choosing a code editor
You can write code in Text Editor or Notepad, and for a first page that is fine. A code editor adds things that save time and prevent mistakes: colours for different parts of the code, matching brackets, automatic indentation, a file list for the whole project and search across all files. Visual Studio Code is a free and very widely used editor for Ubuntu and Windows. Others exist, such as Sublime Text and, on Linux, GNOME Text Editor for lighter use. Choose one and learn it well rather than trying many.
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <title>Amina's page</title>
5 <link rel="stylesheet" href="css/style.css">
- Marker 1: Project file list
- Marker 2: Code with line numbers
- Marker 3: Save your changes
Read this figure as text
A simulated editor window with a file list on the left, an open index.html and numbered callouts.
A simulated code editor. The left side bar lists the project files: index.html (selected), about.html, css folder and images folder. The main area shows index.html with lines of HTML code. Marker 1 points to the file list, marker 2 to the open file's code, marker 3 to the Save button below. Editors usually add colour to tags and show line numbers.
Testing on your own computer
You do not need the internet to test a website. Open index.html in a browser (double-click it or drag it onto the browser window) and the browser reads it straight from your disk. The address bar will start with file://. The working routine is: edit, save, switch to the browser, refresh with F5, look. If a change does not appear, a hard refresh with Ctrl+Shift+R asks the browser to ignore its stored copy of the page.
Version control with Git
Have you ever saved essay-final, essay-final2 and essay-really-final? Git solves that problem properly. It is a free tool that records the history of a project folder. A folder tracked by Git is called a Repository, or repo. Each time you reach a good point you make a commit: a saved snapshot plus a short message saying what changed. You can look back at any commit and return to it if something breaks.
A branch is a separate line of work. The main line is usually called main. To try a risky idea you create a branch, work there, and either merge it back into main if it works or throw it away if it does not. GitHub is a website that stores copies of repositories online, which gives you an off-site backup and a way to share and publish. Git is the tool on your computer; GitHub is one place to keep a copy. They are not the same thing.
- Step 1: Edit filesyour folder
- Step 2: git addchoose changes
- Step 3: git commitsave snapshot
- Step 4: git pushcopy to GitHub
Read this figure as text
A flow of four steps: edit files, git add, git commit, git push.
Four numbered boxes. 1: you edit files in your working folder. 2: git add chooses which changed files go into the next snapshot. 3: git commit saves the snapshot with a message, on your computer only. 4: git push sends your commits to the copy on GitHub. Steps 2 and 3 happen on your computer; step 4 needs the internet and a GitHub account.
- Folder plus its history
- Like a notebook with every page kept
- One saved snapshot
- Has a message
- Like a dated page
- A separate line of work
- main is the usual first one
- Like a photocopy to experiment on
Read this figure as text
Three columns explaining repository, commit and branch with a plain comparison each.
Three columns. Repository: a project folder with its whole history, like a notebook with every page you have ever written. Commit: one saved snapshot with a message, like dating and signing a page. Branch: a separate line of work, like photocopying the notebook to try changes without spoiling the original.
Your first repository
Install Git and make a repository
Check whether Git is already installed by typing git --version first. If it prints a version number, skip the install.
On Ubuntu, Git is installed from the package manager. sudo runs one command as administrator, so you will be asked for your own account password. Type it only at that real system prompt, nowhere else. apt install git downloads and installs Git.
git --version
sudo apt install gitTell Git who you are once. These two lines store your name and email in Git's settings so commits are labelled. Use any name; the email may be a made-up one if you do not want it to appear anywhere public later.
git config --global user.name "Amina Wanjiru"
git config --global user.email "amina@example.com"Now create a practice project and turn it into a repository. mkdir -p makes the folder, cd enters it, git init starts tracking it and git branch -M main makes sure the first branch is called main, whatever your Git's default is.
mkdir -p ~/practice/my-site
cd ~/practice/my-site
git init
git branch -M mainOn Windows 11, download Git for Windows from the official site git-scm.com and run the installer, accepting the default options. When it finishes, open Git Bash or PowerShell from Start. The commands below work the same in both. First check the installation.
git --versionTell Git who you are once. Use any name; the email may be a made-up one if you do not want it to appear in public later.
git config --global user.name "Amina Wanjiru"
git config --global user.email "amina@example.com"Create a practice project and turn it into a repository. New-Item -ItemType Directory makes the folder, Set-Location enters it, git init starts tracking it and git branch -M main makes sure the first branch is called main.
New-Item -ItemType Directory -Force $HOME\practice\my-site
Set-Location $HOME\practice\my-site
git init
git branch -M mainCreate an index.html file inside my-site with your editor (any content, for example a heading), save it, then ask Git what it sees. git status is the command you will use most: it shows which files are new, changed and ready to be saved. Then git add index.html puts the file in the next snapshot, and git commit -m "..." saves it with your message.
git status
git add index.html
git commit -m "Add home page"
git log --oneline- Marker 1: Git sees the file but is not tracking it yet
- Marker 2: -m gives the commit message
- Marker 3: The new snapshot, newest first
Read this figure as text
A simulated terminal with git status, git add, git commit and git log commands and their outputs.
A simulated bash terminal in the folder ~/practice/my-site. The command git status prints that index.html is untracked. The command git add index.html prints nothing. The command git commit -m Add home page prints a line starting with main and a short code, then 1 file changed. The command git log --oneline prints one line: a seven-character code followed by Add home page. Marker 1 is the untracked warning, marker 2 the commit message and marker 3 the log line.
To make and try a branch, git switch -c new-colours creates and moves to a branch of that name, and git switch main returns. To put the project on GitHub, create an empty repository on the website, then follow the two or three lines it shows, which look like git remote add origin <address> and git push -u origin main. GitHub no longer accepts your account password for pushes from the command line; it asks for a token or a browser sign-in, and the page will explain the current method. We do that as part of the next lesson.
Common mistakes
✖ Instead of: Naming files with spaces and capitals, such as
My Page.HTML.✔ Do this: Use lower-case words joined by hyphens:
my-page.html.✖ Instead of: Running
git commitwithoutgit addfirst, then seeingnothing added to commit.✔ Do this: Run
git add <file>to choose what goes in the snapshot, then commit.✖ Instead of: Making one huge commit at the end of the week with the message
stuff.✔ Do this: Commit small, working steps with messages that say what changed.
✖ Instead of: Pushing a file with a password in it and then deleting it in a later commit.
✔ Do this: The old commit still holds it. Keep secrets out from the start, and change any password that was exposed.
If something goes wrong
- Git says
Author identity unknownorPlease tell me who you are. - Run the two
git config --globalcommands from the lesson with your name and email, then commit again. git: command not foundorgit is not recognized.- Git is not installed or the terminal was opened before installing. Install it, close and reopen the terminal, then run
git --version. - My CSS or image does not show when I open the page.
- Check the relative path in
hreforsrcagainst the real folder layout and the exact spelling, including capitals. Then save and refresh. git commitopens a strange text screen.- You left out
-m "message", so Git opened an editor for the message. Type a message, save and close it, or press Ctrl+C and retry with-m.
Key terms
- Code editor
- A text editor made for writing code, with colouring, helpful hints and tools for working on many files.
- Commit
- A saved snapshot of your project in Git, with a short message saying what changed.
- Branch
- A separate line of work in Git where you can try changes without disturbing the main version.
- GitHub
- A website that stores Git repositories online so they are backed up, shareable and can be published.
- Git
- A tool that records the history of changes to your project files so you can go back to earlier versions.
- Repository
- An online store of software or project files that tools can download from.
Practise it for real
Reading is not the same as being able to do it. Complete these on your own computer, then tick only what you have actually done. These ticks are your own honest record: nothing checks them for you.
Build the folder and make a commit
Create the my-site folder in your practice area with the subfolders css, js and images. Add index.html and css/style.css, link the stylesheet, open the page in your browser, then make a Git repository and commit the files with a clear message.
Challenge: try a change on a branch
Create a branch called new-colours, change the background colour in style.css, commit it, then switch back to main and refresh the page. Switch to the branch again and compare. Work out how the page can differ between the two without you copying any files.
Knowledge check
6 questions. You need 80% to pass. Mistakes here are part of learning, and you can retry as often as you like.
Summary
- Keep a project in one folder, with css, js and images subfolders and index.html at the top.
- Use lower-case names with hyphens and relative paths between files.
- A code editor helps with colours, brackets and project files; edit, save, refresh to test.
- Git records history: repository, commit and branch. add then commit saves a snapshot.
- GitHub keeps an online copy. Never commit secrets or private details.
Checklist: can you do these?
Marking a lesson as read does not prove you can do it. Skills count as demonstrated when you pass the quiz (not yet) and complete the practical tasks (not yet).
Related lessons
References and further reading
Official sources for details that can change between versions.
- Git documentation (Git)
- Download Git (Git)
- GitHub Docs: Get started (GitHub)
- Visual Studio Code documentation (Microsoft)