Too many files called final
Three friends build a website for a local café. They email files to each other. Soon there are files called menu_final, menu_final2 and menu_final_v3. Nobody knows which one is newest.
Then somebody saves over a friend's work and it is lost. Git keeps every version, with a note of who changed what and why.
By the end of this lesson
Note
I can keep and share versions of my work.
Key words
| Word | What it means | Example |
|---|---|---|
| Repository | a folder whose whole history Git keeps | |
| Commit | a saved version, with a note of who and why | |
| Clone | a full copy of a repository from a server | |
| Stage | choose which changes go into the next commit | git add hours.txt |
| Remote | the copy of a repository on a server | |
| Push | send your commits to the remote |
The cells students fill in are left blank in this preview.
Before you start
Note
Press Start the lab on the bar at the top and wait until it says it is ready. Your lab has its own Git server, called gitea, holding one repository. Nothing you do reaches anybody else.
A repository keeps every version
A repository is a folder plus its whole history. Each saved version is a commit. A commit has a short id, an author, a date and a message.
The first Run makes a clone, your own full copy, in a folder called look. The second lists its commits, newest first.
Clone the repository into look
git clone http://gitea:3000/student/hello-git.git lookShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
List the commits, one a line
git -C look --no-pager log --format="%h %an %ad %s" --date=shortShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Stage first, then commit
Now make a repository of your own, called practice, and save a first version in it.
Git saves in two steps. git add stages a file: it puts it in the next commit. git commit then saves everything staged as one version. Git also needs a name and an email address to write on each commit.
Make a new repository
git init -b main practiceShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Give Git your name
git -C practice config user.name "Student"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Give Git an email address
git -C practice 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.
Write a file
printf 'Open 9 to 5\n' > practice/hours.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Ask Git what it sees
git -C practice status --shortShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Stage the file
git -C practice add hours.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Save the first version
git -C practice commit -m "Add opening hours"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 commit sends their work to the server. A commit is saved only in your own copy. Nobody else sees it until you push it to the remote.
Sharing means pushing to the remote
A clone remembers where it came from. That copy on the server is the remote, and Git calls it origin.
git push sends your commits there, and git pull fetches other people's. The Run shows the remote of look. Your practice folder has none, so it cannot share yet.
Show where look came from
git -C look remote -vShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Going further: what changed?
Change the closing time with sed -i 's/5/6/' practice/hours.txt. Run git -C practice --no-pager diff and read the lines that start with a minus and a plus.
Then save it with git -C practice commit -am "Close an hour later". The -a stages every changed file first. List the commits again.