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

Free lesson

OS 8.3 (2 of 4): Git: read the history, then branch

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

45 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

Who changed this, and why?

A line in a file is wrong. Who wrote it? When? Why?

In a team, many people change the same code. Without a record, nobody knows. With a tool called Git, every change has an author, a date and a message.

Key words

WordWhat it meansExample
Version controlA tool that records every change to your files.Git
StageChoosing which changes go into the next commit.git add visitors.txt
RepositoryA folder of files and their whole history.hello-git
CommitOne saved step in the history, with a message.Add a list of visitors
BranchA separate line of work, kept apart until you merge.add-my-name
MergeBringing one branch's changes into another.Merge add-my-name into main
HistoryThe list of all commits, newest first.git log
CloneMaking your own copy of a repository.git clone
MainThe branch that holds the finished, shared code.main

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

Start the lab first

Note

Press Start the lab on the bar at the top of this lesson. Wait until it says it is running. Each Run button types its command into your lab terminal, one after another. The option --no-pager makes Git show all the text at once, which is easier to read here.

1. Say who you are

Every commit has an author. Tell Git your name and your email. Run both commands.

Command
git config --global user.name "Student"

A command for the lesson’s lab terminal. It is shown here and not run.

Command
git config --global user.email "[email protected]"

A command for the lesson’s lab terminal. It is shown here and not run.

2. Clone a copy of the project

The lab has a project called hello-git. Clone it. Then go into its folder.

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

A command for the lesson’s lab terminal. It is shown here and not run.

Command
cd hello-git

A command for the lesson’s lab terminal. It is shown here and not run.

3. The history shows who changed what

Two people worked here before you. The first command lists the commits, newest first. The second shows, for each line of a file, who wrote it. Many people think blame is about finding fault, but it is not. It helps you find the right person to ask.

Command
git --no-pager log --oneline

A command for the lesson’s lab terminal. It is shown here and not run.

Command
git --no-pager blame visitors.txt

A command for the lesson’s lab terminal. It is shown here and not run.

Check: read the history

Have a go

Check: read the history

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

4. A branch keeps your work apart

Do not change the shared code directly. Make a branch, so your work stays apart until it is ready.

Run the commands in order: make the branch, add your name to the list, stage the file, save the commit.

Command
git switch -c add-my-name

A command for the lesson’s lab terminal. It is shown here and not run.

Command
echo "Student" >> visitors.txt

A command for the lesson’s lab terminal. It is shown here and not run.

Command
git add visitors.txt

A command for the lesson’s lab terminal. It is shown here and not run.

Command
git commit -m "Add Student to the visitors list"

A command for the lesson’s lab terminal. It is shown here and not run.

Command
git --no-pager log --oneline

A command for the lesson’s lab terminal. It is shown here and not run.

5. Merging puts your work on main

Go back to main. Look at the file. Your name is not there yet. Your work is only on the branch.

Then merge. Now the change is on main, and the history shows it.

Command
git switch main

A command for the lesson’s lab terminal. It is shown here and not run.

Command
cat visitors.txt

A command for the lesson’s lab terminal. It is shown here and not run.

Command
git merge add-my-name

A command for the lesson’s lab terminal. It is shown here and not run.

Command
cat visitors.txt

A command for the lesson’s lab terminal. It is shown here and not run.

Match each idea to its command

Have a go

Loading…

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Final check

Have a go

Final check

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Read, branch, commit, then merge

Important

Read the history before you change code. Work on a branch. Write a clear commit message. Merge when it is ready.

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.