# 簡介 因為我一直忘記 Git 的指令,所以乾脆做一個常用指令統整,不定期更新,看我遇到啥就更新啥。 好用網站推推:[Learn Git Branching](https://learngitbranching.js.org/?locale=zh_TW) ![image](https://hackmd.io/_uploads/r1j5GCiPR.png) # Git 指令 ## Repo 相關 - `git clone <url>`:複製 repo 到 local - `-b <branch>` or `--brach <branch>`:指定分支 - `git init`:在當前位置初始化一個 git repo ## 本地相關 - `git add <file>`:將修改加入本地暫存,`.` 為所有檔案的所有修改 - `--patch` or `-p`:Partial add a file to staged area, check [here](https://stackoverflow.com/questions/1085162/commit-only-part-of-a-files-changes-in-git) for how to do that - y/n/q/s/?: yes/no/quit/split/help - `git rm <file>`:刪除檔案 - `git mv <from> <to>`:移動檔案 - `git restore <file>`:回復檔案變更到未修改前 - `--staged `:從本地 staging area 移除,但保留修改(較常用) - `git commit`:將加入暫存的修改加入一個新的 commit - `-a`:會先進行 `git add .`,但不適用於新檔案 - `-m <message>`:commit message - `--amend`: edit last commit message, can be used together with `-m <message>` - `git clean`:清除 untracked files - `--dry-run` or `-n`:Dry run before remove - `--force` or `-f`:Need to add this to force remove - `git stash`:將現有本地修改加入暫存 - `list`:列出現有暫存 - `pop`:將暫存回覆到本地修改(執行在 pull 之後) - `stash@{id}`:第幾筆 stash - `clear`:清除所有 stash - `--include-untracked` or `-u`:包含 untracked files ## 遠端相關 - `git push`:將本地 commit 推到遠端 - `--set-upstream` or `-u` + `<remote name> <branch name>`:推到遠端的同時,將本地 branch 的追蹤對象設為指定的 remote branch - `<remote name> <branch name>` is short for `<remote name> <local branch>:<remote branch>` - [可以修改 git config 自動推到同名遠端 branch](https://stackoverflow.com/questions/29422101/automatically-track-remote-branch-with-git) - `-d`:[可以刪除遠端分支](https://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-locally-and-remotely) - [可以直接放新的 branch name](https://stackoverflow.com/questions/1519006/how-do-i-create-a-remote-git-branch) - `git fetch`:將遠端更新拉到本地,但不更新現有本地檔案 - `origin <branch>:<branch>`:更新指定 local branch - `git pull`:`git fetch` + `git merge` - ` --rebase`:使用 rebase 的方法 pull,用以解決 conflict ## 檢查狀態 - `git status`:檢查現在 git 與修改狀態 - `git log`:檢查 commit history - `--oneline`:每個 commit 僅有一行 - `git reflog`:參考日誌 - `git remote`:檢查遠端 - `-v`:列出網址 - `git help <command>`:解釋 git command 怎麼使用 ## Branch 相關 - `git branch`:檢查所有現在 branch - `-v`: Verbose - `-vv`: Double Verbose - `<new_branch_name>`:新增一個 branch - `-d <branch_name>`:刪除 branch - `git checkout` - `<branch>`:切換到指定 branch,亦可用於新增+切換(須加`-b`) - `<commit>`:移動本地 HEAD 的位置 - `-f`:強制移動 - `git switch` - `-c <branch>`:與 `git checkout` 相同 - [Can be used to switch to a existing remote branch](https://stackoverflow.com/questions/1783405/how-do-i-check-out-a-remote-git-branch) - `git merge <branch>`:將該 branch 合併到目前 branch 裡面,並會增加一個新的 commit 紀錄 - 記得先檢查該 branch 的 local 情況! - `git rebase <branch>`:將目前 branch 重新以該指定 branch 作為新的 base,兩者之間的差異會被補在後面,完成後兩 branch 會在同一條線上 - `-i`:Interactive mode - `--abort`:遇到衝突時,放棄 rebase,回覆到之前的狀態 - `git cherry-pick <commit>`:將選擇的 commit 加到現在 branch,不一定可以直接用 - `git diff <commit1> <commit2>`:比較兩個 commit 的不同之處 - `--name-only`:僅顯示檔案名稱 - `--stat`:顯示行數跟 + - 的比例 - `--numstat`:顯示 + - 的行數 ## 切換狀態 - `git reset <commit>`:移動 branch 到指定 commit,可用 `HEAD^` or `HEAD~3` 等方法來表示 - `--mixed`:移動但檔案變更都會留存到 working directory(預設) - `--soft`:移動但檔案變更都會留存到 staging area - `--hard`:移動但檔案變更都不會留存 ![image](https://hackmd.io/_uploads/BJ19XAowC.png) - `git revert <commit>`:退掉指定 commit,但是是使用新 commit 來抵消 ## PR 相關 - 不能 partially create a PR for merge - 直接推到同個 branch 就可以更新 PR - 如果前一個 PR 還沒 merge,但有新的 commit 要開新 PR 時,可以開一個新 branch 並把新 PR 的 merge destination 設為原本 PR 的那個 branch