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:
- Press Sign In, top right, and sign in as
studentwith the passwordstudent. - Press + at the top right, then New Repository.
- Name it
my-first-repo. Leave Initialize Repository unticked, so it starts empty. - 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.
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.
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.
mkdir -p ~/my-first-repoShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
cd ~/my-first-repoShown 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.
git initShown 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.
echo "Hello, Git" > hello.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
cat hello.txtShown 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.
git statusShown 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".
git add hello.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
git statusShown 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.
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.
git --no-pager log --onelineShown 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.
git remote add origin http://student:student@gitea:3000/student/my-first-repo.gitShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
git remote -vShown 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.
git branch -M mainShown 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.
git push -u origin mainShown 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.