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

Free lesson

Work on a branch and merge it (Git)

After this lesson: I can work on a branch and merge it back.

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

Two people, one price list

Two staff at a small shop both edit the price list on the same afternoon. One raises a price. The other fixes a spelling mistake on the same line.

If they share one folder, one of them loses work. Version control keeps both versions and shows exactly where they disagree.

By the end of this lesson

Note

I can work on a branch and merge it back.

Key words

WordWhat it means
Remotea shared copy of a repository on a server
Cloneto copy a repository, with all of its history
Brancha separate line of work with its own commits
Mergeto bring the work of one branch into another
Conflicta merge where two branches changed the same lines

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

A clone copies the whole history

A remote is the shared copy of a repository, kept on a server such as Gitea. To work on it, you clone it. You get every file and every past commit.

The lab's Gitea server holds a repository called hello-git. The first Run clones it into a practice folder named review. The second reads its history, newest first.

Clone the repository into review

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

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

Read the history, one line each

Command
git -C review --no-pager log --format="%h|%an|%s"

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

A branch keeps your work apart

A branch is a line of commits of its own. You change files on it, and the main branch stays as it was.

Every commit records who made it. Tell Git your name and address for this folder first. Then make a branch called late-opening and change a line on it.

This folder is for practice. You will not push it.

Set your name for this folder

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

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

Set your address for this folder

Command
git -C review 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.

Make a branch and move onto it

Command
git -C review switch -c late-opening

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

Change a line on the branch

Command
sed -i 's/Ada Lovelace/Ada Lovelace (host)/' review/visitors.txt

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

Commit the change on the branch

Command
git -C review commit -am "Mark Ada as the host"

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

A merge brings two lines of work together

When two branches change different lines, Git merges them with no help. When both change the same line, Git cannot choose, so it stops and asks you. That is a conflict.

You will make one on purpose. Go back to the main branch. Change the same line a different way. Commit. Then merge the other branch in.

Move back to the main branch

Command
git -C review switch main

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

Change the same line another way

Command
sed -i 's/Ada Lovelace/Ada L. Lovelace/' review/visitors.txt

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

Commit the change on main

Command
git -C review commit -am "Use the full initials"

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

Merge the other branch in

Command
git -C review merge late-opening

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

Read what Git wrote in the file

Command
cat review/visitors.txt

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

Three marker lines show the two versions

Git wrote both versions into the file. The line <<<<<<< HEAD starts the version from the branch you are on. The line ======= ends it. The line >>>>>>> late-opening ends the other version.

To resolve it, you edit the file to the text you want and delete the three marker lines. Then you git add the file and git commit.

Here you stop instead, and undo the merge.

Undo the merge

Command
git -C review merge --abort

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

Draw both branches as a graph

Command
git -C review --no-pager log --oneline --graph --all

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 conflict means the repository is broken, or that someone made a mistake. It means two people changed the same lines. Nothing is lost. Choose the right text, or run merge --abort to go back to before the merge.

Going further

Run the merge again in review, and this time resolve it. Make the line read Ada L. Lovelace (host), delete the three marker lines, then git add the file. Commit with --no-edit, which keeps Git's own message instead of opening an editor.

Draw the graph again and say how the merge commit differs from the others. Do not push: review stays practice.

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.