---
tags: todo, github
---
# GitHub 教學
每週一主題
--------
每週1個主題, 一步步學 GitHub 指令
- 第0階段(準備): 建GitHub帳號, 建GitHub repo專案, 安裝 Git for Windows, git clone https://github.com/帳號/專案名
- 在這一階段, 還不會使用 git, 不過已經把相關工具/前置作業做好了
- 建 GitHub 帳號
- 建 GitHub repo 專案, 會在網頁按 new 來新增 repository, 也了解 `.gitignore` 及 `README.md`
- 安裝 Git for Windows, 有了 git 工具, 可以開始在 cmd.exe 小黑裡打字操作
- git clone https://github.com/帳號/專案名 將前面新增的專案下載, 也能下載別人的專案
- 第1階段(上傳雲端):
- 在這一階段, 熟悉 git 最常用的 1+3 指令, 在小黑視窗cmd.exe 裡下指令
- 也會順便熟悉 cmd.exe 裡的 DOS 指令 cd 切換目錄
- `git status` 查目前狀況
- `git add .` 將現在目錄加入
- `git commit -m "訊息"` 設定還原點
- `git push` 推/送上雲端
- 重點: 可以讓自己的 github 內容越來越豐富
- 第2階段(努力寫程式):
- 看看 GitHub contribution graph (會想塗鴉)
- 有自己的 public 及 private 的許多專案
- 隨時可在電腦中寫程式/編輯 `README.md` , 熟悉 Notepad++ 及其他程式開發工具
- 每個目錄都建 `README.md` 說明
- 可做自己網頁 `帳號.github.io`
- 每台電腦都裝 Git for Windows
- 第3階段(做錯可還原):
- `git log` vs. `git log --oneline` (一次一行) 熟悉 HEAD 及
- `git reflog` (倉庫中的 ref log)
- `git reset HEAD^` vs. `git reset HEAD^^` (回一步)
- More? 的[笑話](https://stackoverflow.com/questions/14203952/git-reset-asks-more)
- http://jdev.tw/blog/4239/git-rest-hard-head-in-windows-cmd-exe
- 可改用 `git reset "HEAD^"`
- `git reset --hard "HEAD^"` 或 `git reset --hard`
- `git push`
- 第4階段(更多的指令)
- git 的常見指令: status, add, diff, commit, push, pull, clone, log, reflog, reset,
- cmd.exe 配上 Git for Windows 其實簡單很夠用
- cmd.exe 的常見指令: `cd 目錄`, `cd ..` , `dir` 秀出目錄, `start` . 在這裡開啟檔案總管, `notepad filename` 開記事本編輯, `more filename` 慢慢看檔案
- cmd.exe 裡, 可用 `doskey /history` 看自己下過的指令
- cmd.exe 在 Windows 10 裡, 可用 mouse圈起來, 使用 Ctrl-C, Ctrl-V
- 第5階段(累積放網頁)
- yourname.github.com
- 你的專案, 從 main branch 改設好 gh-pages branch, 就可在 yourname.github.com/your_project 看到
- 在 Setting 設定 Jekyll theme
整理中的筆記
==========
GitHub Gist
---------------
有時需要分享程式碼教學時, 可以使用 GitHub Gist功能。
gist.github.com
有幾個設定的地方
- 檔名: Filename including extension 檔名+副檔名, 決定使用的語言
- 介紹: Description
- Space vs. TAB 的戰爭
- Public vs. Private
- Add File
git.io 短網址
------------
因為 Gist 的網址太長了, 可利用 git.io 來縮短網址
- 可直接用 git.io 來設定
- 但如果想要自己的短網址名,可利用 curl 來設定參數
- 下載 [curl for Windows](https://curl.se/windows/)
- 指令 curl -i https://git.io -F "url=https://gist.github.com/jsyeh/很長的網址" -F "code=jsyeh-caesar.c"
- 參考資料: https://titangene.github.io/article/github-url-shortener.html
- 參考資料: https://github.blog/2011-11-10-git-io-github-url-shortener/
```
C:\Users\USER\Downloads\curl-7.75.0-win64-mingw\bin>curl -i https://git.io -F "url=https://gist.github.com/jsyeh/1db893bbdf7dc0fa7a5e117b4757067e" -F "code=jsyeh-caesar.c"
HTTP/1.1 201 Created
Server: Cowboy
Connection: keep-alive
Date: Fri, 05 Feb 2021 18:03:41 GMT
Status: 201 Created
Content-Type: text/html;charset=utf-8
Location: https://git.io/jsyeh-caesar.c
Content-Length: 62
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Runtime: 0.009971
X-Node: 799434ea-9da1-4674-9d8e-34420f9b2ff2
X-Revision: 392798d237fc1aa5cd55cada10d2945773e741a8
Strict-Transport-Security: max-age=31536000; includeSubDomains
Via: 1.1 vegur
```
學習 Git-it
-----------
- 使用 electron 開發的跨平台教學軟, 使用 javascript 做到。
-
買網址
-----
- 先用 whois 去查有沒有被買走
- 再用 godaddy 確認可以買
- 再查 gmail.com 還能不能使用
-
規劃
---
- Idea: GitHub 教學, 應該放在 GitHub 上。
- 可以先在 HackMD 寫好, 因為寫起來比較快
- 這樣也能整合成 book 方便慢慢翻閱
- 每週的小任務, 就是一個小文件, 這樣書讀起來比較輕鬆
修改日期
-------
https://mail.google.com/mail/u/0/#search/github/KtbxLzGDXqBfVflmZZJXkzkvtkfCbGrgbq
todo: 關於 github commit 日期設定 用 --date="2021-01-25 13:00:00" 即可
git commit -m "add step04_WaveFunc" --date="2021-01-24 13:00:00"
在Windows 簡單操作
----------------
https://mail.google.com/mail/u/0/#search/github/QgrcJHsbdJHZVTmfdDcQbMwZZVgTWDwtVXV
1. 安裝 Git for Windows
2. 利用 `git clone https://github.com/帳號/repo名` 下載複製
2.1. 如果是 private repo, 需要用 browser 登入 github
3. 修改之後, `git add .` 把你目錄裡修改的檔案加進去
4. 使用 `git commit -m "訊息"` 記下你的還原點訊息
4.1. 如果第1次使用, 需要設定 email 及 名字
4.2. `git config --global user.email "jsyeh@mail.mcu.edu.tw"`
4.3. `git config --global user.name "jsyeh"`
4.4. 如果想要偽造日期, 可以使用 `git commit -m "訊息" --date="2021-01-21 13:00:00"` 來改日期
5. `git push` 推回 github 上面
如何用上癮
--------
https://mail.google.com/mail/u/0/#inbox/KtbxLvHPwtsGBxCthkjhKgfCSCBnkLKdhg
todo 這些使用的方法,列入github的教學主題 2021/1/13
- 邊學邊用: 不用全部學完才用。邊學邊用,能學得更好。用之後,才有動機學更多更深入、學更好。
- 打卡: 那個daily usage graph 像打卡機制一樣,讓人有成就想去持續做。(我們學程式,也可用一樣的作法)
- 手機 APP + Browser
- Q: App為何沒有 contributions graph? A: 可在Chrome用手機看到
- Q: App要如何改README. md A: 可在Chrome裡用手機修改
- Q: 那GitHub App能做什麼? 只能 create issue嗎?
- A: 其實只適合view,如果想改code可在手機chrome裡修改
- (GitHub App可看完後,將 repo share時複製網址,再於chrome裡貼網址修改)
如何 reset
----------
`git add .` 之後如果後悔, 只要在 `git commit` 之前, 都可以 `git reset`
比如說, Unity 的專案會產生大量不需備份的檔案, 可以使用 .gitignore 來排除。但是如果不小心忘了把 .gitignore 放在正確的 Unity 專案目錄中, 就會在 `git add .` 時, 看到大量的檔案被加入。這時候, 可以使用 `git reset` 來放棄剛剛的 `git add .` 動作。
Reference: https://www.freecodecamp.org/news/how-to-undo-a-git-add/
commit還原
---------
回憶一下, 我如何在 git 中將錯誤的 commit 還原。 2021/1/29
事由: 我想要在特定時間 commit , 結果忘記了。想要時間還原, 可以嗎?
基礎: 已經會
關於 commit 後悔
https://xken831.pixnet.net/blog/post/478332482
1. Git Bash, 到專案目錄中,
2. `git reset --hard commitID`
3. `git push -f`
關於 commit 後悔
https://gitbook.tw/chapters/using-git/reset-commit.html
1. `git log --oneline` 可以秀出 commitID
2. `git reset HEAD^` 或 `git reset master^` 或 `git reset` 查出想要的commitID
3. reset 有幾種模式: Mixed - keep working copy but reset index
3.1. --mixed 工作目錄不變, Stage丟掉
3.2. --soft 只移 HEAD, 不丟檔(工作目錄不變,Stage不變)
3.3. --hard 將工作區、暫存區(Stage)的檔案都丟掉
3.4. 名詞中文翻譯: 工作目錄(working directory) --git add--> 暫存區(staging rea) --git commit--> 儲存庫(repository)
關於 `git reset HEAD^` 的 ^符號出問題
https://stackoverflow.com/questions/14203952/git-reset-asks-more
Windows cmd.exe 裡, ^ 是特殊符號, 所以
`git reset HEAD^` 要改成
`git reset HEAD^^`
或是 `git log Head~1`
或是 `git reset "Head^"`
或是改用 Git Bash
關於 --hard
`git reset --hard "HEAD^"`
關於 reflog
https://gitbook.tw/chapters/using-git/restore-hard-reset-commit.html
1. `git reflog` 可看到之前 `git reset --hard` 刪掉的 commitID 及檔案
2. 這時再 `git reset --hard commitID` 到你要的那個 commitID即可
什麼是 commitID
操作歷史記錄
----------
```
>doskey /history
dir
cd code
dir
cd ad
dir
cd unityPBD
dir
more .gitignore
dir
copy ..\README.md .
start .
git status
git diff
cd ..
cd glutPBD
dir
notpad glutPBD.layout
notepad glutPBD.layout
git diff
git status
more README.md
start .
git status
git commit -m "finish unityPBD - one spring version
git add .
git commit -m "finish unityPBD - one spring version
git add .
git status
cd ..
git add .
git status
git commit -m "finish unityPBD - one spring version
git push
git status
git diff
dir
cd glutPBD
dir
notepad glutPBD.layout
git status
cd ..
git status
git diff
git status
git diff
doskey /history
C:\Users\User\code\ad>
C:\Users\User\code\marbling>doskey /history
git clone https://github.com/jsyeh/marbling
cd marbling
git log
git log --online
git log --oneline
git reset 0f692ea
git status
git pupsh
git push
git status
git reflog
cd ..
cd code
cd marbling
dir
git reflog
git status
git reset 0f692ea
git reset 0f692ea --hard
git help push
git
git push
git push -f
git status
git diff
git status
git diff
git status
git add .
git status
git diff
git add .
git status
git log
git log --oneline
git diff
git add .
doskey /history
C:\Users\User\code\marbling>
C:\Users\User\code\marbling>git log --oneline
6180db2 (HEAD -> main, origin/main, origin/HEAD) Update README.md
19104b5 update README.md - MOST list
3271516 update README.md - MOST list
5a4f6e0 update README.md - recover
20ba054 add step05_CircularTineLineFunction
e7fc940 add step05_CircularTineLineFunction
0f692ea add step04_WaveFunc
3037c5a modify step03_TineLineFunc with undo
6c4fcb1 add step03_TineLineFunc
37bb434 toggle bSimpleInkDrop for simple/deformation
83734d1 MathMarbling/README.md - Step02, TODO
0573c47 add step02_DropInkFunction
9375e9e Update README.md - More Details
f9e8e59 Update README.md ( angle in for loop)
b9cfe66 Update README.md with More Detail
63694c5 update step01_ArrayList_PVector README.md and comments
64251f1 add step01_ArrayList_PVector
3be8f99 add step01_ArrayList_PVector
751b662 add step00_ManyEllipse
017117a add step00_ManyEllipse
82e88f2 Introduction to Mathematical Marbling
1b87bc7 download Jaffer's Mathematical Marbling paper
02c533d Update README.md
5b4671e Initial commit
C:\Users\User\code\marbling>g
C:\Users\User\code\marbling>doskey /history
git diff
git commit -m "add step05_CircularTineLineFunction" --date="2021-01-25 13:00:00"
git add .
git status
git commit -m "add step05_CircularTineLineFunction" --date="2021-01-25 13:00:00"
git push
git pull
git reflog
git status
git log
git status
git add .
git status
dir
more README.md
notpad README.md
notepad README.md
git status
git diff
git commit -m "add step05_CircularTineLineFunction" --date="2021-01-25 13:00:00"
gi status
git status
git add .
git commit -m "add step05_CircularTineLineFunction" --date="2021-01-25 13:00:00"
git status
git log
git push
git commit -m "update README.md - recover
git status
git push
dir
cd ..
notepad README.md
git commit -m "update README.md - MOST list" --date="2021-01-26 13:00:00"
git push
git commit -m "update README.md - MOST list" --date="2021-01-26 13:00:00"
git push
git status
git add .
git diff
git status
git diff
git status
git diff
git commit -m "Update README.md" --date="2021-01-27 13:00:00"
git status
git push
git log
git log --oneline
doskey /history
C:\Users\User\code\marbling>
C:\Users\User\marbling\MathMarbling>doskey /history
cd marbling
dir
git pull
git status
more README.md
dir
git status
git diff
git log
git reflog
git log
git status
git diff
notepad README.md
cd MathMarbling
notepad README.md
doskey /history
C:\Users\User\marbling\MathMarbling>
```