Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Keeping and sharing versions of your work (Git)

After this lesson: I can keep and share versions of my work.

45 min lesson10 commandsGit and Gitea lab: learn clone, commit and push on a Git server of your own

Too many files called final

Three friends build a website for a local café. They email files to each other. Soon there are files called menu_final, menu_final2 and menu_final_v3. Nobody knows which one is newest.

Then somebody saves over a friend's work and it is lost. Git keeps every version, with a note of who changed what and why.

By the end of this lesson

Note

I can keep and share versions of my work.

Key words

WordWhat it meansExample
Repositorya folder whose whole history Git keeps
Commita saved version, with a note of who and why
Clonea full copy of a repository from a server
Stagechoose which changes go into the next commitgit add hours.txt
Remotethe copy of a repository on a server
Pushsend your commits to the remote

The cells students fill in are left blank in this preview.

Before you start

Note

Press Start the lab on the bar at the top and wait until it says it is ready. Your lab has its own Git server, called gitea, holding one repository. Nothing you do reaches anybody else.

A repository keeps every version

A repository is a folder plus its whole history. Each saved version is a commit. A commit has a short id, an author, a date and a message.

The first Run makes a clone, your own full copy, in a folder called look. The second lists its commits, newest first.

Clone the repository into look

Command
git clone http://gitea:3000/student/hello-git.git look

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

List the commits, one a line

Command
git -C look --no-pager log --format="%h %an %ad %s" --date=short

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Stage first, then commit

Now make a repository of your own, called practice, and save a first version in it.

Git saves in two steps. git add stages a file: it puts it in the next commit. git commit then saves everything staged as one version. Git also needs a name and an email address to write on each commit.

Make a new repository

Command
git init -b main practice

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Give Git your name

Command
git -C practice config user.name "Student"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Give Git an email address

Command
git -C practice config user.email "[email protected]"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Write a file

Command
printf 'Open 9 to 5\n' > practice/hours.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Ask Git what it sees

Command
git -C practice status --short

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Stage the file

Command
git -C practice add hours.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Save the first version

Command
git -C practice commit -m "Add opening hours"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think a commit sends their work to the server. A commit is saved only in your own copy. Nobody else sees it until you push it to the remote.

Sharing means pushing to the remote

A clone remembers where it came from. That copy on the server is the remote, and Git calls it origin.

git push sends your commits there, and git pull fetches other people's. The Run shows the remote of look. Your practice folder has none, so it cannot share yet.

Show where look came from

Command
git -C look remote -v

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Going further: what changed?

Change the closing time with sed -i 's/5/6/' practice/hours.txt. Run git -C practice --no-pager diff and read the lines that start with a minus and a plus.

Then save it with git -C practice commit -am "Close an hour later". The -a stages every changed file first. List the commits again.

About this lab and its source: Git and Gitea lab: learn clone, commit and push on a Git server of your own

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.