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

Free lesson

Git Basics 2

The whole lesson is here to read. Create a free account to run it in a live lab.

Container lab11 min read14 commands

What you will do

A branch is a separate line of work in the same repository. You make a change on your own branch, so the main one stays as it was, and then you merge your branch back in when you are happy with it. Teams do this for every change. You will do it once, with the repository hello-git that this lab's Gitea already holds.

Before you start

Note

Press Start the lab on the bar at the top of this lesson and wait until it says it is running. Gitea opens from the Gitea link on the bar. Each Run button below types its command into your lab's terminal, one after another in the same session. To watch them, or to type your own, press Open terminal on the bar.

1. Tell Git who you are, and get the repository

Git needs your name for every commit. Then git clone copies hello-git from Gitea into a new folder of the same name in your home folder, ~, and cd moves into it. It has two files and two commits already. The ~/ matters if you did Git Basics first: without it the copy would land inside that lesson's folder.

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

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

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

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

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

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

Command
cd ~/hello-git

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

2. Start your own branch

git switch -c my-change makes a new branch called my-change and moves you onto it. From now on, commits go on this branch and main does not move.

Command
git switch -c my-change

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

3. Make a change and commit it

Add your line to the visitors list, then stage and commit it, the same three steps as Git Basics. git log --oneline shows your commit on top of the two that were there.

Command
echo "Your Name" >> visitors.txt

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

Command
git add visitors.txt

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

Command
git commit -m "Add my name to the visitors list"

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

Command
git --no-pager log --oneline

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

4. Push the branch

The branch only exists on your computer until you push it. -u origin my-change sends it to Gitea and remembers where it goes. git branch -a now lists the branches on both sides. Gitea also prints a link for making a pull request: it starts http://localhost, which your browser cannot reach, so use the Gitea link on the bar instead, in the next step.

Command
git push -u origin my-change

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

Command
git --no-pager branch -a

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

5. Merge it in Gitea

Press the Gitea link on the lab bar and sign in as student with the password student. Open student/hello-git and press Pull Requests, New Pull Request. Choose my-change to be merged into main, press Create Pull Request, then Merge. A pull request is how a team asks to merge a branch, and where somebody else can read it first.

6. Bring the merge back

Your computer does not know about the merge yet. Switch back to main and run git pull to fetch it. git log --oneline now shows your commit and the merge.

Command
git switch main

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

Command
git pull

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

Command
git --no-pager log --oneline

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

For teachers

Tip

This lesson is a small example of a lesson worked in a lab: the steps and the Run buttons are the lesson, and the lab is each student's own copy. This lesson uses the same Gitea lab as Git Basics, so each student keeps one terminal and one history across both. A lab can back as many lessons as you like. Copy it into your own module and change it, or build one from scratch with the Lab menu in the lesson builder.

More in this lab

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 these lessons for your students with marking and a lab for every student.