Two people, one line
Ana and Ben work on the same file. Both add a name at the end of the same list.
Ana finishes first. Then Ben tries to join his work to hers. Whose line should win?
Git does not guess. It stops and asks a person. In this lesson, you are that person.
Key words
| Word | What it means | Example |
|---|---|---|
| Conflict | Two changes to the same place that Git cannot join by itself. | Two names on the same line |
| Merge | Bringing one branch's changes into another. | git merge ben |
| Branch | A separate line of work. | ben |
| Commit | One saved step in the history. | Add Ben |
| Merge commit | A commit that joins two lines of work. | Merge branch ben |
| Conflict markers | Lines Git writes in the file to show both versions. | <<<<<<< and >>>>>>> |
| Resolve | Choosing what the file should say, then saving it. | Keep both names |
| Repository | A folder of files and their whole history. | hello-git |
| Stage | Choosing which changes go into the next commit. | git add visitors.txt |
| Clone | Making your own copy of a repository. | git clone ... team |
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. You work in a new folder, so this lesson does not need the earlier one.
Two changes in one place make a conflict
flowchart LR
start[Shared list: Ada] --> ana[Ana adds a line: Ana]
start --> ben[Ben adds a line: Ben]
ana --> join{Merge}
ben --> join
join --> conflict[Conflict: both changed the same place]
conflict --> person[A person decides: keep both]
1. Clone a fresh copy
First tell Git your name and email. Then clone the project (make your own copy) into a new folder called team. Then go into the folder.
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.
git clone http://student:student@gitea:3000/student/hello-git.git teamA command for the lesson’s lab terminal. It is shown here and not run.
cd teamA command for the lesson’s lab terminal. It is shown here and not run.
2. Ben adds a line on a branch
You play both people. First you are Ben. Make a branch, add a line, and commit. The -a in commit -am means: stage all changes to files that Git already knows. The -m gives the message.
git switch -c benA command for the lesson’s lab terminal. It is shown here and not run.
echo "Ben" >> visitors.txtA command for the lesson’s lab terminal. It is shown here and not run.
git commit -am "Add Ben to the visitors list"A command for the lesson’s lab terminal. It is shown here and not run.
3. Ana adds a line on main
Now you are Ana. Go back to main and add a different line in the same place. Commit it.
git switch mainA command for the lesson’s lab terminal. It is shown here and not run.
echo "Ana" >> visitors.txtA command for the lesson’s lab terminal. It is shown here and not run.
git commit -am "Add Ana to the visitors list"A command for the lesson’s lab terminal. It is shown here and not run.
4. Merge, and Git reports a conflict
Merge Ben's branch into main. Git says CONFLICT. This is normal. Then look at the file. Git has written both versions between markers. If you get lost, the command git merge --abort goes back to before the merge.
git merge benA 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.
Check: read the conflict markers
Have a go
Check: read the conflict markers
3 questions5. Keep both names to resolve it
Keep both names and remove the markers. The printf command writes the whole file again, the way you want it. Then stage it and commit. The option --no-edit keeps the message that Git already wrote.
printf "People who have pushed to this repository:\n\nAda Lovelace\nAna\nBen\n" > 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 --no-editA command for the lesson’s lab terminal. It is shown here and not run.
git --no-pager log --oneline --graphA command for the lesson’s lab terminal. It is shown here and not run.
The graph shows the split and join
Tip
The graph shows two lines that split and join again at the merge commit. It shows the history of the work.
Match each word to its meaning
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