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

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

Container lab13 min read16 commands

What you will do

Git keeps the history of a folder: every time you commit, it records what changed, who changed it and why. A server such as Gitea, GitHub or GitLab keeps a copy of that history that other people can see and add to.

In this lesson you make a repository on your own Gitea server, make a folder on your computer with one file in it, commit it, and push it up to the server.

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. 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. Make a repository in Gitea

On the lab bar, press Gitea to open it in a new tab, then:

  1. Press Sign In, top right, and sign in as student with the password student.
  2. Press + at the top right, then New Repository.
  3. Name it my-first-repo. Leave Initialize Repository unticked, so it starts empty.
  4. Press Create Repository.

Gitea shows an empty repository and the commands to fill it. That is what the rest of this lesson does, one step at a time.

2. Tell Git who you are

Every commit records an author. Git asks for a name and an email address once, and --global keeps them for every repository on this computer. Run both.

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.

3. Make a folder for the project

mkdir makes a folder called my-first-repo in your home folder, the same name as the repository in Gitea, and cd moves into it. Every command after this one runs inside it.

Command
mkdir -p ~/my-first-repo

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

Command
cd ~/my-first-repo

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

4. Turn it into a repository

git init makes this folder a Git repository by adding a hidden .git folder, where the history will be kept. Nothing is recorded yet. Git may print a few lines of hint: about the name master: that is normal, and step 10 deals with it.

Command
git init

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

5. Make a file

echo writes one line, and > puts it in a new file called hello.txt. cat shows what the file holds.

Command
echo "Hello, Git" > hello.txt

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

Command
cat hello.txt

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

6. Ask Git what it sees

git status lists what has changed. hello.txt is untracked: Git can see it, but nobody has asked Git to keep it.

Command
git status

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

7. Stage the file

git add puts a file in the staging area, the list of changes that go into the next commit. Run git status again and hello.txt is now under "Changes to be committed".

Command
git add hello.txt

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

Command
git status

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

8. Commit it

A commit records everything staged as one step in the history, with a message saying what it is. git log --oneline lists the commits so far: one, with a short code that names it and your message. --no-pager prints the list straight out, where plain git log would open it in a viewer you leave by pressing q.

Command
git commit -m "Add hello.txt"

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.

9. Connect it to Gitea

A remote is another copy of the repository, here the one you made in step 1. origin is the name everybody gives the main one. git remote -v shows where it points.

Command
git remote add origin http://student:student@gitea:3000/student/my-first-repo.git

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

Command
git remote -v

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

A password in an address

Warning

The address above holds the Gitea account's name and password, so Git never stops to ask. This is safe only on a practice server that you alone can reach. Never put a real password in an address: Git asks for one when it needs it, and a real server such as GitHub wants a token instead.

10. Name the branch main

Commits are made on a branch. Older versions of Git call the first one master; most servers, Gitea and GitHub among them, now expect main. git branch -M main renames it to main whatever it was called, so it matches.

Command
git branch -M main

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

11. Push it

git push sends your commits to the remote. -u origin main also remembers that this branch goes to origin, so next time a plain git push is enough.

Command
git push -u origin main

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

12. See it in Gitea

Go back to the Gitea tab and open student/my-first-repo (or refresh it). hello.txt is there, with your commit message beside it. Press the commit count to see the history.

Try it again on your own

Tip

Press Open terminal on the lab bar, add a second line with echo "Second line" >> hello.txt (two >, so it adds rather than replaces), then git add, git commit and git push, and watch the history grow in Gitea.

If the terminal was restarted, run the cd in step 3 again first to get back into the folder.

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.