- Introduction: The Necessity of Multi-Device Collaboration
- Core Principles of Hexo Multi-Device Synchronization
- Old Computer Operation Guide: Building Remote Synchronization Foundation
- New Computer Environment Setup: Restoring Blog from Remote Repository
- Daily Update Workflow: Dual Branch Collaborative Operation
- Common Problems and Solutions
- Advanced Tips: Private Repositories and Multi-Branch Strategies
- Conclusion: Best Practices for Multi-Device Synchronization
Introduction:The Necessity of Multi-Device Collaboration
A couple of days ago, I am thinking if I can update blogs using multiple devices (such as home computers, office computers, laptops) which has become the norm from my perspective. As an efficient static blog generator, Hexo’s default deployment mechanism only pushes static files to GitHub Pages, but source code files (Markdown articles, theme configurations, plugins) are not synchronized, leading to the following pain points in multi-device collaboration:
- Scattered storage of source code, unable to manage uniformly
- Repeated construction of environment configurations, low efficiency
- Sudden situations (such as computer failure) leading to source code loss
So I write this to build a complete Hexo multi-device synchronization solution through Git branch strategies, achieving seamless synchronization and management of source code across devices.
Core Principles of Hexo Multi-Device Synchronization
Hexo Directory Structure and Version Control Strategy
In the Hexo blog directory structure, the following files/directories require special attention:
| Directory/File | Function | Version Control Strategy |
|---|---|---|
node_modules/ |
Dependency package directory | Ignored (regenerated via package.json) |
themes/ |
Theme files | Must be synchronized (contains custom configurations) |
public/ |
Static generated files | Ignored (generated by hexo g) |
source/ |
Blog source code(Markdown) | Must be synchronized |
_config.yml |
Global configuration file | Must be synchronized |
package.json |
Dependency description file | Must be synchronized |
Git Branch Division of Labor Model
A dual-branch strategy is higly recommended:
- Source branch (e.g.,
hexo01orsource): Stores Hexo source code (Markdown, themes, configurations) - Deployment branch (e.g.,
masterorgh-pages): Stores static generated files (automatically pushed byhexo deploy)
Core logic:
- The source branch serves as the “development branch” where all manual modifications are completed.
- The deployment branch serves as the “production branch” only receiving static generated files. When pushing to the “development branch” using
git push origin hexo01, the “production branch” will be automatically synchronized. - The two branches are isolated through
.gitignoreconfiguration.
Old Computer Operation Guide:
Creating and Configuring Remote Branches
- Remote repository preparation
Create a repositoryusername.github.ioon GitHub and execute the following operations:1
2
3
4# Create hexo source branch
git branch hexo
# Set hexo as the default branch, this is a key step!
git branch --set-upstream-to=origin/hexo hexo - Local environment initialization
Execute in the blog root directory:
Initialize Git repository1
2
3git init
# Add remote repository
git remote add origin git@github.com:username/username.github.io.git
Local Environment and Remote Repository Synchronization
- Clean and copy files
Retain the .git folder and delete other files
Copy the original Hexo directory (except .deploy_git/) to the current directory
Commit source code to the remote branch1
2
3
4
5
6# Add all files
git add .
# Commit changes
git commit -m "Init hexo source branch"
# Push to the remote hexo branch
git push origin hexo
.gitignore Key Configuration
Create .gitignore file in the project root directory with the following content:
1 | .DS_Store |
New Computer Environment Setup
SSH Key Configuration and Environment Dependency Installation
- Generate and add SSH key
1
ssh-keygen -t rsa -C "your_email@example.com"
- Add the generated public key like id_rsa.pub to
GitHub's Settings > SSH and GPG Keys, mac is under~/.sshand windows is underC:\Users\username\.ssh - Configure Git global information
1
2git config --global user.name "YourName"
git config --global user.email "your_email@example.com"
Remote Repository Cloning and Dependency Recovery
- Clone the source branch2.Install dependencies
1
2git clone -b hexo git@github.com:username/username.github.io.git blog
cd blogNote:make sure you use the same version of (Node.js)[https://nodejs.org/en/download] to install the tools above otherwise there may be some compatibility problems.1
2
3
4
5
6# Install Hexo CLI
npm install -g hexo-cli
# Install project dependencies
npm install
# Install deployment plugins
npm install hexo-deployer-git
First Run Verification and Configuration Check
Local preview verification
1 | hexo clean |
Access http://localhost:4000 to confirm the blog runs normally
Deployment configuration check
Ensure the deployment configuration in _config.yml points to the master branch:
1 | deploy: |
Daily Update Workflow: Dual Branch Collaborative Operation
Source Code Update and Submission Process
- Pull the latest source code
1
git pull origin hexo
- Modify and commit
1
2
3
4
5
6
7# Add a new article
hexo new "New Article"
# Commit changes
git add .
git commit -m "Update blog content"
# Push to the remote source branch
git push origin hexo
Automation Script to Optimize Workflow
Create a deploy.sh script to simplify the process:
1 |
|
Grant execution permissions and run:
1 | chmod +x deploy.sh |
Common Problems and Solutions
Branch Switching Conflict Handling
When switching branches causes node_modules conflicts, execute:
1 | # Stash current changes |
If you can not run hexo command like hexo s on Mac,this is because the default terminal on Mac is zsh (which can actually be queried with an echo $SHELL command), and the user environment variable file is~/. zshrc. After executing the following command, the environment variable configuration was successful:
1 | echo 'PATH="$PATH:./node_modules/.bin"' >> ~/.zshrc |
Environment Dependency Inconsistency Issue
Ensure Node.js versions are consistent across devices, which can be locked via package.json:
1 | "engines": { |
On new devices, execute:
1 | npm install --no-save |
Theme and Plugin Synchronization Abnormality
- Check for nested Git in themes
Delete .git folders in theme directories:1
find themes/ -type d -name ".git" -exec rm -rf {} \;
- Manually synchronize theme updates
If the theme is updated, manage it via Git submodule:1
git submodule add <theme-repo> themes/<theme-name>
Advanced Tips: Private Repositories and Multi-Branch Strategies
Private Repositories to Protect Source Code Security
- Create a private repository
Create a new private repositoryhexo-sourceon GitHub to store source code - Migrate the source branch
1
2
3
4
5
6# Remove old remote
git remote remove origin
# Add new private repository
git remote add origin git@github.com:username/hexo-source.git
# Push the source branch
git push -u origin hexo - Configure dual deployment targets
Add to _config.yml:1
2
3
4
5
6
7deploy:
- type: git
repo: git@github.com:username/hexo-source.git
branch: hexo
- type: git
repo: git@github.com:username/username.github.io.git
branch: main
Multi-Branch Collaborative Development Model
- Feature branch workflow
1
2
3
4
5
6# Create a feature branch
git checkout -b feature/new-theme
# After development, merge into the hexo branch
git checkout hexo
git merge feature/new-theme
git push origin hexo
Best Practices for Multi-Device Synchronization
- Core principles:
- Separate storage of source code and static files
- Manage dependencies via package.json
- Always pull before committing to avoid conflicts - Operation checklist:
- For new devices:git clone -b hexo + npm install
- Daily updates:git pull→ edit →git push→hexo deploy(must need git pull firstly)
- Environment migration: Only requires Git cloning + dependency installation, no need for re-initialization - Advanced suggestions:
- Use GitHub Actions for automated deployment
- Regularly back up source branches to other cloud storages
- Adopt branch protection strategies for team collaboration