Jekyll

How To Deploy Jekyll Blogs with Git

Are you ready to take your blogging to the next level? If you’re looking to create a fast, simple, and customizable blog, Jekyll is an excellent choice. Not only is it a static site generator that transforms plain text into a website, but it also integrates seamlessly with Git for version control and deployment. In this post, we’ll walk you through the steps to deploy your Jekyll blog using Git, making it easy to manage your content and collaborate with others.

What You’ll Need

Before we dive into the deployment process, make sure you have the following:

  1. Ruby and Bundler: Jekyll is built on Ruby, so you’ll need to have Ruby installed on your machine along with Bundler for managing gem dependencies.
  2. Git: You’ll need to have Git installed to manage your repository.
  3. GitHub (or another Git hosting service): This guide will focus on deploying to GitHub Pages, but the process is similar for other platforms.
  4. A basic understanding of command-line interface (CLI): Most of the commands you’ll execute will be in a terminal.

Step 1: Install Jekyll

If you haven’t installed Jekyll yet, open your terminal and run the following command:

gem install --user-install jekyll bundler

This command installs Jekyll and Bundler in your local Ruby environment.

Step 2: Create a New Jekyll Site

With Jekyll installed, you can create a new blog. Run the following command in your terminal:

jekyll new myblog

Replace myblog with your desired blog name. Navigate into your new directory:

cd myblog

Step 3: Build Your Site Locally

Before deploying your site, it’s a good idea to preview it locally. Start the Jekyll server:

bundle exec jekyll serve

Your site will be available at http://localhost:4000. Open this URL in your web browser to see your new blog!

Step 4: Initialize a Git Repository

To track changes and deploy your blog, you need to initialize a Git repository:

git init

Next, add all files to your repository and commit them:

git add .
git commit -m "Initial commit"

Step 5: Create a GitHub Repository

  1. Go to GitHub and log in to your account.
  2. Click on the “+” icon in the top right corner and select “New repository.”
  3. Name your repository (e.g., myblog), and make sure to select “Public.”
  4. Do not initialize the repository with a README or any other files, as you’ve already done that locally.
  5. Click “Create repository.”

Step 6: Link Your Local Repository to GitHub

Now you need to connect your local repository to the GitHub remote repository you just created. Copy the HTTPS URL of your new repository, and run the following commands in your terminal:

git remote add origin <your-repo-url>
git branch -M main
git push -u origin main

Replace <your-repo-url> with the URL you copied from GitHub.

Step 7: Deploy to GitHub Pages

To deploy your blog on GitHub Pages, you will need to create a gh-pages branch. You can do this by running:

git checkout -b gh-pages

Next, build your Jekyll site and move the generated files to the root of your gh-pages branch:

JEKYLL_ENV=production bundle exec jekyll build
cp -r _site/* .
git add .
git commit -m "Deploying site"
git push -u origin gh-pages

Now your blog should be live at https://<your-username>.github.io/<your-repo-name>/.

Step 8: Configure Your Repository

Go to your GitHub repository, click on “Settings,” then scroll down to the “GitHub Pages” section. Ensure the source is set to gh-pages branch. This step confirms that your blog will be served from the correct branch.

Step 9: Update Your Blog

As you create new posts or make changes to your blog, simply commit your changes to the main branch and push to the gh-pages branch following the same steps as before.

git checkout main
# Make changes to your blog
git add .
git commit -m "Your commit message"
git push origin main

git checkout gh-pages
JEKYLL_ENV=production bundle exec jekyll build
cp -r _site/* .
git add .
git commit -m "Deploying updated site"
git push origin gh-pages

Conclusion

Deploying a Jekyll blog with Git is a powerful way to manage your content and keep your site up-to-date. With just a few commands, you can have a fully functional blog hosted on GitHub Pages. Now that you know how to deploy your Jekyll blog, you can focus on creating amazing content for your readers.

If you have any questions or need further assistance, feel free to reach out to us at Greenhost.cloud. Happy blogging!