# gitBook應用

安裝

```
npm install gitbook -g
```

## 使用gitbook init初始化書籍目錄&#x20;

README.md和Summary.md是兩個必須文件，README.md是對書籍的簡單介紹

```
gitbook init
Installing GitBook 3.2.3........

tree  /home/pi/mygithub/mynote/
/home/pi/mygithub/mynote/
├── README.md
└── SUMMARY.md

```

## gitbook服務

使用gitbook serve編譯書籍

```
gitbook serve
Live reload server started on port: 35729
Press CTRL+C to quit ...
........
........
Starting server ...
Serving book on http://localhost:4000

```

![](https://529150212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lh81JtaIQ84bwQqz_Wh%2F-LwVaNb5bnDGBrTkpMIF%2F-LwVeyzDuz3t4sc5uD3e%2Fimage.png?alt=media\&token=77dab749-36a6-4537-b43f-f490f37e1088)

## 建立書籍

```
gitbook build
```

```
git checkout --orphan gh-pages
git rm --cached -r .
rm -f *.md
echo "*~" > .gitignore
echo "_book" >> .gitignore

git commit -m "Ignore some files"
git checkout --orphan gh-pages
cp -r _book/* .
git add .
git commit -m "Publish book"
git config --global user.email "kawsing@gmail.com"
git config --global user.name "kawsing"
git remote add origin https://github.com/kawsing/mynote.git
git push -u origin gh-pages
```

![](https://529150212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lh81JtaIQ84bwQqz_Wh%2F-LwVfNdbYS1f7m3Znr3Z%2F-LwVm5NcGMppXFrLVfNL%2Fimage.png?alt=media\&token=30a54b2a-83e5-4d1b-848f-81fb2b1531cc)

## 變更內容

### nano SUMMARY.md

```
# MYSUMMARY

* [Chapter1](chapter1/README.md)
  * [Section1.1](chapter1/section1.1.md)
  * [Section1.2](chapter1/section1.2.md)
* [Chapter2](chapter2/README.md)

```

### gitbook build重新建立書籍

```
gitbook build
cp -r _book/* .
git add .
git commit -m "Publish Book"
git push -u origin gh-pages
```

<div align="left"><img src="https://529150212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lh81JtaIQ84bwQqz_Wh%2F-LwVunVVPivVLtHwU4bv%2F-LwW6HvIvU8i7Aj3xn_i%2Fimage.png?alt=media&#x26;token=8c98229d-181c-45c5-ae7f-b3ce2e53d600" alt=""></div>

## 完整流程

```
mkdir Book
cd Book/
gitbook init
gitbook build
pi@ubuntu:~/mygithub/Book$ ls
_book  README.md  SUMMARY.md

pi@ubuntu:~/mygithub/Book$ git checkout --orphan gh-pages
fatal: not a git repository (or any of the parent directories): .git

pi@ubuntu:~/mygithub/Book$ ls -a
.  ..  _book  README.md  SUMMARY.md

pi@ubuntu:~/mygithub/Book$ git init
Initialized empty Git repository in /home/pi/mygithub/Book/.git/

pi@ubuntu:~/mygithub/Book$ git checkout --orphan gh-pages
Switched to a new branch 'gh-pages'
git rm --cached -r .

rm -rf *~

echo "*~" > .gitignore
echo "_book" >> .gitignore
git checkout --orphan gh-pages

git init
git commit -m "Ignore some files"
cp -r _book/* .

git add .

git commit -m "Publish book"
git push -u origin gh-pages
git remote add origin https://github.com/kawsing/Book.git
git commit -m "Publish book"
git push -u origin gh-pages

pi@ubuntu:~/mygithub/Book$ ls
_book  gitbook  index.html  README.md  search_index.json  SUMMARY.md

```
