Who changed this, and why?
A line in a file is wrong. Who wrote it? When? Why?
In a team, many people change the same code. Without a record, nobody knows. With a tool called Git, every change has an author, a date and a message.
Key words
| Word | What it means | Example |
|---|---|---|
| Version control | A tool that records every change to your files. | Git |
| Stage | Choosing which changes go into the next commit. | git add visitors.txt |
| Repository | A folder of files and their whole history. | hello-git |
| Commit | One saved step in the history, with a message. | Add a list of visitors |
| Branch | A separate line of work, kept apart until you merge. | add-my-name |
| Merge | Bringing one branch's changes into another. | Merge add-my-name into main |
| History | The list of all commits, newest first. | git log |
| Clone | Making your own copy of a repository. | git clone |
| Main | The branch that holds the finished, shared code. | main |
The cells students fill in are left blank in this preview.
Start the lab first
Note
Press Start the lab on the bar at the top of this lesson. Wait until it says it is running. Each Run button types its command into your lab terminal, one after another. The option --no-pager makes Git show all the text at once, which is easier to read here.
1. Say who you are
Every commit has an author. Tell Git your name and your email. Run both commands.
git config --global user.name "Student"A command for the lesson’s lab terminal. It is shown here and not run.
git config --global user.email "[email protected]"A command for the lesson’s lab terminal. It is shown here and not run.
2. Clone a copy of the project
The lab has a project called hello-git. Clone it. Then go into its folder.
git clone http://student:student@gitea:3000/student/hello-git.gitA command for the lesson’s lab terminal. It is shown here and not run.
cd hello-gitA command for the lesson’s lab terminal. It is shown here and not run.
3. The history shows who changed what
Two people worked here before you. The first command lists the commits, newest first. The second shows, for each line of a file, who wrote it. Many people think blame is about finding fault, but it is not. It helps you find the right person to ask.
git --no-pager log --onelineA command for the lesson’s lab terminal. It is shown here and not run.
git --no-pager blame visitors.txtA command for the lesson’s lab terminal. It is shown here and not run.
Check: read the history
Have a go
Check: read the history
4 questions4. A branch keeps your work apart
Do not change the shared code directly. Make a branch, so your work stays apart until it is ready.
Run the commands in order: make the branch, add your name to the list, stage the file, save the commit.
git switch -c add-my-nameA command for the lesson’s lab terminal. It is shown here and not run.
echo "Student" >> visitors.txtA command for the lesson’s lab terminal. It is shown here and not run.
git add visitors.txtA command for the lesson’s lab terminal. It is shown here and not run.
git commit -m "Add Student to the visitors list"A command for the lesson’s lab terminal. It is shown here and not run.
git --no-pager log --onelineA command for the lesson’s lab terminal. It is shown here and not run.
5. Merging puts your work on main
Go back to main. Look at the file. Your name is not there yet. Your work is only on the branch.
Then merge. Now the change is on main, and the history shows it.
git switch mainA command for the lesson’s lab terminal. It is shown here and not run.
cat visitors.txtA command for the lesson’s lab terminal. It is shown here and not run.
git merge add-my-nameA command for the lesson’s lab terminal. It is shown here and not run.
cat visitors.txtA command for the lesson’s lab terminal. It is shown here and not run.
Match each idea to its command
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Final check
Have a go
Final check
5 questionsRead, branch, commit, then merge
Important
Read the history before you change code. Work on a branch. Write a clear commit message. Merge when it is ready.