標籤: git

  • 將現成的 git repo 添加現成的專案資料夾

    將現成的 git repo 添加現成的專案資料夾

    這是一個 git 小技巧,其實滿常用的,但是每次要用的時候都要查一次有點小麻煩,紀錄一下。

    主要的應用場景是在我們在伺服器/本地已經有一個現成的資料夾,要把他添加至一個遠端新創的 git repo中,但是因為環境設定原因,我不想要移動資料夾的路徑。

    要注意的是:

    1. 如果是用 Github 創建,預設都不要選擇 .gitignore 等檔案,會出現一個教學可添加,照著作即可。讚!
    2. 確保目前伺服器/本地可以 push/clone/pull git 伺服器(廢話),推薦使用 ssh 連線,參考這裡
    3. 這是使用的流程以空的 git repo 為主,雖然不影響,但是還是建議拿空的 repo 比較不容易有衝突。
    4. 資料夾中會生成一的 .git 資料夾,有些資料夾是從別人那拉下來的。請先把這個資料夾刪除。也要記得先檢查 .gitignore 是不是你要的。

    流程上:

    1. 記得先安裝好 git 套件。進入到目標資料夾,確保沒有存在其他 .git/ ,以及 .gitignore 設定正確。
    2. 建立一個新的 git
      git init

    3. 添加檔案至 git,並且 commit
      git add .; git commit -m "initial commit"
    4. 注意遠端的 branch 是什麼,在 Github 上通常是 master
      git branch -M master
    5. 添加到遠端 git。 <remote-repo-url> 要替換成 git repo
      git remote add origin <remote-repo-url>

    6. 檢查網址
      git remote -v

    7. git push 資料到遠端 repo
      git push -f origin master

    提醒一下,現在有些 Git 平台使用 main 作為預設的主要 branch。

    以上。

    參考:

  • Git Push 太久解決方法

    Git Push 太久解決方法

    Git 有時 push 太久一直無法結束,可以參考以下原因:

    1. Git Server 限制,不同 git 供應商會有不同限制,建議參考看看有沒有限制檔案大小、數量等。
    2. buffer 問題,參考以下配置指令:
      1. git config –global http.postBuffer 524288000

    以下是 Bitbucket 的限制與解決方法,請參考

  • vue3 學習筆記 EP3

    直譯語言和編譯語言是程式語言的一種分類,自己在有 server-side 的程式中寫 PHP 寫久了,都快忘記編譯語言會有編輯檔案以及編譯後的檔案。 這時會開始苦惱幾個需求:

    1. 在 server 上只 pull 下編譯後檔案即可
    2. 編輯檔還是需要版控
    3. server 上儘量不要搞太多東西,要不以後架設會很麻煩
    4. 儘量自動化

    版控使用 git 來實現,因為部屬到 server 是 master branch,所以打算開一個 dev branch 來處理編輯,然後測試沒問題以後再 marge 到 master branch ,然後 npm run build 把產生的 dist 的資料夾(編譯後的檔案) push 上去。

    參考:

    HowTo have specific .gitignore for each git branch
    HowTo have specific .gitignore for each git branch – gitignore_per_git_branch.md

    可以透過 git 之中的 hook 達到目的。

    他的原理是透過 git hook 在 checkout 的時候,取得對應不同 branch 的 .gitignore.{branch name} 檔案,讀取裡面的設定實現 git ignore 的功能,以達到不同 branch 能夠指定不同需要 ignore 檔案的目的。

    主要的作法是在 .git/hooks 之中建立一個 post-checkout 檔案(該檔案名稱對應 hook 行為):

    #!/bin/sh
    
    old_ref=$1
    new_ref=$2
    branch_switched=$3
    
    if [[ $branch_switched != '1' ]]
    then
        exit 0
    fi
    echo "---- POST CHECKOUT ----"
    current_branch=$(git rev-parse --abbrev-ref HEAD)
    hook_dir=$(dirname $0)
    root_dir="$(pwd -P)"
    info_dir="$root_dir/.git/info"
    
    exclude_target='.gitignore'
    if [[ -f "$root_dir/$exclude_target.$current_branch" ]]
    then
    	echo "Prepare to use .gitignore.$current_branch as exclude file"
        exclude_target=.gitignore.$current_branch
    fi
    cd "$info_dir"
    rm exclude
    #ln -s $exclude_target exclude
    echo "Copy .gitignore.$current_branch file in place of exclude"
    cp "$root_dir/$exclude_target" exclude
    echo "--- POST CHECKOUT END ---"

    然後執行 git init ,重新啟用 git hooks。

    之後實作 .gitignore 時,檔案名稱依照分支加上後綴即可。例如是 dev 分支下,添加 .gitignore.dev 檔案即可生效;在 master 中,添加 .gitignore.master 就行啦。

    附上設定截圖更清楚:

     

    同場加映: