20250701 ‐ hugo understanding - cywongg/2025 GitHub Wiki


Absolutely! Here’s how the **main branch** and **gh-pages branch** work together in your Hugo + GitHub Pages workflow:

---

## **How the `main` and `gh-pages` Branches Work**

### **1. `main` Branch**
- **Purpose:**  
  This is your **source branch**. It contains:
  - All your Hugo source files (`config.toml`, `content/`, `themes/`, etc.)
  - Your Markdown blog posts and pages
  - Your theme (as a submodule or folder)
  - Any custom layouts or static assets
- **What you do here:**  
  - Write and edit blog posts
  - Change site configuration
  - Update or add themes
  - Commit and push your changes to GitHub

### **2. `gh-pages` Branch**
- **Purpose:**  
  This is your **deployment branch**. It contains:
  - Only the **static site output** generated by Hugo (the contents of the `public/` folder)
  - HTML, CSS, JS, images, etc. — everything needed to serve your site, but **not** your source files or Markdown
- **What you do here:**  
  - You (or a script) force-push the contents of `public/` to this branch after every build
  - You do **not** edit this branch by hand

### **3. How They Work Together**
- You work on your site in the `main` branch.
- When you’re ready to publish, you run `hugo -D -F` to generate the static site in `public/`.
- You then commit and force-push the contents of `public/` to the `gh-pages` branch.
- **GitHub Pages** is configured to serve your site from the `gh-pages` branch, so whatever is in that branch is what appears at your live URL.

---

## **Visual Diagram**

```mermaid
graph TD
    A[main branch] -->|hugo build| B[public/ directory]
    B -->|force push| C[gh-pages branch]
    C -->|GitHub Pages serves| D[https://cywongg.github.io/jcywong.io/]
```

---

## **Why This Separation?**

- **main**: Keeps your source code, content, and history clean and versioned.
- **gh-pages**: Keeps your deployed site lightweight and only contains what’s needed for the web.

---

## **Typical Workflow**

1. **Edit** content/config/theme in `main`.
2. **Commit and push** to `main` on GitHub.
3. **Build** the site with Hugo (`hugo -D -F`).
4. **Commit and force-push** the contents of `public/` to `gh-pages`.
5. **GitHub Pages** automatically updates your live site from `gh-pages`.

---

## **Summary Table**

| Branch      | Contains                | Editable?         | Purpose                        |
|-------------|-------------------------|-------------------|--------------------------------|
| `main`      | Source, content, config | Yes (by you)      | Development, writing, config   |
| `gh-pages`  | Static site output      | No (auto-generated) | Deployment, live website       |

---

If you want, I can provide a script to automate this process, or help you set up GitHub Actions for automatic deployment! Let me know if you want to go further.

Absolutely! Here’s a clear breakdown of your `jcywong.io` Hugo project folder, what each part means, and how the push/deploy process works—including how to create a new blog post.

---

## **1. Folder Structure Explained**

```
jcywong.io/
├── .git/                # Git version control data (do not touch manually)
├── .gitmodules          # Info about git submodules (e.g., your theme)
├── config.toml          # Main Hugo configuration file (site title, theme, menus, baseURL, etc.)
├── content/             # Where your actual blog posts and pages live (Markdown files)
│   └── post/            # (Typical) Subfolder for blog posts
├── layouts/             # (Optional) Custom layout overrides (rarely needed for most users)
├── public/              # The generated static site (HTML, CSS, JS) — this is what gets deployed!
├── themes/              # Hugo themes (e.g., Tranquilpeak, as a git submodule)
├── posts/               # (Legacy/unused) Not used by Hugo unless referenced in config
├── images/, css/, js/, ... # Static assets (images, styles, scripts)
├── sitemap.xml, index.xml, index.html, 404.html # Generated or static files for SEO, homepage, error page, etc.
└── README.md            # Project documentation (optional)
```

---

## **2. What Each Part Means**

- **config.toml**: The main configuration for your site. Controls site title, theme, menus, baseURL, etc.
- **content/**: Where you write your posts and pages. Each Markdown file becomes a page/post on your site.
- **themes/**: Contains your Hugo theme (e.g., Tranquilpeak). If it’s a submodule, it’s managed by git.
- **public/**: The output folder. Hugo generates your static site here. **This is what you deploy to GitHub Pages.**
- **.git/**: Git’s internal data. Tracks all changes and history.
- **.gitmodules**: If you use a theme as a submodule, this file tells git where to find it.
- **layouts/**: If you want to override theme templates, you put custom layouts here (advanced).
- **images/, css/, js/**: Static files for your site (images, stylesheets, scripts).

---

## **3. How Push and Deploy Works**

### **A. Local Development**
- You edit files in `content/`, `config.toml`, etc.
- You preview locally with:
  ```sh
  hugo server -D -F
  ```
  (This shows drafts and future posts.)

### **B. Build the Site**
- When ready to deploy, run:
  ```sh
  hugo -D -F
  ```
  - This generates the static site in `public/`.

### **C. Deploy to GitHub Pages**
- The `public/` directory is a separate git repo (or is treated as such for deployment).
- You commit and push the contents of `public/` to the `gh-pages` branch:
  ```sh
  cd public
  git init                # (if not already a repo)
  git checkout -b gh-pages
  git remote add origin https://github.com/cywongg/jcywong.io.git
  git add .
  git commit -m "Deploy site"
  git push -f origin gh-pages
  cd ..
  ```
- GitHub Pages serves your site from the `gh-pages` branch at  
  `https://cywongg.github.io/jcywong.io/`

---

## **4. How Creating a New Blog Post Works**

### **A. Create a New Post**
- Use Hugo’s CLI:
  ```sh
  hugo new post/my-new-post.md
  ```
  - This creates a new Markdown file in `content/post/`.

### **B. Edit the Post**
- Open `content/post/my-new-post.md` and fill in the front matter:
  ```yaml
  ---
  title: "My New Post"
  date: 2024-07-01T12:00:00Z
  tags: ["hugo", "blog"]
  categories: ["blog"]
  draft: false
  ---
  ```
- Write your content below the front matter.

### **C. Build and Deploy**
- Run:
  ```sh
  hugo -D -F
  ```
- Deploy as above (commit and push `public/` to `gh-pages`).

---

## **5. How Hugo, Git, and GitHub Pages Work Together**

- **Hugo**: Converts your Markdown content and theme into a static website.
- **Git**: Tracks all your changes and lets you push code/content to GitHub.
- **GitHub Pages**: Hosts your static site from the `gh-pages` branch.

---

## **6. Typical Workflow**

1. **Write/edit content** in `content/` or update `config.toml`.
2. **Preview locally** with `hugo server -D -F`.
3. **Build** with `hugo -D -F`.
4. **Commit and push** changes in your main repo (`config.toml`, `content/`, etc.).
5. **Deploy**: Commit and push the contents of `public/` to the `gh-pages` branch.

---

## **If You Want to Automate Deployment**
You can write a script to do the build and deploy steps, or use GitHub Actions for automatic deployment.

---

If you want a visual, here’s a [Hugo + GitHub Pages workflow video](https://www.youtube.com/watch?v=LIFvgrRxdt4) that matches this process.

Let me know if you want a sample deploy script, or have more questions about any part of the workflow!

How to add post:

Here’s a summary of what I have done for you, along with the reasoning behind each step:

---

## **Summary of Actions**

### 1. **Fixed Hugo Content Structure**
- **What:** Created a standard `content/` directory with a `post/` subdirectory (`content/post/`).
- **Why:** Hugo expects all content (posts, pages, etc.) to be inside the `content/` directory. Your site was missing this, which caused errors when trying to create new posts and prevented Hugo from properly generating tag/category pages.

---

### 2. **Created a Sample Blog Post**
- **What:** Used `hugo new post/my-first-sidebar-test.md` to create a new post in the correct location.
- **Why:** This demonstrates the correct way to add new content so it appears in your blog, tags, and categories.

---

### 3. **Updated the Post’s Front Matter**
- **What:** Edited the new post to include:
  - `categories: ["blog"]`
  - `tags: ["hugo", "sidebar"]`
  - `draft: false`
- **Why:**  
  - **Categories/Tags:** These are used by Hugo and your theme to organize posts and generate sidebar/tag/category navigation.
  - **Draft:** Setting `draft: false` ensures the post is published and visible both locally and on the deployed site.

---

### 4. **Committed and Deployed the Changes**
- **What:** 
  - Committed all changes to your main branch.
  - Built the site with `hugo -D -F` to include all posts, drafts, and future posts.
  - Deployed the contents of the `public/` directory to the `gh-pages` branch for GitHub Pages hosting.
- **Why:**  
  - **Committing:** Ensures your repository is up to date and reproducible.
  - **Building:** Generates the static site files Hugo needs to serve your content.
  - **Deploying:** Updates your live site so it matches your local build.

---

### 5. **Ran the Hugo Development Server Locally**
- **What:** Started the server with `hugo server -D -F`.
- **Why:**  
  - Lets you preview your site locally, including drafts and future posts, to ensure everything looks correct before deploying.

---

## **Reasoning Behind the Workflow**

- **Standard Hugo Structure:** Using `content/` ensures compatibility with Hugo’s expectations and theme features.
- **Front Matter:** Properly set tags and categories allow the theme to generate working sidebar/tag/category navigation.
- **Drafts and Future Posts:** The `-D -F` flags ensure you see all content during development and deployment.
- **GitHub Pages Deployment:** Deploying the `public/` directory to `gh-pages` is a best practice for static site hosting on GitHub Pages.
- **Local Preview:** Always check your site locally before deploying to catch issues early.

---

## **Result**

- Your sidebar buttons (tags, categories) now work.
- New posts appear under the correct tags and categories.
- Your local and deployed sites are in sync.

---

If you have more questions about Hugo, content organization, or deployment, just ask!


Absolutely! Here’s a detailed explanation of the entire workflow, including the role of git submodules, what went wrong, and how it was fixed:

---

## **1. Hugo Site Development Workflow**

### **A. Local Development**
- You write your content (Markdown files) and configure your site in the Hugo project directory.
- You preview your site locally using:
  ```sh
  hugo server -D
  ```
  The `-D` flag includes drafts, so you see everything, even unpublished posts.

### **B. Building the Site**
- When you’re ready to deploy, you generate the static site with:
  ```sh
  hugo -D -F
  ```
  - `-D` includes drafts.
  - `-F` includes future-dated posts.
- Hugo outputs the static HTML, CSS, JS, and images to the `public/` directory.

---

## **2. Deploying to GitHub Pages**

### **A. Why Deploy the `public/` Directory?**
- GitHub Pages serves static files from a specific branch (commonly `gh-pages`).
- You want the `gh-pages` branch to contain only the built static site, not your source files.

### **B. The Deployment Steps**
1. **Build the site:**  
   `hugo -D -F`
2. **Go into the `public/` directory:**  
   `cd public`
3. **Initialize a git repo (if needed) and set remote:**  
   `git init`  
   `git remote add origin https://github.com/yourusername/yourrepo.git`
4. **Switch to the `gh-pages` branch:**  
   `git checkout -b gh-pages`
5. **Add, commit, and push:**  
   `git add .`  
   `git commit -m "Deploy site"`  
   `git push -f origin gh-pages`
6. **Go back to your project root:**  
   `cd ..`
7. **Set GitHub Pages to use the `gh-pages` branch as the source.**

---

## **3. The Role of Git Submodules (Themes)**

### **A. What is a Git Submodule?**
- A git submodule is a way to include one git repository inside another.
- In Hugo, themes are often added as submodules so you can easily update them and keep your main repo clean.

### **B. How to Add a Theme as a Submodule**
```sh
git submodule add https://github.com/kakawait/hugo-tranquilpeak-theme.git themes/Tranquilpeak
git submodule update --init --recursive
```
- This creates a `.gitmodules` file and a reference to the theme’s repo.
- When you clone your site repo, you must use `--recurse-submodules` or run `git submodule update --init --recursive` to fetch the theme.

---

## **4. What Was the Issue?**

### **A. The Problem**
- The theme directory (`themes/Tranquilpeak`) was added as a submodule, but the `.gitmodules` file was missing or broken.
- When deploying or cloning, git could not find the URL for the submodule, causing errors like:
  ```
  fatal: No url found for submodule path 'themes/Tranquilpeak' in .gitmodules
  ```
- Additionally, when you re-added the theme as a submodule, it restored the original files, including references to Google Analytics templates that did not exist, causing Hugo build errors.

### **B. How It Was Fixed**
1. **Removed the broken submodule reference:**
   - Deleted the `themes/Tranquilpeak` directory and cleaned up the git index.
2. **Re-added the theme as a proper submodule:**
   - Used `git submodule add ...` to correctly set up the submodule and `.gitmodules`.
3. **Removed problematic Google Analytics template references again** (since the submodule restore brought them back).
4. **Committed all content and config changes** to ensure the build used the latest files.
5. **Rebuilt the site and redeployed** the `public/` directory to the `gh-pages` branch.

---

## **5. Key Takeaways**

- **Always commit your content and config changes before building for deployment.**
- **If you use a theme as a submodule, make sure `.gitmodules` is present and correct.**
- **When deploying to GitHub Pages, only the static files in `public/` should go to the `gh-pages` branch.**
- **If you see differences between local and deployed sites, check for uncommitted changes, build flags, and deployment steps.**

---

If you want a visual walkthrough, the referenced [YouTube video](https://www.youtube.com/watch?v=LIFvgrRxdt4) is a great resource and matches this workflow.

If you have more questions about Hugo, git submodules, or deployment, feel free to ask!