Lesson 17: Git and GitHub: Version Control for Real Projects
Sooner or later, every developer loses work: a file overwritten by accident, a change that breaks everything with no way back, or two people editing the same file and destroying each other's work. Version control exists to stop that happening, and Git is the version control system almost the whole industry uses. Git records snapshots of your project, called commits, each with a message saying what changed, so you can look back at any earlier version, compare two versions, and undo a mistake without losing everything else. GitHub is a website that hosts Git projects, so you can back your work up, share it, and work with others. This lesson teaches the everyday Git workflow, which you will use for every project from now on, including your capstone.
Repositories, commits and the working tree
A repository, or repo, is a project folder that Git is watching, created with git init or copied from GitHub with git clone. Inside it, your files are in one of three states: the working tree, the files as they are on your disk; the staging area, where you place changes you want to record, with git add; and the repository itself, where commits live, created with git commit. The workflow is always the same: change files, check what changed with git status, add the changes with git add, and record them with git commit -m "Add the contact page", writing a message that says what changed and why. Commit often, in small steps, so each commit is one idea: a commit that does one thing is easy to understand, easy to review and easy to undo, while a giant commit that does everything is none of those.
Branches: safe places to experiment
A branch is a separate line of work inside the same repository, and it lets you try something risky without touching the main version of the project. You create one with git branch feature-name or git checkout -b feature-name, work on it, commit as usual, and when the work is ready, you merge it back into the main branch with git merge feature-name. The main branch should always be in a working state, and new work happens on branches, which is how teams keep a site live while building new features. If an experiment fails, you can simply abandon the branch, and the main version is untouched. Branches are also how you keep your capstone organised: one branch per feature, merged when the feature works, with a clear commit history that tells the story of how the project was built.
git init
git add .
git commit -m "Start the course catalogue"
git branch search-feature
git checkout search-feature
# ... make changes, commit them ...
git checkout main
git merge search-feature
git push origin main
GitHub: backup, sharing and collaboration
Git on your own computer protects you from mistakes, and GitHub adds backup and collaboration. You connect a local repo to GitHub with git remote add origin, using the address GitHub gives you, and git push sends your commits to GitHub, while git pull brings down commits others have made. If two people change the same lines, Git reports a merge conflict, which is not a disaster: it marks the conflicting lines in the file, you choose which version to keep, or combine them, and then you commit the result. GitHub also hosts your published site, which you will use in Lesson 19, and it shows your work to others, which is why many developers keep a public profile of projects there. One warning: never commit secrets, such as passwords or API keys, because anything pushed to GitHub can be read by others, and removing it later is hard, since Git remembers everything.
A workflow you can use every day
Here is the daily workflow in full. Start by pulling the latest changes with git pull, so you are working on the newest version. Create a branch for the feature you are about to build, make your changes in small steps, and commit each step with a clear message. When the feature works, switch back to the main branch, merge your branch in, and push to GitHub. If you make a mistake, git log shows your recent commits, git diff shows what changed, and git revert undoes a commit safely by creating a new commit that reverses it, which is safer than deleting history. Practise this workflow on a small project before your capstone, until the commands feel automatic, because version control is not a separate skill from coding: it is part of coding, and the developers who use it well are the ones who sleep well.
Key Terms
- Git
- A version control system that records snapshots of a project so you can look back, compare and undo.
- Repository
- A project folder that Git is watching, holding the files and their whole history.
- Commit
- A snapshot of the project at one moment, with a message saying what changed.
- Staging area
- The place where changes wait, added with git add, before they are recorded in a commit.
- Branch
- A separate line of work in a repository, for building a feature without touching the main version.
- Merge
- Bringing the changes from one branch into another.
- Merge conflict
- What happens when two people change the same lines; Git marks them and asks you to choose.
Discussion Questions
- Why is it better to commit in small steps with clear messages than to commit rarely in giant steps?
- A teammate's push conflicts with your changes. Is that a disaster? What should you do, step by step?
- Why must secrets never be committed to Git, even in a private repository?
Practice Questions
- What do git add and git commit do, and why are they separate steps?
- What is a branch for?
- What does git push do?
- What is a merge conflict, and how do you resolve one?
- Why is git revert safer than deleting a commit from history?
Show answers
- git add stages changes and git commit records them. Staging lets you choose exactly which changes go into the next snapshot, so a commit holds one idea.
- It is a separate line of work for building a feature safely. You can experiment on a branch without touching the main version, and merge it back when it works.
- It sends your commits to a remote repository such as GitHub. That backs your work up and shares it with others.
- It happens when two people change the same lines. Git marks the conflicting lines in the file, you choose which version to keep or combine them, then commit the result.
- It undoes a change by creating a new commit that reverses it. The history stays complete, so nothing is lost and collaborators are not confused.