TExcelTAD
Home › Git › Git Mini Project

Git Mini Project

In this project you take a small website from an empty folder to two published releases, working the way real teams do: a shared repository on a server, feature branches, pull requests merged by a teammate, a rebase to tidy history, a tagged release with a generated changelog, and a hotfix.

The cast:

Who Where Role
Ada (you) ~/recipe-site builds the pancake recipe feature
Grace ~/grace builds the theme, reviews and merges pull requests
"GitHub" ~/github/recipe-site.git a bare repository both of them push to

Every step's script replays the previous steps first (marked with a comment), because each example starts in an empty home folder. The new part of each step comes after the # --- step N line.

Step 1: Create the project and publish it

Start with a sensible skeleton: a .gitignore before anything else, a README and the first page. Tag the skeleton as v0.1.0 and push it with --follow-tags, which pushes annotated tags that point at the pushed commits.

bash · try it yourself
# --- step 1 ---
git init -q --bare ~/github/recipe-site.git
mkdir ~/recipe-site && cd ~/recipe-site && git init -q
printf 'dist/\n*.log\n.env\n' > .gitignore
printf '# Recipe Site\n\nA tiny static site with family recipes.\n' > README.md
mkdir src
printf '<!DOCTYPE html>\n<title>Recipes</title>\n<h1>Family Recipes</h1>\n' > src/index.html
git add . && git commit -q -m "Create recipe site skeleton"
git tag -a v0.1.0 -m "Skeleton"
git remote add origin ~/github/recipe-site.git
git push -q -u origin main --follow-tags
git ls-files
git log --oneline --decorate
git ls-remote origin
.gitignore
README.md
src/index.html
4c71399 (HEAD -> main, tag: v0.1.0, origin/main) Create recipe site skeleton
4c71399058420ff9f0d665ea040b09cba95053e3	HEAD
4c71399058420ff9f0d665ea040b09cba95053e3	refs/heads/main
e5a503ba36f94b7a8ce8ca5e8fe3f6bd86aef045	refs/tags/v0.1.0
4c71399058420ff9f0d665ea040b09cba95053e3	refs/tags/v0.1.0^{}

The server now has main and the tag (the ^{} line is the commit the annotated tag points to).

Step 2: Two features in parallel

Grace clones the repository, builds a stylesheet on style/basic-theme, opens pull request #1 and, after review, merges it with a merge commit (what GitHub's Merge pull request button does). Meanwhile Ada works on feature/pancakes with three commits, one of which only fixes a typo in the previous one.

bash · try it yourself
# --- step 1 (replayed) ---
git init -q --bare ~/github/recipe-site.git
mkdir ~/recipe-site && cd ~/recipe-site && git init -q
printf 'dist/\n*.log\n.env\n' > .gitignore
printf '# Recipe Site\n\nA tiny static site with family recipes.\n' > README.md
mkdir src
printf '<!DOCTYPE html>\n<title>Recipes</title>\n<h1>Family Recipes</h1>\n' > src/index.html
git add . && git commit -q -m "Create recipe site skeleton"
git tag -a v0.1.0 -m "Skeleton"
git remote add origin ~/github/recipe-site.git
git push -q -u origin main --follow-tags
# --- step 2 ---
git clone -q ~/github/recipe-site.git ~/grace && cd ~/grace
git config user.name "Grace Hopper" && git config user.email grace@example.com
git switch -q -c style/basic-theme
printf 'body { font-family: Georgia, serif; margin: 2rem auto; max-width: 40rem; }\n' > src/style.css
sed -i 's#<title>Recipes</title>#&\n<link rel="stylesheet" href="style.css">#' src/index.html
git add . && git commit -q -m "Add basic theme"
git push -q -u origin style/basic-theme
git switch -q main && git merge -q --no-ff style/basic-theme -m "Merge pull request #1 from grace/style/basic-theme"
git push -q && git push -q origin --delete style/basic-theme
cd ~/recipe-site
git switch -q -c feature/pancakes
mkdir src/recipes
printf '<h2>Pancakes</h2>\n<ul><li>2 eggs</li><li>1 cup flour</li><li>1 cup milk</li></ul>\n' > src/recipes/pancakes.html
git add . && git commit -q -m "Add pancake recipe"
sed -i 's#<h1>Family Recipes</h1>#&\n<a href="recipes/pankaces.html">Pancakes</a>#' src/index.html
git commit -q -am "Link pancake recipe from home page"
sed -i 's/pankaces/pancakes/' src/index.html && git commit -q -am "Fix link typo"
git fetch -q
git log --oneline --graph --all
* 81d0af7 Fix link typo
* f2de39a Link pancake recipe from home page
* 580d3c1 Add pancake recipe
| * 3fb2db3 Merge pull request #1 from grace/style/basic-theme
|/|
| * daa4c99 Add basic theme
|/
* 4c71399 Create recipe site skeleton

After fetching, Ada sees that origin/main moved on (the merge of PR #1) while her branch still starts from the skeleton commit.

Step 3: Rebase, tidy up and merge

Before opening her pull request, Ada:

  1. rebases feature/pancakes onto the new origin/main, so the PR contains only her work and applies cleanly;
  2. folds "Fix link typo" into the commit it fixes with an interactive rebase (fixup). In a terminal you'd run git rebase -i origin/main and edit the plan; here GIT_SEQUENCE_EDITOR makes the edit with sed;
  3. pushes the branch. Grace merges PR #2 and deletes the branch on the server;
  4. updates her main with git pull --prune and deletes her local branch.
bash · try it yourself
# --- steps 1-2 (replayed) ---
git init -q --bare ~/github/recipe-site.git
mkdir ~/recipe-site && cd ~/recipe-site && git init -q
printf 'dist/\n*.log\n.env\n' > .gitignore
printf '# Recipe Site\n\nA tiny static site with family recipes.\n' > README.md
mkdir src
printf '<!DOCTYPE html>\n<title>Recipes</title>\n<h1>Family Recipes</h1>\n' > src/index.html
git add . && git commit -q -m "Create recipe site skeleton"
git tag -a v0.1.0 -m "Skeleton"
git remote add origin ~/github/recipe-site.git
git push -q -u origin main --follow-tags
git clone -q ~/github/recipe-site.git ~/grace && cd ~/grace
git config user.name "Grace Hopper" && git config user.email grace@example.com
git switch -q -c style/basic-theme
printf 'body { font-family: Georgia, serif; margin: 2rem auto; max-width: 40rem; }\n' > src/style.css
sed -i 's#<title>Recipes</title>#&\n<link rel="stylesheet" href="style.css">#' src/index.html
git add . && git commit -q -m "Add basic theme"
git push -q -u origin style/basic-theme
git switch -q main && git merge -q --no-ff style/basic-theme -m "Merge pull request #1 from grace/style/basic-theme"
git push -q && git push -q origin --delete style/basic-theme
cd ~/recipe-site
git switch -q -c feature/pancakes
mkdir src/recipes
printf '<h2>Pancakes</h2>\n<ul><li>2 eggs</li><li>1 cup flour</li><li>1 cup milk</li></ul>\n' > src/recipes/pancakes.html
git add . && git commit -q -m "Add pancake recipe"
sed -i 's#<h1>Family Recipes</h1>#&\n<a href="recipes/pankaces.html">Pancakes</a>#' src/index.html
git commit -q -am "Link pancake recipe from home page"
sed -i 's/pankaces/pancakes/' src/index.html && git commit -q -am "Fix link typo"
# --- step 3 ---
git fetch -q
git rebase -q origin/main
GIT_SEQUENCE_EDITOR="sed -i 's/^pick \(.*\) Fix link typo/fixup \1 Fix link typo/'" git rebase -q -i origin/main
git push -q -u origin feature/pancakes
cd ~/grace && git fetch -q
git merge -q --no-ff origin/feature/pancakes -m "Merge pull request #2 from ada/feature/pancakes"
git push -q && git push -q origin --delete feature/pancakes
cd ~/recipe-site && git switch -q main && git pull -q --prune && git branch -q -d feature/pancakes
git log --oneline --graph
echo "--- src/index.html:"
cat src/index.html
*   e8de375 Merge pull request #2 from ada/feature/pancakes
|\
| * 8adccc2 Link pancake recipe from home page
| * 3d4a480 Add pancake recipe
|/
*   3fb2db3 Merge pull request #1 from grace/style/basic-theme
|\
| * daa4c99 Add basic theme
|/
* 4c71399 Create recipe site skeleton
--- src/index.html:
<!DOCTYPE html>
<title>Recipes</title>
<link rel="stylesheet" href="style.css">
<h1>Family Recipes</h1>
<a href="recipes/pancakes.html">Pancakes</a>

The history tells the story clearly: two pull requests, each a small group of commits merged into main, and the home page contains both Grace's stylesheet link and Ada's (correctly spelled) recipe link.

Step 4: Release 1.0.0

Tag the release, push the tag, and generate release notes from the commit messages since the previous tag. Merge commits are skipped with --no-merges. Then build the downloadable archive straight from the tag with git archive:

bash · try it yourself
# --- steps 1-3 (replayed) ---
git init -q --bare ~/github/recipe-site.git
mkdir ~/recipe-site && cd ~/recipe-site && git init -q
printf 'dist/\n*.log\n.env\n' > .gitignore
printf '# Recipe Site\n\nA tiny static site with family recipes.\n' > README.md
mkdir src
printf '<!DOCTYPE html>\n<title>Recipes</title>\n<h1>Family Recipes</h1>\n' > src/index.html
git add . && git commit -q -m "Create recipe site skeleton"
git tag -a v0.1.0 -m "Skeleton"
git remote add origin ~/github/recipe-site.git
git push -q -u origin main --follow-tags
git clone -q ~/github/recipe-site.git ~/grace && cd ~/grace
git config user.name "Grace Hopper" && git config user.email grace@example.com
git switch -q -c style/basic-theme
printf 'body { font-family: Georgia, serif; margin: 2rem auto; max-width: 40rem; }\n' > src/style.css
sed -i 's#<title>Recipes</title>#&\n<link rel="stylesheet" href="style.css">#' src/index.html
git add . && git commit -q -m "Add basic theme"
git push -q -u origin style/basic-theme
git switch -q main && git merge -q --no-ff style/basic-theme -m "Merge pull request #1 from grace/style/basic-theme"
git push -q && git push -q origin --delete style/basic-theme
cd ~/recipe-site
git switch -q -c feature/pancakes
mkdir src/recipes
printf '<h2>Pancakes</h2>\n<ul><li>2 eggs</li><li>1 cup flour</li><li>1 cup milk</li></ul>\n' > src/recipes/pancakes.html
git add . && git commit -q -m "Add pancake recipe"
sed -i 's#<h1>Family Recipes</h1>#&\n<a href="recipes/pankaces.html">Pancakes</a>#' src/index.html
git commit -q -am "Link pancake recipe from home page"
sed -i 's/pankaces/pancakes/' src/index.html && git commit -q -am "Fix link typo"
git fetch -q
git rebase -q origin/main
GIT_SEQUENCE_EDITOR="sed -i 's/^pick \(.*\) Fix link typo/fixup \1 Fix link typo/'" git rebase -q -i origin/main
git push -q -u origin feature/pancakes
cd ~/grace && git fetch -q
git merge -q --no-ff origin/feature/pancakes -m "Merge pull request #2 from ada/feature/pancakes"
git push -q && git push -q origin --delete feature/pancakes
cd ~/recipe-site && git switch -q main && git pull -q --prune && git branch -q -d feature/pancakes
# --- step 4 ---
git tag -a v1.0.0 -m "Recipe site 1.0.0"
git push -q origin v1.0.0
echo "## v1.0.0"
git log --no-merges --topo-order --reverse --format='- %s (%an)' v0.1.0..v1.0.0
echo "--- release archive:"
git archive --format=tar --prefix=recipe-site-1.0.0/ v1.0.0 | tar -t
## v1.0.0
- Add basic theme (Grace Hopper)
- Add pancake recipe (Ada Lovelace)
- Link pancake recipe from home page (Ada Lovelace)
--- release archive:
recipe-site-1.0.0/
recipe-site-1.0.0/.gitignore
recipe-site-1.0.0/README.md
recipe-site-1.0.0/src/
recipe-site-1.0.0/src/index.html
recipe-site-1.0.0/src/recipes/
recipe-site-1.0.0/src/recipes/pancakes.html
recipe-site-1.0.0/src/style.css

Good commit messages pay off here: the changelog wrote itself.

Step 5: A hotfix release

A reader reports that the pancakes are too dry. The fix starts from the released tag (so it contains nothing unreleased), is merged into main, and is published as v1.0.1:

bash · try it yourself
# --- steps 1-4 (replayed) ---
git init -q --bare ~/github/recipe-site.git
mkdir ~/recipe-site && cd ~/recipe-site && git init -q
printf 'dist/\n*.log\n.env\n' > .gitignore
printf '# Recipe Site\n\nA tiny static site with family recipes.\n' > README.md
mkdir src
printf '<!DOCTYPE html>\n<title>Recipes</title>\n<h1>Family Recipes</h1>\n' > src/index.html
git add . && git commit -q -m "Create recipe site skeleton"
git tag -a v0.1.0 -m "Skeleton"
git remote add origin ~/github/recipe-site.git
git push -q -u origin main --follow-tags
git clone -q ~/github/recipe-site.git ~/grace && cd ~/grace
git config user.name "Grace Hopper" && git config user.email grace@example.com
git switch -q -c style/basic-theme
printf 'body { font-family: Georgia, serif; margin: 2rem auto; max-width: 40rem; }\n' > src/style.css
sed -i 's#<title>Recipes</title>#&\n<link rel="stylesheet" href="style.css">#' src/index.html
git add . && git commit -q -m "Add basic theme"
git push -q -u origin style/basic-theme
git switch -q main && git merge -q --no-ff style/basic-theme -m "Merge pull request #1 from grace/style/basic-theme"
git push -q && git push -q origin --delete style/basic-theme
cd ~/recipe-site
git switch -q -c feature/pancakes
mkdir src/recipes
printf '<h2>Pancakes</h2>\n<ul><li>2 eggs</li><li>1 cup flour</li><li>1 cup milk</li></ul>\n' > src/recipes/pancakes.html
git add . && git commit -q -m "Add pancake recipe"
sed -i 's#<h1>Family Recipes</h1>#&\n<a href="recipes/pankaces.html">Pancakes</a>#' src/index.html
git commit -q -am "Link pancake recipe from home page"
sed -i 's/pankaces/pancakes/' src/index.html && git commit -q -am "Fix link typo"
git fetch -q
git rebase -q origin/main
GIT_SEQUENCE_EDITOR="sed -i 's/^pick \(.*\) Fix link typo/fixup \1 Fix link typo/'" git rebase -q -i origin/main
git push -q -u origin feature/pancakes
cd ~/grace && git fetch -q
git merge -q --no-ff origin/feature/pancakes -m "Merge pull request #2 from ada/feature/pancakes"
git push -q && git push -q origin --delete feature/pancakes
cd ~/recipe-site && git switch -q main && git pull -q --prune && git branch -q -d feature/pancakes
git tag -a v1.0.0 -m "Recipe site 1.0.0"
git push -q origin v1.0.0
# --- step 5 ---
git switch -q -c hotfix/milk v1.0.0
sed -i 's#1 cup milk#1 1/4 cups milk#' src/recipes/pancakes.html
git commit -q -am "Fix milk amount in pancake recipe"
git switch -q main && git merge -q --no-ff hotfix/milk -m "Merge hotfix/milk"
git branch -q -d hotfix/milk
git tag -a v1.0.1 -m "Recipe site 1.0.1"
git push -q --follow-tags
git log --oneline --graph --decorate
echo "--- version: $(git describe)"
echo "--- contributors:"
git shortlog -sn --no-merges HEAD
echo "--- tags on the server:"
git ls-remote --tags --refs origin | cut -f2
*   43bbcd9 (HEAD -> main, tag: v1.0.1, origin/main, origin/HEAD) Merge hotfix/milk
|\
| * 7141b60 Fix milk amount in pancake recipe
|/
*   e8de375 (tag: v1.0.0) Merge pull request #2 from ada/feature/pancakes
|\
| * 8adccc2 Link pancake recipe from home page
| * 3d4a480 Add pancake recipe
|/
*   3fb2db3 Merge pull request #1 from grace/style/basic-theme
|\
| * daa4c99 Add basic theme
|/
* 4c71399 (tag: v0.1.0) Create recipe site skeleton
--- version: v1.0.1
--- contributors:
     4	Ada Lovelace
     1	Grace Hopper
--- tags on the server:
refs/tags/v0.1.0
refs/tags/v1.0.0
refs/tags/v1.0.1

git describe confirms that main is exactly the v1.0.1 release, and the server has all three tags.

What you practised

Skill Commands
Project setup init, .gitignore, add, commit, remote add, push -u --follow-tags
Collaboration clone, switch -c, push -u, fetch, pull --prune
Pull requests merge --no-ff, push --delete, branch -d
Clean history rebase, rebase -i with fixup
Releases tag -a, log --no-merges --format, archive, describe
Hotfixes switch -c hotfix v1.0.0, merge, new tag

Ideas to extend the project

  • Add a pre-commit hook that rejects HTML files without a <title> (Hooks).
  • Make Grace and Ada edit the same line and resolve the conflict during the rebase (Merge Conflicts).
  • Publish the src/ folder on an orphan gh-pages branch (GitHub Pages).
  • Use git bisect run to find which commit broke a link checker script (Bisect).

Test yourself

Why did Ada rebase her branch onto origin/main before opening the pull request?

  1. To delete Grace's commits
  2. So her branch starts from the latest main and the PR applies cleanly with a linear branch history
  3. Because pull requests require rebased branches
  4. To create a tag

Why does the hotfix branch start from the tag v1.0.0 instead of from main?

  1. Tags are faster
  2. So the hotfix release contains only the fix, not unreleased work that may be on main
  3. Git requires it
  4. To avoid a merge

Which option made git push also send the annotated release tag?

  1. --tags-only
  2. --follow-tags
  3. --all
  4. -u