git init: build .git foldergit statusgit add .: select all files--patch: 可以只加部分行數
git commit: will open CLI text editor-m "add a message without editor"--amend: 編輯 HEAD commit msg, 並 merge staged files-c ORIG_HEAD: 重用舊的訊息 (+ CLI editor)
-
git push [<remote> <localBranch>]--set-upstream origin main: 順便連結 main branch 到 origin/(main)--set-upstream origin dev-2:dev: 把本地 dev-2 推到遠端 dev
-
git push origin <Tag Name>: 上傳 tag -
git push origin :refs/tags/<Tag Name>: 移除遠端 tag (相當 push nothing 到遠端) -
git push origin --delete <branch> -
git remote add origin <URL.git>: 新增 remote 連結-v: 顯示已新增連結remove <name>- 可以連結多個 remote (eg. 'upstream'),就可以同步 forked branch。
-
git pull [<remote> <branch>]: 預設會保留本地 & 遠端更新,除非無法相容 -
git clone [--depth 1] <URL.git>: 加上 depth 可限制 log 數- 相當於 fetch (抓資訊) + checkout (改檔案)
--branch <name or tag>
-
git log [--oneline] <tree-ish> | <commit range>HEAD~2..HEAD: 不包含 HEAD~2-n 1: 筆數--graph --decorate: 視覺上更漂亮
-
git diff [<commit-1> <commit-2> | HEAD 'path/to/file' | <blob-id-1> <blob-2>]- 如果都不加:預設是比較 unstaged 跟 staged 的差別。
--cached: 比較 staged 跟 HEAD 的差別
-
git show <commit> [--stat]: 顯示做了什麼
git tag <Tag Name> <commit id>: 加一個 lightweight tag-a: 加正式的 annotated tag (PGP, message, date)-d <Tag Name>: 移除 tag
git describe --tags --abbrev=0: 顯示最接近的 tag
git reset [--soft] HEAD~: 還原上次 commit (不動檔案)(上次會變成 ORIG_HEAD)--hard: 會動到檔案
git restore --staged .: 還原 index 到 HEAD- 加
--worktree一併還原檔案
- 加
git clean -dff: 清除 untracked files
-
git branch <New branch>-avv: 列出所有的,連結 remote 者會有框框--delete <name>--set-upstream-to=origin/<branch> <localBranch>: 連結現有 branch 到 remote- 如果遇到 does not exist,確認 remote.origin.fetch 是
*然後再 fetch。
- 如果遇到 does not exist,確認 remote.origin.fetch 是
-
git checkout <otherBranch | commit>: 用 ORIG_HEAD 返回上一動git switch <otherBranch>git checkout --track origin/<branch>: 創 local branch 並追蹤 upstream
-
git merge --no-ff <Another Branch>: 有衝突就按照 git status 提示做git rebase <branch-B>: 另一種 merge,會把 B 當做基底疊加 A 的 commitgit cherry-pick <commit>: 把某個 commit 套用過來, conflict 時解決 "<<<" 再繼續
git archive --format=zip HEAD > a.zip: 打包成 zipgit format-patch HEAD~ -o ~/: 製作 patch, 相當於 HEAD~..HEADgit am <files>: 套用 patch, conflict 時改用git apply --reject <file>, 解決後刪掉*.rej再git am --continue
git reset --soft HEAD~3git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})"
git config --global credential.helper 'cache': 會存在 ~/.gitconfig'cache --timeout 10800': 10800=3hr, 3600=1hr
git bisect startgit bisect new- 切到某個舊的 commit, 然後
git bisect old - 接下來會一直切到不同 commit,只需回答 git bisect old/new。
git branch --move OLD NEWgit fetch origingit branch --set-upstream-to origin/NEW NEW: 連結 NEW 到遠端 NEWgit remote set-head origin -a
git ls-tree <commit-id> | <tree-id>: 看目錄git cat-file blob <blob-id>: 印出檔案
git stash --include-untrackedgit stash pop: 但如果忘記 pop,git commit 並不會提醒你git stash show -p: 顯示 stash 中內容
-
.git/config: branch & remote 設定都存在這 -
~/.gitconfig: git config 所做的設定都存在這 -
.gitignore:預設是不會排除 .gitignore 本身- 只能排除 "untracked" files, modified 會被記錄。
-
.gitattributes:- export-ignore 可以在打包壓縮檔時忽略檔案
-
tree-ish:
HEAD~: HEAD 沿著 main branch 的 parent (用git log --graph看主/支線)HEAD~n: 往前 n 個 parent。
HEAD^1: HEAD 的第一個 parent (merge 會有多個 parent)