Unays Learn ยท Tools

Git basics: version control for beginners

Git records the history of your project, so you can see what changed, undo mistakes and work with other people without overwriting each other. It is used by almost every software team.

Why version control?

Without version control, projects end up with files like final.html, final2.html and final-really.html. Git replaces that with a timeline of commits: saved snapshots of the whole project, each with a message explaining what changed and why.

Start a repository

After installing Git, tell it who you are once, then create a repository in your project folder:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

cd my-website
git init

git init creates a hidden .git folder where the history is stored.

The everyday workflow

git status                 # what has changed?
git add index.html style.css   # choose changes for the next commit
git commit -m "Add contact form"
git log --oneline          # see the history

Think of it in three places: your working folder (files you edit), the staging area (changes you chose with git add) and the history (commits). Commit small, related changes with clear messages, such as "Fix menu on phones", not "stuff".

Seeing and undoing changes

git diff                      # changes not yet staged
git restore index.html        # throw away unsaved edits to a file
git restore --staged index.html  # un-stage, keep the edits
git revert <commit-id>        # make a new commit that undoes an old one

git restore on a file discards your edits permanently, so check git diff first. git revert is the safe way to undo a commit that others may already have.

Branches

A branch is a separate line of work. Try an idea without breaking the main version:

git switch -c dark-mode    # create and move to a new branch
# ...edit and commit...
git switch main            # back to the main branch
git merge dark-mode        # bring the work in

If both branches changed the same lines, Git reports a merge conflict and marks the lines in the file. Edit the file to keep the right version, then git add and git commit.

Sharing with a remote

Services such as GitHub, GitLab and Codeberg host a copy of your repository online, called a remote:

git clone https://example.com/team/project.git   # copy a project
git pull     # get other people's new commits
git push     # send your commits

Never commit passwords or API keys. List files that should stay private, such as .env, in a .gitignore file.

Try it yourself

Install Git, put a page you made in Unays Code into a folder, and make three commits as you improve it. Then create a branch, change the colours, and merge it back.

This tutorial was written with AI assistance and reviewed by Unays. The runnable code examples were tested before publishing. Spotted a mistake? Tell us at [email protected].