What you can do after this lesson
You will have run through everything the course covered in one connected story. By the end you will have collaborated with a pretend teammate, resolved a conflict, merged work, tagged a release, and kept a second copy in sync. No GitHub account is needed. A bare repository on your own disk plays the part of GitHub, exactly as in the remotes lesson. This is a project, not a lecture: type the commands yourself, read each output, and compare with ours. Hashes and paths will differ; everything else should match closely.
The story
Two friends, Asha and Ben, keep a shared reading list. Asha is the maintainer: she owns the shared repository and merges changes. Ben contributes. The files will be tiny: a README.md, a .gitignore and books.txt. The people and books are fictional.
You will play both people by using two folders. Make a scratch folder to hold everything, and work inside it.
Step 1: create the shared remote and Asha's clone
git init --bare --initial-branch=main readinglist.git
git clone readinglist.git asha
cd asha
git config user.name "Asha Rao"
git config user.email "asha@example.com"Initialized empty Git repository in /home/you/fp/work/readinglist.git/
Cloning into 'asha'...
warning: You appear to have cloned an empty repository.
done.The warning is expected. The two git config lines print nothing; they give Asha a separate identity in this clone only.
Step 2: the first commit and push
Create three files:
cat > README.md <<'EOT'
Reading List
============
A tiny shared list of books we want to read.
EOT
printf '.DS_Store\n*.tmp\n' > .gitignore
printf 'Dune\nEmma\n' > books.txt
git add -A
git commit -m "Start the reading list"
git push -u origin main[main (root-commit) b5b50d4] Start the reading list
3 files changed, 8 insertions(+)
create mode 100644 .gitignore
create mode 100644 README.md
create mode 100644 books.txt
To /home/you/fp/work/readinglist.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.Step 3: Ben joins
Go back to the scratch folder and make Ben's clone with his own identity:
cd ..
git clone readinglist.git ben
cd ben
git config user.name "Ben Iyer"
git config user.email "ben@example.com"He now has the full history. Ben starts a feature branch to label every book with its genre:
git switch -c add-genres
printf 'Dune (science fiction)\nEmma (classic)\n' > books.txt
git commit -am "Label each book with a genre"
git push -u origin add-genresSwitched to a new branch 'add-genres'
[add-genres 0607f99] Label each book with a genre
1 file changed, 2 insertions(+), 2 deletions(-)
To /home/you/fp/work/readinglist.git
* [new branch] add-genres -> add-genres
branch 'add-genres' set up to track 'origin/add-genres'.Step 4: Asha works at the same time
Back in Asha's folder (cd ../asha), she works on a different branch, and unluckily edits the same file:
git switch -c add-persuasion
printf 'Dune\nEmma\nPersuasion\n' > books.txt
git commit -am "Add Persuasion to the list"
git push -u origin add-persuasionSwitched to a new branch 'add-persuasion'
[add-persuasion 423ca86] Add Persuasion to the list
1 file changed, 1 insertion(+)
To /home/you/fp/work/readinglist.git
* [new branch] add-persuasion -> add-persuasion
branch 'add-persuasion' set up to track 'origin/add-persuasion'.On GitHub, the next step would be two pull requests. Here, Asha plays the reviewer and merges them herself, as the maintainer, with the same commands that GitHub's merge button runs for you.
Step 5: merge Ben's work first
git switch main
git fetch origin
git merge --no-ff origin/add-genres -m "Merge add-genres"Switched to branch 'main'
Your branch is up to date with 'origin/main'.
From /home/you/fp/work/readinglist
* [new branch] add-genres -> origin/add-genres
Merge made by the 'ort' strategy.
books.txt | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)--no-ff creates a merge commit so history shows the feature branch, like GitHub's "merge commit" option.
Step 6: merge Asha's own branch and meet the conflict
git merge --no-ff add-persuasion -m "Merge add-persuasion"Auto-merging books.txt
CONFLICT (content): Merge conflict in books.txt
Automatic merge failed; fix conflicts and then commit the result.Both branches changed the lines of books.txt. Open it:
<<<<<<< HEAD
Dune (science fiction)
Emma (classic)
=======
Dune
Emma
Persuasion
>>>>>>> add-persuasionThe right answer keeps Ben's genres and Asha's new book. Write the final file by hand, with no markers:
Dune (science fiction)
Emma (classic)
Persuasion (classic)Stage, finish the merge, and look at the history:
git add books.txt
git commit --no-edit
git log --oneline --graph --decorate[main f1ffaf8] Merge add-persuasion
* f1ffaf8 (HEAD -> main) Merge add-persuasion
|\
| * 423ca86 (origin/add-persuasion, add-persuasion) Add Persuasion to the list
* | 91cb0bc Merge add-genres
|\ \
| |/
|/|
| * 0607f99 (origin/add-genres) Label each book with a genre
|/
* b5b50d4 (origin/main, origin/HEAD) Start the reading listStudy the graph: two branches left b5b50d4, and both are now merged back. Note that Persuasion's genre (classic) was our own decision; in a real team you would agree on it.
Step 7: publish, tag and tidy the remote
git push origin main
git tag -a v1.0.0 -m "First shared list"
git push origin v1.0.0
git push origin --delete add-genres add-persuasion
git branch -d add-persuasionTo /home/you/fp/work/readinglist.git
b5b50d4..f1ffaf8 main -> main
To /home/you/fp/work/readinglist.git
* [new tag] v1.0.0 -> v1.0.0
To /home/you/fp/work/readinglist.git
- [deleted] add-genres
- [deleted] add-persuasion
Deleted branch add-persuasion (was 423ca86).Confirm what the remote holds with git ls-remote --tags origin. The annotated tag shows two lines, one for the tag object and one with ^{} for the commit.
Step 8: Ben catches up
In Ben's folder (cd ../ben):
git fetch --prune
git switch main
git pull
git branch -d add-genres
git log --oneline --decorate -4
git branch -aFrom /home/you/fp/work/readinglist
- [deleted] (none) -> origin/add-genres
b5b50d4..f1ffaf8 main -> origin/main
* [new tag] v1.0.0 -> v1.0.0
Switched to branch 'main'
Your branch is behind 'origin/main' by 4 commits, and can be fast-forwarded.
(use "git pull" to update your local branch)
Updating b5b50d4..f1ffaf8
Fast-forward
books.txt | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
Deleted branch add-genres (was 0607f99).
f1ffaf8 (HEAD -> main, tag: v1.0.0, origin/main, origin/HEAD) Merge add-persuasion
91cb0bc Merge add-genres
423ca86 Add Persuasion to the list
b5b50d4 Start the reading list
* main
remotes/origin/HEAD -> origin/main
remotes/origin/mainBoth clones now agree. The remote has main and the tag, with no stale feature branches.
Checkpoints
Before you finish, check yourself against this list:
git statusin both clones says the working tree is clean andmainis up to date.git log --oneline --graph --alllooks the same in both clones.books.txtcontains no conflict markers.git taglistsv1.0.0in both.- You can explain which commands correspond to the buttons on GitHub's pull request page.
Extension tasks
Once the main run works, try one of these, using what you learned in the course:
- Ben adds a branch
fix-typo, and Asha cherry-picks the fix onto arelease/1.0branch created from the tag. - Make a bad commit on
mainand undo it withgit revert, then push. - Add a
commit-msghook that rejects subjects over 72 characters. - Squash a messy branch with an interactive rebase before merging it.
- Stash half-finished work in the middle of a branch and come back to it.
Common mistakes
- Forgetting the identity step in the second clone. Commits may be attributed to you or fail.
- Running commands in the wrong folder. Check the prompt or
pwd, andgit branchbefore committing. - Leaving conflict markers in the file. Search for
<<<<<<<before staging. - Forgetting to push after merging. The other clone then has nothing to fetch.
- Skipping
fetch --prune. Old branch names linger in the list.
Exercise
- Complete steps 1 to 8 from memory as far as you can, looking back only when stuck.
- Repeat the project, but this time have Ben rebase his branch onto
mainbefore Asha merges it. Does the conflict appear in a different place? - Write a short summary of the workflow in your own words as a README section called "How we work".
Show one possible answer
With rebase, the conflict appears when Ben rebases add-genres onto the updated main (if main already contains the Persuasion change), rather than at Asha's merge. He resolves it with git add and git rebase --continue, then pushes with git push --force-with-lease, because he rewrote a branch only he uses. Asha's merge is then clean.
A sample README section: "How we work: we never commit to main. Each change gets a branch, a pull request and a review. We merge with a merge commit, delete the branch afterwards and tag releases as vMAJOR.MINOR.PATCH."
Congratulations: that was the full cycle that real teams run every day. For deeper topics, the Pro Git book on git-scm.com and the manual pages (git help <command>) are the authoritative sources.
Key points
- A bare repository on your own disk can stand in for GitHub, so you can rehearse a full team workflow with no account.
- Two clones in two folders play two teammates, each with their own identity set locally.
- The cycle is branch, commit, push the branch, merge into main (the role a pull request plays), resolve conflicts, push main, tag, delete branches.
- After merging, the other clone fetches, fast-forwards main and prunes deleted branches.
- Everything here uses fictional names and values; hashes in your output will differ.
Check yourself
Final exam and certificate
Finish all 30 lessons to unlock a 15-question exam on Git and GitHub. Pass it and you get a certificate with a public link you can share.
Sign in to take the exam