Two people, one price list
Two staff at a small shop both edit the price list on the same afternoon. One raises a price. The other fixes a spelling mistake on the same line.
If they share one folder, one of them loses work. Version control keeps both versions and shows exactly where they disagree.
By the end of this lesson
Note
I can work on a branch and merge it back.
Key words
| Word | What it means |
|---|---|
| Remote | a shared copy of a repository on a server |
| Clone | to copy a repository, with all of its history |
| Branch | a separate line of work with its own commits |
| Merge | to bring the work of one branch into another |
| Conflict | a merge where two branches changed the same lines |
The cells students fill in are left blank in this preview.
A clone copies the whole history
A remote is the shared copy of a repository, kept on a server such as Gitea. To work on it, you clone it. You get every file and every past commit.
The lab's Gitea server holds a repository called hello-git. The first Run clones it into a practice folder named review. The second reads its history, newest first.
Clone the repository into review
git clone http://gitea:3000/student/hello-git.git reviewShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Read the history, one line each
git -C review --no-pager log --format="%h|%an|%s"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A branch keeps your work apart
A branch is a line of commits of its own. You change files on it, and the main branch stays as it was.
Every commit records who made it. Tell Git your name and address for this folder first. Then make a branch called late-opening and change a line on it.
This folder is for practice. You will not push it.
Set your name for this folder
git -C review config user.name "Student"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Set your address for this folder
git -C review 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.
Make a branch and move onto it
git -C review switch -c late-openingShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Change a line on the branch
sed -i 's/Ada Lovelace/Ada Lovelace (host)/' review/visitors.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Commit the change on the branch
git -C review commit -am "Mark Ada as the host"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A merge brings two lines of work together
When two branches change different lines, Git merges them with no help. When both change the same line, Git cannot choose, so it stops and asks you. That is a conflict.
You will make one on purpose. Go back to the main branch. Change the same line a different way. Commit. Then merge the other branch in.
Move back to the main branch
git -C review switch mainShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Change the same line another way
sed -i 's/Ada Lovelace/Ada L. Lovelace/' review/visitors.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Commit the change on main
git -C review commit -am "Use the full initials"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Merge the other branch in
git -C review merge late-openingShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Read what Git wrote in the file
cat review/visitors.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Three marker lines show the two versions
Git wrote both versions into the file. The line <<<<<<< HEAD starts the version from the branch you are on. The line ======= ends it. The line >>>>>>> late-opening ends the other version.
To resolve it, you edit the file to the text you want and delete the three marker lines. Then you git add the file and git commit.
Here you stop instead, and undo the merge.
Undo the merge
git -C review merge --abortShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Draw both branches as a graph
git -C review --no-pager log --oneline --graph --allShown 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 conflict means the repository is broken, or that someone made a mistake. It means two people changed the same lines. Nothing is lost. Choose the right text, or run merge --abort to go back to before the merge.
Going further
Run the merge again in review, and this time resolve it. Make the line read Ada L. Lovelace (host), delete the three marker lines, then git add the file. Commit with --no-edit, which keeps Git's own message instead of opening an editor.
Draw the graph again and say how the merge commit differs from the others. Do not push: review stays practice.