指令流程
圖片來源:git command flow
基本概念
如果想知道所有可用指令,可以使用 git help -a
。
- master:default develop branch
- origin:default upstream branch
- HEAD:current branch
- HEAD^:parent of HEAD
- HEAD~4:great-great grandparent of HEAD
project 的 .git
目錄中可以看到一個叫做 HEAD
的檔案,內容為:
$ cat .git/HEAD ref: refs/heads/master
這表示目前 HEAD 指向的是 master
分支。
Git 設定
git config --list
:查看所有設定。git config --global userr.name "Your Name"
:設置附加到你的 commits 和 tags 的名稱。git config --global user.email “you@example.com”
:設置附加到你的 commits 和 tags 的Email。
開始一個項目
git init [project name]
:創建一個新的本地repo。如果提供了 [project name],Git 將創建一個新的目錄名稱 [project name] 並將初始化repo。如果未提供 [project name],則提供一個新的 repo 在當前目錄中初始化。git clone [project url]
:從遠程(remote) repo 下載具有完整歷史記錄的 project。
日常工作
基本版更
git status
:顯示您的 project 的狀態。選項包括新的、暫存和修改的文件。它將檢索分支名稱,當前提交標識符和待提交的更改。我都是用這個指令來確認已經提交和未提交的檔案。git add [file]
:將文件添加到暫存區。使用代替完整文件路徑來添加所有將文件從當前目錄更改為目錄樹。如果需要將未提交的檔案一次提交可以使用git add .
。git commit -m "message"
:從添加到暫存區的更改創建新提交。git reset [file]
:將您的 repo 恢復到以前已知的工作狀態。git rm [file]
:從 project directory 和暫存區中刪除文件。git diff [file]
:顯示 project directory 和暫存區之間的變化。
檔案變更版更操作
git clean -f [file]
:清除未被追蹤的指定檔案。git clean -fd
:清除未被追蹤的所有檔案。git checkout [file]
:放棄工作目錄中的更改。此操作不可恢復。git restore [file]
:當前目錄回復前次存檔。git diff --staged [file]
:顯示暫存區和 repo 之間的任何更改。
分支操作
git branch [-a]
:列出存儲庫中的所有本地分支。 with -a: 顯示所有分支git branch [branch_name]
:創建新分支,引用當前 HEAD。git checkout [-b][branch_name]
:將工作目錄切換到指定的分支。使用 -b:如果指定的分支不存在,則創建它。git merge [from name]
:將指定的 [from name] 分支加入您當前的分支。git branch -d [name]
:刪除選定的分支,如果它已經合併到任何其他分支。-D
強制刪除。
檢查工作
git log [-n count]
:列出當前分支的提交歷史。 -n count 限制列表到最後 n
提交。git log ref..
:列出當前分支上存在但未合併的提交。ref 可以是分支名稱或標籤名稱。git log ..ref
:列出存在於 ref 上且尚未合併到當前分支的提交。git reflog
:列出對本地 repo 進行的操作(例如簽出或提交)。
同步 repo
git fetch [remote]
:從遠程獲取更改,但不更新跟踪分支。git fetch --prune [remote]
:刪除從遠端 repo 中被移除的 remote refs。git pull"
:把遠端修改的內容合併到本地。git push [remote] [branch]"
:將本地 repo 推送到遠端。
參考資料
Latest posts by pluto (see all)
- React 那些好看、有趣、實用的函式庫、組件庫推薦(2) - 2022 年 6 月 26 日
- 解決 preact 資源請求路徑錯誤的問題 - 2022 年 6 月 24 日
- [楓之谷私服] 潮流轉蛋機 NPC 腳本優化 - 2022 年 6 月 19 日