Skip to content

Commit 7fe8469

Browse files
committed
feat: new stories basics.
1 parent 96c605c commit 7fe8469

File tree

3 files changed

+119
-1
lines changed

3 files changed

+119
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@
3333
"scripts": {
3434
"generate-rss": "node scripts/generate-rss.js",
3535
"generate-sitemap": "node scripts/generate-sitemap.js",
36+
"pull-stories": "git subtree pull --prefix public/stories fezcodex-stories main --squash",
3637
"generate-wallpapers": "node scripts/generateWallpapers.js",
3738
"prestart": "npm run generate-wallpapers && npm run generate-rss && npm run generate-sitemap",
3839
"start": "craco start",
39-
"prebuild": "npm run generate-wallpapers && npm run generate-rss && npm run generate-sitemap",
40+
"prebuild": "npm run pull-stories && npm run generate-wallpapers && npm run generate-rss && npm run generate-sitemap",
4041
"build": "craco build",
4142
"test": "craco test",
4243
"eject": "react-scripts eject",
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Fezcodex Stories with `git subtree`
2+
3+
Let's cover how we integrate `fezcodex.stories` repo to store and show our stories (dnd) section.
4+
5+
## Integrating External Content Seamlessly with Git Subtree: A Practical Guide
6+
7+
In modern web development, it's common to need to incorporate content or even entire sub-projects from external Git repositories into your main project. Whether it's a shared library, documentation, or, as in our case, a collection of stories or blog posts, managing this external content efficiently is key. Git offers a couple of powerful tools for this: `git submodule` and `git subtree`.
8+
9+
While `git submodule` is excellent for managing distinct project dependencies, `git subtree` often shines when you want to integrate external content directly into your repository as if it were always part of it, especially when you need to easily pull updates. Let's dive into how `git subtree` can help you manage external content like your `fezcodex.stories` within your `public/stories` directory.
10+
11+
### Why Choose Git Subtree?
12+
13+
When deciding between `git submodule` and `git subtree`, consider these advantages of `git subtree` for content integration:
14+
15+
* **Integrated History:** The content of the external repository becomes a part of your main repository's history. This means anyone cloning your main repository gets all the content directly, without needing extra steps.
16+
* **Simpler Cloning:** A regular `git clone` of your main repository will fetch all the subtree content. No special commands like `git submodule update --init --recursive` are required for collaborators.
17+
* **Easy Updates:** Keeping your integrated content up-to-date with the original source is straightforward with a single `git subtree pull` command.
18+
* **No `.gitmodules`:** `git subtree` doesn't introduce additional configuration files like `.gitmodules`, keeping your repository root cleaner.
19+
* **Works with Existing Tools:** Since the content is fully integrated, all your existing Git tools and workflows (like `git grep`, `git log`) work seamlessly across your entire project, including the subtree content.
20+
21+
### Setting Up Your Git Subtree: Step-by-Step
22+
23+
Let's walk through the process of adding the `fezcodex.stories` repository into your `public/stories` directory.
24+
25+
#### Prerequisites:
26+
27+
Before you begin, ensure your working directory is clean. Git commands like `git subtree add` prefer a state where there are no uncommitted changes to prevent conflicts.
28+
29+
* **Check your status:** Run `git status` to see if you have any pending changes.
30+
* **Commit or Stash:** If you have modifications, either commit them (`git add . && git commit -m "WIP: Prepare for subtree addition"`) or temporarily stash them (`git stash`).
31+
32+
#### Step 1: Add the External Repository as a Remote
33+
34+
First, we'll add the external repository as a remote to your current Git project. This gives it a short, memorable name that you can use to reference it later.
35+
36+
**Explanation:** This command tells your local Git repository about the existence of the `fezcodex.stories` repository and associates it with the name `fezcodex-stories`. This makes it easier to fetch from or push to this external repository without typing out the full URL every time.
37+
38+
**Command:**
39+
```bash
40+
git remote add fezcodex-stories https://github.com/fezcode/fezcodex.stories
41+
```
42+
43+
#### Step 2: Add the Remote as a Subtree
44+
45+
Now, we'll integrate the content from the `fezcodex-stories` remote into a specific directory within your project (`public/stories`).
46+
47+
**Explanation:**
48+
* `git subtree add`: This is the core command to add a subtree.
49+
* `--prefix public/stories`: This specifies the local directory within your main project where the content from the external repository will reside. Git will create this directory if it doesn't exist.
50+
* `fezcodex-stories`: This is the name of the remote you defined in Step 1.
51+
* `main`: This indicates the branch from the `fezcodex-stories` remote that you want to pull. **Important:** Double-check the default branch name of the external repository (it might be `master` instead of `main`).
52+
* `--squash`: This option is highly recommended. It squashes all the commits from the external repository's history into a single commit when adding it to your main repository. This keeps your main project's commit history cleaner, preventing it from being flooded with potentially hundreds of commits from the external source.
53+
54+
**Command:**
55+
```bash
56+
git subtree add --prefix public/stories fezcodex-stories main --squash
57+
```
58+
59+
### Managing Your Git Subtree
60+
61+
Once your subtree is set up, here's how you'll typically interact with it.
62+
63+
#### Pulling Updates from the Subtree Source
64+
65+
The primary reason for using `git subtree` for content is to easily keep it updated. When the original `fezcodex.stories` repository has new content, you can pull those changes into your project.
66+
67+
**Explanation:** This command is very similar to the `add` command, but `pull` fetches the latest changes from the specified remote and branch, and then merges them into your local subtree directory. The `--squash` option again helps to keep your history tidy by squashing the incoming changes into a single merge commit.
68+
69+
**Command:**
70+
```bash
71+
git subtree pull --prefix public/stories fezcodex-stories main --squash
72+
```
73+
74+
#### Making Changes within the Subtree and Pushing Back (Optional)
75+
76+
Sometimes, you might make modifications to the files within your `public/stories` directory (the subtree content) and wish to contribute those changes back to the original `fezcodex.stories` repository.
77+
78+
**Explanation:**
79+
* First, commit your changes in your main repository as you normally would.
80+
* Then, use `git subtree push`. This command takes the commits related to your `public/stories` directory and pushes them to the `main` branch of the `fezcodex-stories` remote.
81+
* **Important:** You must have push access to the original `https://github.com/fezcode/fezcodex.stories` repository for this to work. If you don't, you'd typically fork the original repository, push to your fork, and then open a pull request.
82+
83+
**Command:**
84+
```bash
85+
git subtree push --prefix public/stories fezcodex-stories main
86+
```
87+
88+
#### Removing a Git Subtree (If Needed)
89+
90+
If you ever need to remove the subtree, it's a multi-step process:
91+
92+
**Explanation:**
93+
1. `git rm -r public/stories`: This removes the directory and its contents from your working tree and stages the deletion.
94+
2. `git commit -m "Remove subtree public/stories"`: Commits the removal.
95+
3. `git remote rm fezcodex-stories`: Removes the remote reference you added earlier.
96+
4. You might also want to clean up any leftover Git configuration related to the subtree, though `git remote rm` handles the main part.
97+
98+
**Commands:**
99+
```bash
100+
git rm -r public/stories
101+
git commit -m "Remove subtree public/stories"
102+
git remote rm fezcodex-stories
103+
```
104+
105+
### Conclusion
106+
107+
`git subtree` provides a robust and integrated way to manage external content within your main Git repository. It simplifies collaboration by making external content directly available upon cloning and streamlines the update process. By following these steps, you can effectively incorporate and maintain your `fezcodex.stories` content, or any other external project, within your `public/stories` directory.

public/posts/posts.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
[
2+
{
3+
"slug": "fezcodex-stories-with-git-subtrees",
4+
"title": "Fezcodex Stories with `git subtree`",
5+
"date": "2025-11-14",
6+
"updated": "2025-11-14",
7+
"description": "A short explanation of git subtrees",
8+
"tags": ["git", "subtree", "dnd", "stories"],
9+
"category": "dev",
10+
"filename": "fezcodex-stories-with-git-subtrees.txt"
11+
},
212
{
313
"slug": "publish-to-npm",
414
"title": "Publishing to NPM",

0 commit comments

Comments
 (0)