Git Pages初体验

122 阅读2分钟

由于我目前有一个想要放静态资源的需求,就去查看了Git Pages怎么用。

参考链接:

创建一个.github.io的仓库

前往GitHub并创建一个名为username .github.io 的新公共存储库,其中username是您在 GitHub 上的用户名(或组织名称)。

image.png

提交代码

git clone https://github.com/*用户名*/*用户名*.github.io
cd*用户名*.github.io
echo "Hello World" > index.html

git add --all
git commit -m "初始提交"
git push -u origin main

启动项目

启动浏览器并访问https:// username .github.io

如何放其他项目

启动后就出现了一个问题,那我所有要展示的页面都要放在github.io这个仓库里吗,只能使用master分支吗。百度了一下,找到了上面的那个资源。

可以使用同git下的其他仓库,指定对应的分支和想要展示的文件夹,这样就方便使用vue/react等了。

1.新建一个仓库test

2.提交代码

git clone https://github.com/*用户名*/test.git
cd test

git checkout -b gh-pages
mkdir dist
echo "Hello World" > index.html
git add --all
git commit -m "初始提交"
git push -u origin main
  1. 将当前分支的dist设置为需要展示的路径 git subtree push --prefix=dist origin gh-pages

但是此时我发现https:// username .github.io/test/ 并没有展示我想要的index.html,而是在https:// username .github.io/test/dist/ 这个路径下展示了我想要的index.html。再次设置就提示了这样的错误。


git push using: origin gh-pages  
Enter passphrase for key '/Users/miotech-099/.ssh/id_rsa':  
To github.com:****/test.git  
! [rejected] f92af18d442b094a2e91285ff2fb0df93e993f38 -> gh-pages (non-fast-forward)  
error: failed to push some refs to 'github.com:****/test.git'  
hint: Updates were rejected because the tip of your current branch is behind  
hint: its remote counterpart. Integrate the remote changes (e.g.  
hint: 'git pull ...') before pushing again.  
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

后来执行了这样的强制更新代码才更新成功。

git push origin `git subtree split --prefix=dist gh-pages`:gh-pages --force

4. 可以用了https:// username .github.io/test/