首先確認是否已經安裝 git。
git --version
如果已顯示版本,則不需要重新安裝。
如果沒有,請從 這裡 獲取安裝。
確認是否安裝成功。
git --version
應該會顯示版本。
git config --global user.name YOUR_USER_NAME
git config --global user.email [email protected]
ssh-keygen -t ed25519 -C "[email protected]"
不太確定,但可能會在過程中詢問儲存位置,指定為 ~/.ssh
。
在 ~/.ssh
目錄下會生成 id_ed25519
(私鑰)和 id_ed25519.pub
(公鑰)。
確認
ls ~/.ssh
cat ~/.ssh/id_ed25519.pub
該指令會在終端中顯示公鑰內容,記得復製下來。
也可以在 VSCode 中打開並複製。
透過瀏覽器前往 GitHub 並執行以下步驟:
git config --global core.autocrlf input
提示: 隨著處理的 repository 增多,可能會變得混亂,因此建議創建一個 repo
資料夾並將其放入該資料夾中。
git clone git@......
clone 完成後會生成 repository 的資料夾,進入該資料夾。
git branch
應該會顯示 master
或 main
。
git checkout -b my_branch
使用 git branch
可以查看到已創建的新分支,並且可以看到目前所在的分支(前面有 * 標記並顯示為綠色)。
~ 作業完成後 ~
git status
可以查看哪些文件已修改。
進行暫存
git add . # 全部暫存
git add FILE_NAME # 僅上傳指定文件
確認是否已成功暫存,使用 git status
檢查是否有多餘的文件。如果有多餘的文件,可以使用 git reset <filename>
來撤銷。
(當不小心暫存了多餘的文件時: 撤銷 git add)
創建 commit
git commit -m "描述所做的更改的訊息"
推送到遠端 (push)
git push -u origin my_branch
這樣就會反映到 GitHub 上。
更新遠端追蹤分支
git fetch
顯示所有本地和遠端的分支
git branch -a
將他人創建的分支引入本地
git checkout -b other_member_branch origin/other_member_branch
git checkout 目標分支名稱
切換後為了安全確認,使用 git branch
查看是否已成功切換。
切換到目標分支後,
git pull
提示: 為了減少衝突的可能性,建議在作業前先執行 git pull
。
# 確保 main 是最新狀態
git checkout main
git pull origin main
# 切換到作業分支
git checkout my_branch
# 合併 main
git merge main
方法一: 先提交更改,然後執行上述 正常流程。
方法二: 使用 stash 暫存更改。
# 暫存尚未提交的更改
git stash
# 更新 main 並合併
git checkout main
git pull origin main
git checkout my_branch
git merge main
# 恢復暫存的更改
git stash pop
關於 git stash pop
的補充
注意: 當使用 stash pop
恢復時,若合併後的代碼與自己的更改發生衝突,將會出現衝突。
也可以為 stash 添加註解。
範例
git stash push -m "stash 註解"
git stash
可以多次使用,因此可以通過索引進行管理。
檢查 stash
git stash list
輸出範例: stash@{0}
是最新的 stash,編號越大表示越舊。
stash@{0}: WIP on my_branch: 123abc4 增加登入表單
stash@{1}: WIP on my_branch: 456def7 修正錯字
stash@{2}: 在 main: 789ghij 快速除錯
指定索引提取
# 應用特定的 stash (保留該 stash)
git stash apply stash@{2}
# 應用特定的 stash 並刪除
git stash pop stash@{1}
刪除 stash
# 刪除特定的 stash
git stash drop stash@{0}
# 刪除所有 stash
git stash clear
下載子模組
git submodule init
git submodule update