git

id 可用 HEAD 代替

HEAD^ = HEAD 的前一個(與 HEAD^1 相等)

HEAD^^ = HEAD 的前兩個,但 HEAD^2 不是 HEAD^^ ,他是 HEAD^1 的另一條路線

HEAD~1 = HEAD 的前一個 、 HEAD~2 = HEAD 的前兩個

工作區 = workspace

暫存區 = staged

某些指令在檔案路徑之前可以加入 -- ,但非必要。 因為某些特殊情況(例如檔案剛好以--開頭),可以明確告知git這個--之後出現的--filename是檔名的一部分,而不是指令

version & update

git version  # 查看 git 版號
git update  # 提升 git 版號

congif

git config --global user.name "your name"
git config --global user.email "your email"
git config --global core.editor "code --wait"

cloone

git clone <url>  # 拉取特定儲存庫到該儲存庫相同名稱的資料夾下
git clone <url> <dir-name>  # 拉取特定儲存庫,並將其存在特定資料夾下

init

git init  # 初始化一個 git

status

git status  # 查看 git 狀態

add

git add .  # 將所有改動的檔案從工作區加入暫存區
git add <files>  # 只將特定檔案加入暫存區

commit

git commit -m "initial commit"
git commit -am "add and commit"  # add 並 commit  但對新檔案無效
git commit --amend  # 開啟編輯器修改上一個commit 的訊息 或 檔案內容
git commit --amend --no-edit  # 修改上一個commit的檔案內容,但不開啟編輯器修改 commit 訊息
git commit --amend --date="YYYY-MM-DDThh:mm:ss"  # 修改上次提交日期 前面需加上類似 GIT_COMMITTER_DATE=2024-02-10T06:16:11 的訊息

log

git log  # 顯示詳細 log
git log --oneline  # 顯示單行 log
git log --graph  # 顯示 log 分支路線圖

diff

git diff  # 比較還沒 add 的檔案與工作區的差異
git diff --cached  # 比較暫存區的檔案(git add 後的)與工作區的差異
git diff <id>  # 比較特定 commit 與 HEAD 的差異
git diff <id1> <id2>  # 比較特定兩個 commit 之間的差異

tag

git tag <tag-name>  # 添加標籤到 HEAD
git tag <tag-name> <id> # 將標籤加到特定 commit
git tag <tag-name> <id> -a -m <message>  # 有附註 message 的標籤
git tag -d <tag-name>  # 刪除特定標籤

push

git push  # 如果有設定好上游分支則會自動推送
git push -f  # 強制推送覆蓋遠端儲存庫
git push origin main  # 推到指定儲存庫的特定分支
git push --set-upstream origin main  # 設定上游分支

git push origin <tag-name>  # 推送單一標籤
git push --tags  # 單純推送所有 tag
git push --follow-tags  # 推送所有已經被上標籤的 commit 和 tag
git push --all --tags  # 推送所有的 commit 和 tag

git push origin --delete <branch>  # 刪除遠端分支
git push --delete origin <tag-name>  # 刪除遠端標籤

fetch

git fetch  # 從遠端下載最新的 commit,但不會自動合併到現在的工作區內

pull

git pull  # pull = fetch + merge。 會先使用 fetch 從遠端下載最新的 commit,然後自動合併到目前的工作區內
git pull --no-ff  # 下載遠端並合併時候不要使用快轉合併
git pull --rebase  # 下載後使用 rebase 進行合併

branch

git branch  # 查看所有本地分支
git branch -vv  # 查看本地極其對應的上游分支
git branch -r  # 查看所有遠端分支
git branch <new-branch>  # 從現在分支創建出一個新分支
git branch -m <old-branch> <new-branch>  # 分支改名
git branch -f <branch> <branch/id>  # 移動分支貼紙
git branch -d <branch>  # 刪除本地分支 (已合併分支可用 -d)
git branch -D branch-to-delete  # 刪除未合併分支必須用 -D
git branch -u origin/feature-branch feature-branch # 設定上游分支

checkout

git checkout <id>  # 切換 HEAD 到某個指定地方
git checkout <id> <file>  # 將指定檔案恢復到特定 commit 時間點的狀態

# 以下不推薦使用,可以用其他指令代替
g  # 切換分支 (git switch)
git checkout -b <new-branch>  # 創建並切換分支 (git switch -c)
git checkout <file>  # 將未加入到暫存區的檔案恢復到指定到最新 commit 的檔案狀態 (git restore)

switch

git switch <branch>  # 切換分支
git switch -c <new-branch>  # 創建並切換分支

revert

git revert <id>  # 撤銷該點之後的提交並提交成一個新的 commit

reset

git reset # 取消目前所有暫存區的檔案 (git reset --mixed) (git restore --staged .)
git reset HEAD <file>  # 只重置單一檔案
git reset --soft <id>  # 重置到某個 commit,但不會將檔案移出暫存區,也不會影響工作區的檔案
git reset --hard <id>  # 重置到某個時間點的 commit (會一併取消該commit之後的提交,及工作區的檔案)

restore

git restore --staged <files>  # 將指定檔案移除 staged 範圍 (git add 的準備 commit區)
git restore <files>  # 將指定且未放到暫存區的檔案恢復修改 (恢復到已經提交的最新commit的狀態)。相當於 git checkout HEAD <files>

merge

git merge <merge-branch-name>
git add <conflicted-file>
git merge --continue
git merge --abort
git merge --no-ff
git merge --no-commit

rebase

git rebase <branch>
git rebase --abort

git rebase -i <id>  # 允許你擠壓、編輯過去commit訊息等操作

reflog

git reflog
git reset --hard ORIG_HEAD  # 危險操作,建議先使用下面指令檢查
git log ORIG_HEAD..HEAD

cherry-pick

git cherry-pick <id>  # 指合併部分 commit 內容,接到目前分支後面
git cherry-pick <id> --no-commit  # 先加入暫存區,而不是直接 commit

stash

git stash  # stash 目前工作區到 commit之間的改動
git stash pop  # 彈出最新的 stash 並刪除
git stash list  # 列出目前的 stash
git stash apply <stash@{0}> # 恢復特定 stash,但不會刪除
git stash drop <stash@{1}> # 刪除特定 stash
git stash clear  # 刪除所有 stash

remote

git remote  # 列出本地存儲庫中已經設定的所有遠端存儲庫的名字(通常是 origin)
git remote -v  # 列出遠端處存庫的詳細訊息
git remote add <remote-name> <remote-url>  # 添加遠端儲存庫
git remote remove <remote-name>  # 刪除遠端儲存庫
git remote rename <old-name> <new-name>  # 重命名遠端儲存庫
git remote set-url <remote-name> <new-url>  # 重設遠端儲存庫 URL
git remote get-url <remote-name>  # 查看特定遠端儲存庫的 URL