[CLI] Git 指令
- CS Visualized: Useful Git Commands @ dev:用圖示的方式清楚說明 git cli 不同指令所做的事
- help @ Github
[TOC]
常用指令
git checkout . # 編輯檔案後,恢復目錄到最後一次的 commit 狀態
git checkout [FileName] # 把某支修改過的檔案還原到未修改狀態 commit 後修改的檔案內容移除
git reset HEAD [filename] # 取消已經被 add 加入索引的檔案(不會改變檔案內容)
git reset HEAD~1 # 取消最後一次提交的 commit ,檔案內容回到 unstaged 狀態(已編輯的內容會被保留),等同於 reset HEAD^
git reset HEAD~1 --hard # 取消最後一次提交的 commit ,檔案內容回到 unstaged 狀態(已編輯的內容不會被保留)
git reset --hard ORIG_HEAD # 將不小心透過上行指令刪除的 commit 復原
# 將 local 的 branch 對齊回 remote 的 branch 的 commits
git fetch && git reset --hard @{u}
##
# 合併分支
##
git rebase [some-hash]^ --onto [some-branch]
git merge --squash [some-branch]
##
# git commit
##
git commit --amend # 修改當前 commit 撰寫內容,ESC :wq 可離開 amend
git commit --amend -m "[message]" # 將新的 commit 合併到上一次的 commit 中(上一次的 commit 的內容會被覆蓋)
git commit --amend --no-edit # 修改當前 commit 但不修改 commit message
git commit --no-verify # 去掉 commit 前一些 lint 的檢查
##
# 刪除遠端分支
##
git push origin :[branch-name] # ":" 表示要刪除遠端 branch 的意思
git push origin --delete [branch-name] # 同上
git fetch -p origin # 刪除 br 內不存在的參考遠端分支
# 將和該次 push 有關的 tags 一起推上去
git push --follow-tags origin main
# 建立一個新分支並拉內容下來(不會衝突)
git fetch
git checkout [branch-name]
# 將 diff 的結果貼到剪貼簿(clipboard)
git diff | pbcopy
# Delete untracked files
$ git clean -n # 列出將被刪除的檔案
$ git clean -f # 刪除沒被 git 追蹤的檔案
安裝
brew install git
指令 CLI
git diff 檢視檔案內容差異
# 檢視檔案內容差異
git diff # 檢視未加入索引的檔案和原檔案有何差異
git diff --staged # 檢視加入索引的檔案和前次提交的檔案有何差異
git diff [first-branch] [second-branch] # 檢視兩個 branch 的檔案有何差異
git show [commit-hash] # 檢視某一次 commit 有何差異
git diff | pbcopy # 把內容複製到剪貼簿,可以存成 .diff 檔就可以看到差異
檢視記錄
keywords: git log
, git reflog
, git blame
# 檢視記錄
git log # 檢視提交記錄
git log -p [FileName] # 針對某一支檔案顯示內容的變更記錄
git log --follow [FileName] # 針對某一支檔案顯示 log
git log --graph # 以圖表方式顯示分支歷史
git log --oneline # 一次 commit 僅以一行顯示重點
git reflog # 每次 HEAD 移動時就會記錄(保留 30 天),因此可以檢視最完整的編輯記錄(包含 reset)
git blame [FileName] # 顯示某一支檔案內容的完整編輯記錄
git blame -L 5,10 [FileName] # 顯示某一支檔案內容的完整編輯記錄(只顯示特定行數)
git checkout 移動 HEAD
# 移動 HEAD
git checkout [FileName] # 編輯檔案後,恢復單一檔案到最後一次的 commit 狀態
git checkout . # 編輯檔案後,恢復工作目錄到最後一次的 commit 狀態
git checkout [commit-hash] # 將 HEAD 移動到特定 commit 位置(檔案內容會改變到該次 commit 的狀態)
git checkout master # 將 HEAD 回到 master 上
git checkout HEAD^
git checkout HEAD~2
git switch 切換分支
git switch other-branch
git switch - # Switch back to previous branch, similar to "cd -"
git switch remote-branch # Directly switch to remote branch and start tracking it
git restore 還原特定檔案
# Unstage changes made to a file, same as "git reset some-file.py"
git restore --staged some-file.py
# Unstage and discard changes made to a file, same as "git checkout some-file.py"
git restore --staged --worktree some-file.py
# Revert a file to some previous commit, same as "git reset commit -- some-file.py"
git restore --source HEAD~2 some-file.py
git reset 取消或刪除某些 commit
# git reset 取消或刪除某些 commit
# 沒給參數預設是使用 --mixed
git reset # 預設 mixed,把暫存區的檔案變成 unstaged,Commit 拆出來的檔案會留在工作目錄,但不會留在暫存區。
git reset [commit-hash] # 取消前一次的 commit 但不還原改變回檔案內容
git reset HEAD # 取消所有已進入 staging/index (add) 的檔案
git reset HEAD [FileName] # 取消單一檔案的索引(staged to unstaged)
git reset --hard HEAD~1 # 刪除前一次的 commit,檔案和索引會還原到前一次 commit 時的內容
git reset --hard ORIG_HEAD # 將不小心透過上行指令刪除的 commit 復原
git config 使用者資料設定
# 使用者資料設定
git --version # 查詢 git 版本
git config --global --add user.name “pjchender” # 設定使用者名稱
git config --global --add user.email “pjchender@gmail.com” # 設定使用者信箱
git config --global —list # 列出使用者設定
git config --global color.ui auto # 讓 git 好看
git config --global core.editor sublime # 變更預設編輯器
git clone 建立本機端的資料庫
# 建立本機端的資料庫
git init # 建立新的本地端數據庫(資料夾中會多一個名為 .git 的資料夾)
git clone [remote_url] [directory_name] # 複製遠端數據庫
git 基本指令
# git 基本指令
git add . # 將工作區的所有檔案加入索引/暫存區
git add [FileName]
git add -u # mark all conflicted files as merged
git commit -m ["commit-message"] # 將暫存區中的檔案加入儲存庫,提交並寫上註記
git status # 確認工作目錄與索引狀態
git commit --amend # 修改 commit 的訊息內容,會產生新的 SHA-1
git 雲端使用
keywords: git push
, git fetch
, git pull
, git remote
# git 雲端使用
git push [repo] [branch] # 把本地端的 branch_name 推到遠端 [repo] 上
git push -u [repo] [branch] # 將該 [repo] 和 [branch] 設為預設值,以後不需要再次輸入
git push [repo] [local_branch]:[remote_branch] # 將本地端的 [local_branch] 分支推到遠端的 [remote_branch] 上
git push [repo] :[remote_branch] # 把遠端的 [remote_branch] 移除
git fetch # 將遠端的檔案拉下來,但尚未建立 merge
git pull [repo] [branch] # 將遠端的檔案拉下來,同時 merge(等同於 fetch + merge)
git pull [repo] [branch] --allow-unrelated-histories # 允許不同的歷史記錄
git pull --rebase [repo] [branch] # 將遠端的檔案拉下來,同時 rebase(不會多產生一個 merge 的 commit)
# git remote 遠端操作
git remote # 顯示所有遠端儲存庫
git remote -v # 在名稱後方顯示 URL
git remote add [short-name] [url] # 新增遠端儲存庫
git remote show [remote-name] # 監看遠端儲存庫
git remote rename [old-name] [new-name] # 更名遠端儲存庫(同時會改變分支名稱)
git remote rm [remote-name] # 移除遠端儲存庫
git branch: 分支操作
keywords: git branch
, git checkout
, git merge
, git rebase
# 分支操作
# git branch 分支指令
git branch # 顯示所有分支紀錄,-a 可以顯示詳細
git branch [branch_name] [commit-key] # 在 commit-key 上面貼上 branch_name 這個貼紙
git branch -f [branch_name] [commit-key] # 強制移動分支
git branch [branch_name] # 新增分支
git branch -d [branch_name] # 刪除分支
git branch -m [old_name] [new_name] # 重新命名分支,-m 指 mv 的意思
git branch -m [new_name] # 重新命名當前分支
git branch --merged # 檢視已經合併過的分支
git branch -u [upstream] # set up stream
# git checkout 切換 HEAD
git checkout [branch_name] # 移動到某一分支
git checkout --ours . # checkout our local version of all files
git checkout -b [branch_name] # 建立該 branch 並切過去
git checkout -b remote_branch origin/remote_branch # 建立分支並拉下來
# git merge 合併分支
git merge [branch_name] # 合併某一分支到目前的分支,會產生新的 commit 記錄
git merge --squash [branch-name]
# git rebase 合併分支 # 將目前分支合併到某一分支後,不會產生新的 commit
git rebase [branch_name]
# 刪除遠端上的分支
git push origin :[branch-name] # ":" 表示要刪除遠端 branch 的意思
git push origin --delete [branch-name] # 同上
git fetch -p origin # 刪除 br 內不存在的參考遠端分支