Git - 常用指令表

SSH Key

  • $ ssh-keygen
    在終端機中產生金鑰
  • $ ls -al ~/.ssh
    查詢是否有 SSH Key了(出現檔案id_rsa.pub 或 id_dsa.pub 就成功囉)
  • $ pbcopy < ~/.ssh/id_rsa.pub / $ clip < ~/.ssh/id_rsa.pub
    複製公鑰 ( Mac / Windows git bash )

config

GitzshdoRemark
git config --listgcf查看設定
git config --local user.name "(userName)"設定帳號
git config --local user.email "(e-mail)"設定E-mail全域
git config --global user.name "(userName)"設定帳號單專案
git config --global user.email "(e-mail)"設定E-mail單專案

init / clone

GitzshdoRemark
git clone抓遠端儲存庫下來
git initGit 初始化
rm -rf .git移除 Git

remote

GitzshdoRemark
git remote add (origin) (git@~.git)gra遠端連結
git remote set-url (origin) (git@~.git)修改遠端連結
git remote remove (origin)grrm移除遠端連結
git remote -vgrv查詢遠端連結(URL)
git push -u (origin) (master)推上遠端並綁定

基本版更( pull / push / add / commit / status /fetch)

GitzshdoRemark
git statusgst
git add (file)ga (~)
git add .ga .
git commit -m'message'gcmsg '(~)'
git commit --amend
git commit --amend -m'message'修改最後一次的commit
git pullgl
git push (remote) (branch)gp (~) (~)
git push -u (remote) (branch)gp -u
git restore --staged (file)取消 git add
git pull --rebase (remote) (branch)gup
git fetchgl獲取remote

檔案變更版更操作

GitzshdoRemark
git clean -fd清除未被追蹤的所有檔案已編輯的會恢復,新增的不會變動
git checkout (file)gco當前目錄回復前次存檔(已編輯的會恢復,新增的不會變動)
git restore (file)grs當前目錄回復前次存檔(含被刪除的檔)

Branch 分支

GitzshdoRemark
git branchgb查詢所有本地分支
git branch -agba查詢所有遠端分支
git branch (newBranch)當前 commit 新建分支
git branch (newBranch) (commitID)特定 commit 上新建分支
git checkout (branch)gco切換到某分支
git checkout -b (newBranch)gcb新建分支並切換過去
git branch -d (branch)刪除某分支
git branch -D (branch)強制刪除某分支
git branch -m (branch) (newName)將某 branch 更名必須先切到不同分支

Reset 還原

GitzshdoRemark
git reset (commit)還原工作目錄與索引預設為'mixed'
git reset (commit) --mixed放回"1-工作目錄"
git reset (commit) --soft放回"2-暫存區"
git reset (commit) --hard都不留(直接被隱藏)
git reset (commit)^退回前1次的commit^^ 退回前2版…
git reset (commit)~5退回前5次的commit~N 退至前N版
git reset --hard HEAD@{1}取消剛剛的Reset快進下一個commit
  • (commit)可以是 branch / commit ID / HEAD^

  • git reset --soft HEAD^:拆掉最近一次commit但保留異動內容
  • git reset --hard HEAD^:拆掉最近一次 commit
  • git reset --hard ORIG_HEAD:取消剛剛拆掉的 commit

Rebase 合併應用

GitzshdoRemark
git rebase (branch)grb重接分支基底
git rebase --continue繼續 rebase
git rebase --abort取消 rebase

Merge 合併應用

GitzshdoRemark
git merge (branch)gm合併分支(平行)
git merge --abort
git reset --merge
取消 merge

查詢

GitzshdoRemark
git config --listgcf查詢目前設定
which git查詢 Git 位置
git --version查詢Git版本
git statusgst查詢狀態
git log查詢 Log
git log --oneline查詢 Log(單行顯示)
git log --oneline --all --graph樹狀圖形檢視 Log
git log -p FileName查詢檔案 Log
git blame FileName查詢該檔案每行編輯資訊(上傳者&時間)
git reflog查詢 reflog
git helpghh查詢指令
  • reflog:reflog保留HEAD移動的軌跡,可以查詢到commit ID(用於尋找被隱藏的commit)

Tag 標籤

GitzshdoRemark
git tag查詢標籤
git tag -n查詢詳細標籤
git tag (標籤名)新增輕量標籤
git tag -am "(備註文字)" (標籤名)新增標示標籤
git tag -d (標籤名)刪除標籤
git push origin --tags將標籤推至遠端
  • (於目標分支且拉最新 git checkout mastergit pull
  • 本地打 tag : git tag 20200913
  • 推至遠端:git push origin --tags

stash 暫存

GitzshdoRemark
git stashgsta暫時儲存當前目錄
git stash save -u <名稱>暫時儲存且命名
git stash listgstl瀏覽 stash 列表
git stash apply
git stash apply stash@{0}
gstaa還原最新但不刪除
還原~但不刪除
git stash pop
git stash pop stash@{0}
gstp還原最新暫存
還原~且刪除
git stash drop
git stash drop stash@{0}
gstd清除最新暫存
清除~暫存
git stash cleargstc清除全部暫存

其他

GitzshdoRemark
git diff <commit1> <commit2>查看兩個commit的差異
git cherry-pick commit
rebase -i commit
git clean -fd將目前Untracked的檔案全部刪除
git cherry-pick -h查詢cherry-pick相關指令

參考資料

⮩ 本文同步發表在第 12 屆 iT 邦幫忙鐵人賽 《For 前端小幼苗的圖解筆記》