Skip to content

Instantly share code, notes, and snippets.

@davidhcefx
Last active May 23, 2024 11:24
Show Gist options
  • Save davidhcefx/63b6f11442386110f1cf42b7d4ef3a54 to your computer and use it in GitHub Desktop.
Save davidhcefx/63b6f11442386110f1cf42b7d4ef3a54 to your computer and use it in GitHub Desktop.
My Git Learning Notes

Commands

Basic

  • git init: build .git folder
  • git status
  • git add .: select all files
    • --patch: 可以只加部分行數

Commit

  • 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)

Push & Remote

  • 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。

Pull & Clone

  • git pull [<remote> <branch>]: 預設會保留本地 & 遠端更新,除非無法相容

  • git clone [--depth 1] <URL.git>: 加上 depth 可限制 log 數

    • 相當於 fetch (抓資訊) + checkout (改檔案)
    • --branch <name or tag>

Log & Diff

  • 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]: 顯示做了什麼

Tag

  • git tag <Tag Name> <commit id>: 加一個 lightweight tag
    • -a: 加正式的 annotated tag (PGP, message, date)
    • -d <Tag Name>: 移除 tag
  • git describe --tags --abbrev=0: 顯示最接近的 tag

Reset

  • git reset [--soft] HEAD~: 還原上次 commit (不動檔案)(上次會變成 ORIG_HEAD)
    • --hard: 會動到檔案
  • git restore --staged .: 還原 index 到 HEAD
    • --worktree 一併還原檔案
  • git clean -dff: 清除 untracked files

Branch & Merge

  • git branch <New branch>

    • -avv: 列出所有的,連結 remote 者會有框框
    • --delete <name>
    • --set-upstream-to=origin/<branch> <localBranch>: 連結現有 branch 到 remote
      • 如果遇到 does not exist,確認 remote.origin.fetch 是 * 然後再 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 的 commit
    • git cherry-pick <commit>: 把某個 commit 套用過來, conflict 時解決 "<<<" 再繼續

Patch

  • git archive --format=zip HEAD > a.zip: 打包成 zip
  • git format-patch HEAD~ -o ~/: 製作 patch, 相當於 HEAD~..HEAD
    • git am <files>: 套用 patch, conflict 時改用 git apply --reject <file>, 解決後刪掉 *.rejgit am --continue

濃縮成一個 commit (squash):

  • git reset --soft HEAD~3
  • git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})"

Cache 帳號密碼:

  • git config --global credential.helper 'cache': 會存在 ~/.gitconfig
    • 'cache --timeout 10800': 10800=3hr, 3600=1hr

用二分搜定位特定 commit:

  • git bisect start
  • git bisect new
  • 切到某個舊的 commit, 然後 git bisect old
  • 接下來會一直切到不同 commit,只需回答 git bisect old/new。

Branch 改名:

  • git branch --move OLD NEW
  • git fetch origin
  • git branch --set-upstream-to origin/NEW NEW: 連結 NEW 到遠端 NEW
  • git remote set-head origin -a

看檔案:

  • git ls-tree <commit-id> | <tree-id>: 看目錄
  • git cat-file blob <blob-id>: 印出檔案

將目前變更放進暫存區:

  • git stash --include-untracked
  • git stash pop: 但如果忘記 pop,git commit 並不會提醒你

Concepts

  • .gitignore:預設是不會排除 .gitignore 本身

    • 只能排除 "untracked",modified 會被記錄。
  • .gitattributes

    • export-ignore 可以在打包壓縮檔時忽略檔案
  • tree-ish:

    • HEAD~: HEAD 沿著 main branch 的 parent (用 git log --graph 看主/支線)
      • HEAD~n: 往前 n 個 parent。
    • HEAD^1: HEAD 的第一個 parent (merge 會有多個 parent)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment