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 (3 of 4): Git: when two people change one line

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

Two people, one line

Ana and Ben work on the same file. Both add a name at the end of the same list.

Ana finishes first. Then Ben tries to join his work to hers. Whose line should win?

Git does not guess. It stops and asks a person. In this lesson, you are that person.

Key words

WordWhat it meansExample
ConflictTwo changes to the same place that Git cannot join by itself.Two names on the same line
MergeBringing one branch's changes into another.git merge ben
BranchA separate line of work.ben
CommitOne saved step in the history.Add Ben
Merge commitA commit that joins two lines of work.Merge branch ben
Conflict markersLines Git writes in the file to show both versions.<<<<<<< and >>>>>>>
ResolveChoosing what the file should say, then saving it.Keep both names
RepositoryA folder of files and their whole history.hello-git
StageChoosing which changes go into the next commit.git add visitors.txt
CloneMaking your own copy of a repository.git clone ... team

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. You work in a new folder, so this lesson does not need the earlier one.

Two changes in one place make a conflict

flowchart LR
  start[Shared list: Ada] --> ana[Ana adds a line: Ana]
  start --> ben[Ben adds a line: Ben]
  ana --> join{Merge}
  ben --> join
  join --> conflict[Conflict: both changed the same place]
  conflict --> person[A person decides: keep both]

1. Clone a fresh copy

First tell Git your name and email. Then clone the project (make your own copy) into a new folder called team. Then go into the folder.

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.

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

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

Command
cd team

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

2. Ben adds a line on a branch

You play both people. First you are Ben. Make a branch, add a line, and commit. The -a in commit -am means: stage all changes to files that Git already knows. The -m gives the message.

Command
git switch -c ben

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

Command
echo "Ben" >> visitors.txt

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

Command
git commit -am "Add Ben to the visitors list"

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

3. Ana adds a line on main

Now you are Ana. Go back to main and add a different line in the same place. Commit it.

Command
git switch main

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

Command
echo "Ana" >> visitors.txt

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

Command
git commit -am "Add Ana to the visitors list"

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

4. Merge, and Git reports a conflict

Merge Ben's branch into main. Git says CONFLICT. This is normal. Then look at the file. Git has written both versions between markers. If you get lost, the command git merge --abort goes back to before the merge.

Command
git merge ben

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.

Check: read the conflict markers

Have a go

Check: read the conflict markers

3 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

5. Keep both names to resolve it

Keep both names and remove the markers. The printf command writes the whole file again, the way you want it. Then stage it and commit. The option --no-edit keeps the message that Git already wrote.

Command
printf "People who have pushed to this repository:\n\nAda Lovelace\nAna\nBen\n" > 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 --no-edit

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

Command
git --no-pager log --oneline --graph

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

The graph shows the split and join

Tip

The graph shows two lines that split and join again at the merge commit. It shows the history of the work.

Match each word to its meaning

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

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

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.