Skip to content

Instantly share code, notes, and snippets.

@davidhcefx
Last active June 9, 2024 16:59

Revisions

  1. davidhcefx revised this gist Jun 9, 2024. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion Git-學習筆記.md
    Original file line number Diff line number Diff line change
    @@ -107,8 +107,12 @@

    ## Concepts

    - `.git/config`: branch & remote 設定都存在這

    - `~/.gitconfig`: git config 所做的設定都存在這

    - `.gitignore`:預設是不會排除 .gitignore 本身
    + 只能排除 "untracked"modified 會被記錄。
    + 只能排除 "untracked" files, modified 會被記錄。

    - `.gitattributes`
    + export-ignore 可以在打包壓縮檔時忽略檔案
  2. davidhcefx revised this gist Jun 9, 2024. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Git-學習筆記.md
    Original file line number Diff line number Diff line change
    @@ -103,6 +103,7 @@
    ### 將目前變更放進暫存區:
    - `git stash --include-untracked`
    - `git stash pop`: 但如果忘記 pop,git commit 並不會提醒你
    - `git stash show -p`: 顯示 stash 中內容

    ## Concepts

  3. davidhcefx revised this gist May 23, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Git-學習筆記.md
    Original file line number Diff line number Diff line change
    @@ -87,7 +87,7 @@
    ### 用二分搜定位特定 commit:
    - `git bisect start`
    - `git bisect new`
    - `git bisect old <some previous commit>`
    - 切到某個舊的 commit, 然後 `git bisect old`
    - 接下來會一直切到不同 commit,只需回答 git bisect old/new。

    ### Branch 改名:
  4. davidhcefx revised this gist Feb 3, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Git-學習筆記.md
    Original file line number Diff line number Diff line change
    @@ -73,7 +73,7 @@
    ### 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 <files>, 解決後刪掉 *.rej, 然後 git am --continue
    + `git am <files>`: 套用 patch, conflict 時改用 `git apply --reject <file>`, 解決後刪掉 `*.rej``git am --continue`


    ### 濃縮成一個 commit (squash):
  5. davidhcefx revised this gist Feb 3, 2024. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion Git-學習筆記.md
    Original file line number Diff line number Diff line change
    @@ -73,7 +73,8 @@
    ### Patch
    - `git archive --format=zip HEAD > a.zip`: 打包成 zip
    - `git format-patch HEAD~ -o ~/`: 製作 patch, 相當於 HEAD~..HEAD
    + `git am <files>`: 套用 patch
    + `git am <files>`: 套用 patch, conflict 時改用 git apply --reject <files>, 解決後刪掉 *.rej, 然後 git am --continue


    ### 濃縮成一個 commit (squash):
    - `git reset --soft HEAD~3`
  6. davidhcefx revised this gist Feb 3, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Git-學習筆記.md
    Original file line number Diff line number Diff line change
    @@ -68,7 +68,7 @@

    - `git merge --no-ff <Another Branch>`: 有衝突就按照 git status 提示做
    + `git rebase <branch-B>`: 另一種 merge,會把 B 當做基底疊加 A 的 commit
    + `git cherry-pick <commit>`: 把某個 commit 套用過來 (可以 resolve conflict)
    + `git cherry-pick <commit>`: 把某個 commit 套用過來, conflict 時解決 "<<<" 再繼續

    ### Patch
    - `git archive --format=zip HEAD > a.zip`: 打包成 zip
  7. davidhcefx revised this gist Feb 3, 2024. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Git-學習筆記.md
    Original file line number Diff line number Diff line change
    @@ -68,6 +68,7 @@

    - `git merge --no-ff <Another Branch>`: 有衝突就按照 git status 提示做
    + `git rebase <branch-B>`: 另一種 merge,會把 B 當做基底疊加 A 的 commit
    + `git cherry-pick <commit>`: 把某個 commit 套用過來 (可以 resolve conflict)

    ### Patch
    - `git archive --format=zip HEAD > a.zip`: 打包成 zip
  8. davidhcefx revised this gist Jan 11, 2024. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Git-學習筆記.md
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,7 @@
    ### 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
  9. davidhcefx revised this gist Dec 19, 2023. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions Git-學習筆記.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    - `git init`: build .git folder
    - `git status`
    - `git add .`: select all files
    + `--edit`: 可以只加部分行數
    + `--patch`: 可以只加部分行數

    ### Commit
    - `git commit`: will open CLI text editor
    @@ -61,16 +61,16 @@
    + `--delete <name>`
    + `--set-upstream-to=origin/<branch> <localBranch>`: 連結現有 branch 到 remote
    * 如果遇到 *does not exist*,確認 remote.origin.fetch 是 `*` 然後再 fetch。
    - `git checkout <Another branch>`
    + `git switch <Another branch>`
    + `--track origin/<branch>`: 創 local branch 並追蹤 upstream
    - `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

    ### Patch
    - `git archive --format=zip HEAD > a.zip`: 打包成 zip
    - `git format-patch HEAD~ -o ~/`: 製作 patch, 可以寫 HEAD~..HEAD
    - `git format-patch HEAD~ -o ~/`: 製作 patch, 相當於 HEAD~..HEAD
    + `git am <files>`: 套用 patch

    ### 濃縮成一個 commit (squash):
  10. davidhcefx revised this gist Apr 19, 2023. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Git-學習筆記.md
    Original file line number Diff line number Diff line change
    @@ -35,6 +35,7 @@
    - `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 的差別。
  11. davidhcefx revised this gist Apr 19, 2023. 1 changed file with 17 additions and 17 deletions.
    34 changes: 17 additions & 17 deletions Git-學習筆記.md
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,17 @@
    # Commands
    ## Commands

    ## Basic
    ### Basic
    - `git init`: build .git folder
    - `git status`
    - `git add .`: select all files
    + `--edit`: 可以只加部分行數

    ## Commit
    ### Commit
    - `git commit`: will open CLI text editor
    + `-m "add a message without editor"`
    + `-c ORIG_HEAD`: 重用舊的訊息 (+ CLI editor)

    ## Push & Remote
    ### Push & Remote
    - `git push [<remote> <localBranch>]`
    + `--set-upstream origin main`: 順便連結 main branch 到 origin/(main)
    + `--set-upstream origin dev-2:dev`: 把本地 dev-2 推到遠端 dev
    @@ -24,14 +24,14 @@
    + `remove <name>`
    + 可以連結多個 remote (eg. 'upstream'),就可以同步 forked branch。

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

    - `git clone [--depth 1] <URL.git>`: 加上 depth 可限制 log 數
    + 相當於 fetch (抓資訊) + checkout (改檔案)
    + `--branch <name or tag>`

    ## Log & Diff
    ### Log & Diff
    - `git log [--oneline] <tree-ish> | <commit range>`
    + `HEAD~2..HEAD`: 不包含 HEAD~2
    + `-n 1`: 筆數
    @@ -41,20 +41,20 @@
    + `--cached`: 比較 staged 跟 HEAD 的差別
    - `git show <commit> [--stat]`: 顯示做了什麼

    ## Tag
    ### 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
    ### Reset
    - `git reset [--soft] HEAD~`: 還原上次 commit (不動檔案)(上次會變成 ORIG_HEAD)
    + `--hard`: 會動到檔案
    - `git restore --staged .`: 還原 index 到 HEAD
    +`--worktree` 一併還原檔案
    - `git clean -dff`: 清除 untracked files

    ## Branch & Merge
    ### Branch & Merge
    - `git branch <New branch>`
    + `-avv`: 列出所有的,連結 remote 者會有框框
    + `--delete <name>`
    @@ -67,40 +67,40 @@
    - `git merge --no-ff <Another Branch>`: 有衝突就按照 git status 提示做
    + `git rebase <branch-B>`: 另一種 merge,會把 B 當做基底疊加 A 的 commit

    ## Patch
    ### Patch
    - `git archive --format=zip HEAD > a.zip`: 打包成 zip
    - `git format-patch HEAD~ -o ~/`: 製作 patch, 可以寫 HEAD~..HEAD
    + `git am <files>`: 套用 patch

    ## 濃縮成一個 commit (squash):
    ### 濃縮成一個 commit (squash):
    - `git reset --soft HEAD~3`
    - `git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})"`

    ## Cache 帳號密碼:
    ### Cache 帳號密碼:
    - `git config --global credential.helper 'cache'`: 會存在 ~/.gitconfig
    + `'cache --timeout 10800'`: 10800=3hr, 3600=1hr

    ## 用二分搜定位特定 commit:
    ### 用二分搜定位特定 commit:
    - `git bisect start`
    - `git bisect new`
    - `git bisect old <some previous commit>`
    - 接下來會一直切到不同 commit,只需回答 git bisect old/new。

    ## Branch 改名:
    ### 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
    ## Concepts

    - `.gitignore`:預設是不會排除 .gitignore 本身
    + 只能排除 "untracked",modified 會被記錄。
  12. davidhcefx created this gist Apr 19, 2023.
    114 changes: 114 additions & 0 deletions Git-學習筆記.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,114 @@
    # Commands

    ## Basic
    - `git init`: build .git folder
    - `git status`
    - `git add .`: select all files
    + `--edit`: 可以只加部分行數

    ## Commit
    - `git commit`: will open CLI text editor
    + `-m "add a message without editor"`
    + `-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`: 筆數

    - `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 <Another branch>`
    + `git switch <Another branch>`
    + `--track origin/<branch>`: 創 local branch 並追蹤 upstream

    - `git merge --no-ff <Another Branch>`: 有衝突就按照 git status 提示做
    + `git rebase <branch-B>`: 另一種 merge,會把 B 當做基底疊加 A 的 commit

    ## Patch
    - `git archive --format=zip HEAD > a.zip`: 打包成 zip
    - `git format-patch HEAD~ -o ~/`: 製作 patch, 可以寫 HEAD~..HEAD
    + `git am <files>`: 套用 patch

    ## 濃縮成一個 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`
    - `git bisect old <some previous commit>`
    - 接下來會一直切到不同 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)