Git筆記

Git指令

功能 Code
使用者:名稱 git config --global user.name "Stella-X"
使用者:信箱 git config --global user.email "xxx@gmail.com"
config清單 git config --list
將指令自定縮寫 git config --global alias.xx xxxxxxxxx
初始化這個目錄,
讓 Git 對這個目錄開始進行版控
git init
將檔案加至暫存區(Staging Area) git add <檔案名> ex.xxx.html*.html--all
把暫存區的內容移至儲存庫
(Repository)。
git commit -m "message"
省略add步驟,直接commit
(限已在Repository之內容)
git commit -a -m "content"
對新加入的檔案(也就是 Untracked file)是無效的。
複製整份專案,用於第一次佈置專案資料夾 git clone <HTTPS or SSH>
設定專案連接之remote(GitHub專案)位置 git remote add <設定HTTPS or SSH代名詞,預設origin> <HTTPS or SSH>
更新專案內容為GitHub上最新版的內容 git fetch
git pull = git fetch + git merge git pull
先拉(Github上的版本合併後)再推(上Github),用於多人開發衝突狀況 git pull --rebase
PUSH至Github git push <remote代名詞> <分支名>
強制PUSH(覆蓋)Github上的版本 git push -f
查看所有分支 git branch
新增分支 git branch <分支名>
切換分支 git checkout <分支名>
合併<分支名>到目前分支 git merge <分支名>
修改分支名 git branch -m <改前分支名> <改後分支名>
刪除分支 git branch -d <分支名>
強制刪除未合併的分支 git branch -D <分支名>

Git流程 (Git flow)

定義開發流程的分支名,主要的分支有 masterdevelophotfixreleasefeature 這五種分支,各種分支負責不同的功能。其中 Master 以及 Develop 這兩個分支又被稱做長期分支,因為他們會一直存活在整個 Git Flow 裡,而其它的分支大多會因任務結束而被刪除。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

分支說明

常用命令列指令

Windows MacOS / Linux 說明
cd cd 切換目錄
cd pwd 取得目前所在的位置
dir ls 列出目前的檔案列表
mkdir mkdir 建立新的目錄
touch 建立檔案
copy cp 複製檔案
move mv 移動檔案
del rm 刪除檔案
cls clear 清除畫面上的內容

檔案操作指令

  • cd
    • cd ..目錄往上一層
    • cd /xxx切換至xxx目錄
    • cd xxx切換至當前目錄內之xxx子目錄
  • cp
    • cp index.html about.html把檔案 index.html 複製一份成 about.html
  • mv
    • mv index.html info.html把檔案 index.html 更名成 info.html
  • rm
    • rm *.html刪除在這個目錄裡所有的 html 檔

新增 SSH Key 至GitHub

設定 ssh 金鑰

在我們正式上傳到 github 以前,我們需要先設定 ssh 金鑰。為什麼要設定這個金鑰呢?這是為了讓 github 知道,拿著這個金鑰的操作者是屬於那一個帳號,以賦予上傳等等的權限。如果是第一次使用 ssh key,會需要先產生 ssh 的金鑰,在 Bash 輸入以下指令以產生金鑰

$ ssh-keygen                                   # 產生金鑰

Generating public/private rsa key pair.
Enter file in which to save the key (/home/jaycelin/.ssh/id_rsa):   # 金鑰存放路徑,直接按 Enter

Created directory '/home/jaycelin/.ssh'.
Enter passphrase (empty for no passphrase):    # 密碼,可設定可不設定,設定的話每次上傳會多需要輸入一次密碼

Enter same passphrase again:                   # 再輸入一次密碼

The key fingerprint is:                        # 之後會顯示你的 fingerprint,到這裡就完成 key 的產生了

金鑰生成以後會放在剛剛前面的金鑰存放路徑,我們要複製 id_rsa.pub 的內容到 github,可以使用 cat 的讀檔指令:

$ cat /home/jaycelin/.ssh/id_rsa.pub           # 讀取檔案內容

ssh-rsa ~一連串金鑰~ jaycelin@HOME-PC           # 從 ssh-rsa 複製到 username@pc-name

接著到 github 的設定頁面將剛剛複製的金鑰輸入進去,參考下圖流程

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

輸入完畢後就完成金鑰設定了。對於不同的 git server,例如 bitbucket 或 gitlab 等等,流程都是類似的,只是匯入金鑰的網頁介面稍有不同而已,重點就是要將 id_rsa.pub 的內容提供給伺服器。
參考網頁:https://blog.jaycetyle.com/2018/02/github-ssh/

VIM編輯器

https://gitbook.tw/chapters/command-line/vim-introduction.html


參考網頁:https://gitbook.tw