# Git 疑難雜症對應手冊 ## 透過 SSH key push/pull Github 1. 查看 SSH key 是否已存在 ```c++= ls ~/.ssh/id_rsa.pub ``` 如果沒有顯示該檔案, 則需要先產生 SSH key. ```c++= ssh-keygen -t rsa -b 4096 -C "your_email@example.com" ``` 按 Enter 使用預設儲存路徑,接著設定密碼(可跳過)。 2. 複製 SSH key 執行以下命令來手動複製 SSH 公鑰內容到剪貼簿。 ```c++= cat ~/.ssh/id_rsa.pub ``` 3. 將 SSH Key 加入到 GitHub/GitLab - 登入 GitHub 或 GitLab。 - 到「Settings」>「SSH and GPG keys」。 - 點擊「New SSH key」,將複製的金鑰貼上並儲存。  ## 忽視檔案權限變動 預設情況下, git 會追蹤檔案權限, 我們只關心檔案內容變動的話, 可以輸入以下指令: ```shell= # 適用於當下儲存庫 git config core.fileMode false # 適用於全部儲存庫 git config --global core.fileMode false ``` 檢視方式: ```shell= git config --get core.fileMode git config --global --get core.fileMode ``` ## 處理 Windows & Linux 換行符號 提供給 Linux 開發者, 如果專案皆在於 Linux 上開發請遵循以下設定。 (Windows 換行符是 ```CRLF```; Linux 換行符則是 ```LF```) - 最佳處理方式 : 事前配置好 - Linux 平台上設定 ```shell= # 提交時,將 CRLF 轉成 LF ; 檢出時, LF. git config --global core.autocrlf input ``` - Windows 則設定 ```shell= # 提交時,將 CRLF 轉成 LF ; 檢出時, LF 轉成 CRLF. git config --global core.autocrlf true ``` - 透過 ```dos2unix``` 工具 : 將 ```CRLF``` 轉成 ```LF```. ```shell= dos2unix haha.txt ``` ## Git submodules 在開發過程中,專案隨著時間變得越來越肥,不時還生出子專案,此時就會遇到需要各專案共用一些 Code 的部分,如果共用的部分是用 ```copy&paste``` 的方式去同步,那勢必一定會造成兩邊不同步,維護困難。 ### 新增 Submodule 1. 安裝 Submodule 安裝函式庫```<lib-name>```於```3rdparty/<lib-name>```位置,此時 git 新增```.gitmodules```和函式庫。 ```shell= $ git submodule add <lib-url> 3rdparty/<lib-name> ``` 2. 初始化以及更新 Submodule ```shell= $ git submodule init $ git submodule update ``` 3. Commit ```shell= $ git commit -am "Added <lib-name> as a submodule" ``` ### 安裝有Submodule的專案之準備步驟 ```shell= git clone <main-repo-url> git submodule update --init --recursive ``` ## 安裝手冊 1. Meld 作為 git diff tool ```shell= sudo apt-get install meld git config --global diff.external meld ``` ## [行動版本 Git Server : USB](https://hackmd.io/@hbdoy/BJz0V5tv8#Git-Bare-Repo) 1. [Initialize remote repositiry for USB](https://medium.com/@nickhuang_1199/git-init-%E5%92%8C-git-init-bare%E7%9A%84%E5%B7%AE%E7%95%B0-6ab14eb14f87) ```shell= $ git init --bare # For remote repositiry ``` 2. Add USB repository to remote repository ```shell= $ git remote add usb /media/user/MyUSB/usb/ #(USB中.git的路径) ``` 3. Push and Pull - Push (Update USB, remote endpoint) ```shell= $ git push usb master ``` - Pull (Update PC, local endpoint) ```shell= $ git pull usb master ``` ## Git bash for W10 + rsync - from Git Bash (run as admin) ```shell= $ mkdir tmp && cd tmp ``` - install zstd unpacker for tar ```shell= curl -L https://github.com/facebook/zstd/releases/download/v1.5.5/zstd-v1.5.5-win64.zip --output xxx unzip xxx cp zstd-v1.5.5-win64/zstd.exe 'c:\Program Files\Git\usr\bin\' rm -r * .* ``` - install rsync ```shell= curl -L https://repo.msys2.org/msys/x86_64/rsync-3.2.7-2-x86_64.pkg.tar.zst --output xxx tar -I zstd -xvf xxx cp usr/bin/rsync.exe 'c:\Program Files\Git\usr\bin\' rm -r * .* curl -L https://repo.msys2.org/msys/x86_64/libzstd-1.5.5-1-x86_64.pkg.tar.zst --output xxx tar -I zstd -xvf xxx cp usr/bin/msys-zstd-1.dll 'c:\Program Files\Git\usr\bin\' rm -r * .* curl -L https://repo.msys2.org/msys/x86_64/libxxhash-0.8.1-1-x86_64.pkg.tar.zst --output xxx tar -I zstd -xvf xxx cp usr/bin/msys-xxhash-0.dll 'c:\Program Files\Git\usr\bin\' rm -r * .* curl -L https://repo.msys2.org/msys/x86_64/liblz4-1.9.4-1-x86_64.pkg.tar.zst --output xxx tar -I zstd -xvf xxx cp usr/bin/msys-lz4-1.dll 'c:\Program Files\Git\usr\bin\' curl -L https://repo.msys2.org/msys/x86_64/libopenssl-3.1.1-1-x86_64.pkg.tar.zst --output xxx tar -I zstd -xvf xxx cp usr/bin/msys-crypto-3.dll 'c:\Program Files\Git\usr\bin\' ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up