# 发布

以上所说的都是在本地的操作，如何让别人也可以访问自己的书籍，除了自己买域名，还可以利用现有的互联网平台：gitbook.com、 github.com等

## 1.  在 gitbook.com 上发布和管理书籍

* 需要先注册 gitbook 账号。可以单独注册，也可以使用 github 账号关联登录。
* 然后先创建一个 Orgnization 。 ![](https://upload-images.jianshu.io/upload_images/14597179-d243e6157ea9b295.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240)
* 再在这个 Orgnization 里面创建一个 Space。这个就是你的书籍项目了。 ![](https://upload-images.jianshu.io/upload_images/14597179-dde0232f5f81ab25.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240)
* 然后就可以在线写书了～书籍的在线浏览地址为：`https://yourorgnizationname.gitbook.io/yourspacename`

![https://learn-gitbook.gitbook.io/gitbook/](https://upload-images.jianshu.io/upload_images/14597179-98609e485a51c503.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240)

## 2.  在 gitbhub上发布书籍源码

* 1. 在GitBook项目目录，如gitbook中，执行如下命令，创建本地git仓库：

     ```
     git init
     ```

     ![](https://upload-images.jianshu.io/upload_images/14597179-8abbce55e01c8821.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240)
* 1. 使用文本编辑器，创建名为.gitignore的文件，内容如下：

     ```
     *~
     _book
     .DS_Store
     ```

     通过.gitignore文件，本地仓库将忽略临时文件和\_book文件夹，达到只保存书籍源码的目的。
* 1. 现在可以将本地书籍源码添加到本地git仓库中了：

     ```
     git add .
     ```

     ![image.png](https://upload-images.jianshu.io/upload_images/14597179-820bd40f0232f527.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240)
* 1. 添加更新说明

     ```
     git commit -m '更新说明文字'
     ```

     ![](https://upload-images.jianshu.io/upload_images/14597179-9d370f3748d42804.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240)
* 1. 建立本地仓库与远端仓库的对应关系

     ```
     git remote add origin https://远程仓库地址.git
     ```

     ![](https://upload-images.jianshu.io/upload_images/14597179-3d8df83df2b2b095.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240)
* 1. 将本地仓库内容同步到远端仓库：

     ```
     git push -u origin master
     ```

至此，就完成了将gitbook源码推送到远程仓库的任务，之后书籍内容修改后，执行如下操作即可：

```
git add .
git commit -m '更新说明文字'
git push -u origin master
```

## 3. 在 github上发布和管理书籍

需要在原有本地仓库新建一个分支，在这个分支中，**只保留\_book文件夹中的内容**，然后将这些内容推送到远程仓库的pages分支，启用pages服务，最终达到免费发布电子书的目的。

* 1. 新建分支

     ```
     git checkout -b gh-pages
     ```

     新建名为pages的分支，分支名称随意，但最好能反映出分支的用途。
* 1. 删除不需要的文件

     切换到pages分支后，我们需要将\_books目录之外的文件都清理掉：

     ```
     git rm --cached -r .
     git clean -df
     rm -rf *~
     ```
* 1. 添加忽略文件

     使用文本编辑器，创建名为.gitignore的文件，内容如下：

     ```
     *~
     _book
     .DS_Store
     ```

     通过.gitignore文件，本地仓库将忽略临时文件和\_book文件夹。
* 1. 复制\_book文件夹到分支根目录

     ```
     cp -r _book/* .
     ```
* 1. 添加文件

     ```
     git add .
     ```

     添加更新说明

     ```
     git commit -m '更新说明'
     ```

     推送

     ```
     git push -u origin gh-pages
     ```

     现在开启git托管网站的pages服务即可。

     **4. gitbook 与 github 关联同步**

新版 gitbook.com 不支持本地版本管理了，但是对 github 的集成支持的不错。可以通过配置，实现在 github 项目里面提交内容，gitbook 平台会自动同步过去。

* 在 gitbook 平台里，进入要设置的 space，也就是你的书。
* 点左下角的配置按钮，进入配置，点击 Intergrations ，找到 github。

  ![](https://upload-images.jianshu.io/upload_images/14597179-2c006b5428f0b283.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240)
* 点击 link you github repository 按钮，根据向导，登录 github ，选择 reposirory，选择分支，完成绑定和同步。(你还可以选择是 gitbook 同步 github ，还是 github 同步 gitbook)

  > 需要注意的是：绑定的 github 仓库分支里面要是 gitbook 的源码，也就是那些 md 文件。而不是 build 之后生成的 html 文件。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learn-gitbook.gitbook.io/gitbook/fa-bu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
