git repository搬家

若有需要將整個 repository 從 A 搬到 B ,單純的 git clone 跟 git push 是不夠的,因為還有 branches, tags 等等的。

所以得這樣作。

首先在目的端,例如 github/gitlab ,建立一個空的 repository。

接著要處理來源端的 repository,在 clone 時加上 --mirror

git clone --mirror git@example.com:repo1.git

git clone 完成會有一個 repo1 的資料夾,進入 repo1 的資料夾,執行以下指令把所有 branches, tags 資訊都拉取回來,然後加入新的遠端,並進行推送

git fetch --all
git remote add target git@<新的repository>
git push target --mirror

這樣就大功告成了。

參考資料

還原git簽出檔案的修改時間

執行 git clone 以後取得的修改時間是當下執行的時間,所以其實是沒辦法去判斷哪個檔案是七天前修改的,或是十天前修改的。

Stackoverflow 上有人問了相同的問題:Git clone changes file modification time – Stack Overflow

有人就回答了,其實 git 裡面沒有保留檔案修改時間的資訊,但可以有個 tricky 的方法,就是透過 git log 的資訊來變更檔案的修改日期:

git ls-tree -r --name-only HEAD | while read filename; do
  unixtime=$(git log -1 --format="%at" -- "${filename}")
  touchtime=$(date -d @$unixtime +'%Y%m%d%H%M.%S')
  touch -t ${touchtime} "${filename}"
done

這邊主要用到幾個指令,說明如下

  • git ls-tree 是取出檔案名稱
  • git log -1 --format 是取出檔案提交的日期,這邊取出的是 timestamp 值
  • date -d 將 timestamp 值,轉換為日期時間格式。
  • touch -t 依據指定的日期格式去設定檔案的修改日期。

這邊要注意,目錄的修改時間不會被更動到。

透過以上的腳本,就可以還原git簽出檔案的修改時間,然後再搭配 find 指令來找出七天前修改的檔案了。

git-delta

git-delta 是帶有語法高亮的比對工具。

你知道的,git 預設是使用 diff,diff 沒有語法高亮,而且在比對的顯示需要一點時間習慣。

git-delta :delta

安裝

安裝蠻簡單的,提供各種作業系統的安裝方法:Installation – delta

以 Debian/Ubuntu 來說,從下載網頁下載 debian package以後,用 dpkg -i 安裝就可以。

wget https://github.com/dandavison/delta/releases/download/0.15.1/git-delta_0.15.1_amd64.deb
sudo dpkg -i git-delta_0.15.1_amd64.deb

設定

編輯 ~/.gitconfig ,加上以下設定

[core]
    pager = delta

[interactive]
    diffFilter = delta --color-only --features=interactive

[delta]
    features = decorations

[delta "interactive"]
    keep-plus-minus-markers = false

[delta "decorations"]
    commit-decoration-style = blue ol
    commit-style = raw
    file-style = omit
    hunk-header-decoration-style = blue box
    hunk-header-file-style = red
    hunk-header-line-number-style = "#067a00"
    hunk-header-style = file line-number syntax

使用

設定加好以後,在使用以下 git 指令時,就會看到比對了

  • git diff
  • git show
  • git log -p
  • git stash show -p
  • git reflog -p
  • git add -p

也可以直接拿來替代 diff 使用

delta a.txt b.txt

其他還有很多設定可以調整,可以依照自己的需求來調整:Usage – delta

結語

這種可以提升開發速度的工具,要列到建立開發環境的步驟裡,這樣在開發上可以提升效率。

git config 的 includeIf

在推特上看到 @WanCW 的推文,才知道 git config 有 includeIf ,所以來研究一下怎麼用。
先看 git config 的說明:https://git-scm.com/docs/git-config ,用法蠻簡單的。

這邊先做假設情境

  • 使用者家目錄是 /home/user ,也就是 HOME=/home/user
  • $HOME/CompanyProjects 是公司專案目錄,而公司的 git server 是 gitlab.com
  • $HOME/SideProjects 是自己的專案目錄,自己的 git server 也是 gitlab.com
  • 公司用的 ssh key 在 $HOME/.ssh/company.pem
  • 自己用的 ssh key 在 $HOME/.ssh/id_rsa

為了避免使用者名稱、email 跟 SSH key 混用,這時候 git config 該怎麼設定呢?

首先在 $HOME 下建立 .gitconfig.d 目錄,在裡面放入 company.inc 與 side.inc,這兩個檔案的內容,在後面會提到。

先編輯 $HOME/.gitconfig 

; include if $GIT_DIR is under /home/user/CompanyProjects/
[includeIf “gitdir:/home/user/CompanyProjects/"]
    path = /home/user/.gitconfig.d/company.inc

; include if $GIT_DIR is under /home/user/SideProjects/
[includeIf “gitdir:/home/user/SideProjects/"]
    path = /home/user/.gitconfig.d/side.inc

再來是 company.inc 與 side.inc

; $HOME/.gitconfig.d/company.inc
[user]
    email = user@company.com
    name = user(company)

[core]
    sshcommand=ssh -i /home/user/.ssh/company.pem
; $HOME/.gitconfig.d/side.inc
[user]
    email = user@example.com
    name = user


在這樣設定之後,當切換到 $HOME/CompanyProjects 目錄下任一個專案目錄時,git 會使用 company.inc 裡的設定;切換到 $HOME/SideProjects 目錄下任一個專案目錄時,git 會使用 side.inc 裡的設定。

那也可以針對個別專案去設定,假設 $HOME/CompanyProjects/ProjectA 目錄會用到不一樣的,那就在 .gitconfig 加入

[includeIf “gitdir:~/CompanyProjects/ProjectA/“]
    path = ~/.gitconfig.d/projecta.inc

然後在 $HOME/.gitconfig.d 下新增 projecta.inc 即可。

bfg

因為 git repository 裡有敏感資訊,為了安全起見,必須要把這些敏感資訊移掉。
找到這兩篇:

裡面大同小異,主要提到兩個方法,一個是 git filter-branch,一個是 bfg,這邊只介紹 bfg。 bfg 是一個相對於 git filter-branch 來說簡單、快速的工具,除了可以清理敏感資訊之外,也可以清掉超級大的檔案。

MacOS 的安裝可以用 brew:brew install bfg

使用步驟如下:

  1. 移除掉你要處理的檔案或是先清理掉你要處理的檔案內容,然後提交、推送到 repository 去。這段官方有特別說明 (在 Your current files are sacred… 這個小節),我是已經做完下面步驟,上 gitlab 網頁看檔案怎麼沒消失,花了點時間才在網頁上注意到這段,所以這個步驟要先作!!
  2. 切到另外一個目錄,例如 $HOME/tmp,對 repository 作 mirror,這樣拿下來的檔案結構不是一般的檔案佈局,而是 .git 下的佈局:git clone –mirror your_repository.git ,這時候 $HOME/tmp 下會有一個 your_repository.git
  3. 幾種使用方法:
    • 刪除檔案:bfg –delete-files your_file your_repository.git
    • 移除超過指定大小的檔案:bfg –strip-blobs-bigger-than 50M your_repository.git
    • 清理敏感資訊:先新增一個 passwords.txt,裡面放置要移除的文字,範例可以參考這個 gist ,然後執行 bfg –replace-text passwords.txt your_repository.txt
    • 刪除資料夾:bfg –delete-folders .git –delete-files .git –no-blob-protection your_repository.git
  4. 切到 your_repository.git 下,依照指示執行 git reflog expire –expire=now –all && git gc –prune=now –aggressive
  5. 推送回去:git push
  6. 大功告成

修改 git commit 的 author name

因為 commit 時的作者身份錯亂,所以要改掉。在 StackOverflow 查到這篇:How to change the commit author for one specific commit?

  1. 先 git rebase -i ,然後把 pick 全改成 edit
  2. 接著,git 會切到第一個 commit 結束的時間點
  3. 輸入 git commit –amend —author=”Author Name <email@address.com>” –no-edit ,這樣就會把這次 commit 的 author 修改為 Author Name <email@address.com> 。對,別忘了,這邊要改成你自己的名字跟 e-mail 。
  4. 再輸入 git rebase –continue 切到下個 commit
  5. 就這樣依序作業直到結束。

很麻煩,如果有上千個 commit 的話….(眼神死),所以以後還是要把 author 設定好,否則又要再苦一次。

如何寫 Git commit message

主要是看 How to Write a Git Commit Message 這篇文章的紀錄,這篇文章是 2014 年的文,我可能有看過,印象中之前有看過介紹原則跟工具的文章。之前的印象,第一行要明確、簡單,不要太長。有細項,從第三行寫起,用 markdown 語法。

這篇一開始先講為什麼,作者不囉唆,直接要你看自己的 git log –oneline 輸出,看自己覺得 OK 不 OK。所以可讀性很重要,可以很快的看懂,那麼就可以不用費心再去看裏面改了什麼。

主要有7個原則:

  1. Separate subject from body with a blank line :第一行寫主旨,第三行開始寫內容。
  2. Limit the subject line to 50 characters :主旨不要超過 50 個字元
  3. Capitalize the subject line :主旨的第一個字元大寫
  4. Do not end the subject line with a period :主旨不要有句點
  5. Use the imperative mood in the subject line :主旨要用祈使句,就動詞+名詞
  6. Wrap the body at 72 characters :內容的每行不要超過 72 的字元
  7. Use the body to explain what and why vs. how :內容要儘可能描述做了什麼、為什麼這樣做跟如何做。

找 git repository commits 裡的特定字串

因為在目前的程式碼裡找不到特定的字串,但是之前應該有,可能被移掉了,所以想找找之前提交的程式碼有沒有特定的字串。

請 Google 大神幫忙了一下,在 StackOverflow 上找到答案:How to grep (search) committed code in the git history? – Stack Overflow ,原來用 git grep 搭配 git rev-list 就可以做到:

# 找全部
git grep <regexp> $(git rev-list --all)

# 只找特定目錄下的
git grep <regexp> $(git rev-list --all -- lib/util) -- lib/util

用指定的 SSH key 來操作 git

一般來說,都是直接使用 $HOME/.ssh 下這把預設產生的 id_rsa/id_rsa.pub 。那如果要使用另外的 SSH key 該怎麼辦呢?

這時候可以使用 GIT_SSH 這個環境變數搭配自訂的腳本來做:

  1. 先新增一個 custom_ssh.sh:
    #!/bin/sh
    exec /usr/bin/ssh -o StrictHostKeyChecking=no -i /home/me/my_private_key "$@"
    
  2. 在使用 git 的時候,提供 GIT_SSH 環境變數
    GIT_SSH="custom_ssh.sh" git clone your_repository

    ,這樣就可以了。

在什麼情況會用到額外的 SSH key 呢? 最常見的例子就是佈署:在 gitlab/github 裡可以有所謂的 deploy key,這樣在佈署的時候,就可以不提供自己的 SSH key 來做佈署,只透過這把 deploy key 來取得原始碼,增加安全性。

誰有用到 GIT_SSH ?

  1. Jenkins 裡可以新增 SSH key ,讓你在存取 repository 時,使用這把 SSH key。實際上 Jenkins 內部也是利用 GIT_SSH 的方法在運作,但並不是全部。
  2. Ansible 的 git module 也使用了 GIT_SSH 這個技巧 (應該是)。

也可以使用 ssh-add 搭配 ssh-agent 來替代 GIT_SSH 的作法,這樣就可以不需要額外新增一個自訂的腳本了。

產生獨立 SSH Key 的方法:

ssh-keygen -C your_comment -b 4096 -m pem -f key_filename -q -P "" -N "" 

執行上面的指令後,會產生兩個檔案,一個是公鑰,一個是私鑰。在要複製到別的地方使用時,要注意 permission 必須是 0600,僅允許擁有者存取才行。

參考自:Git clone with custom SSH using GIT_SSH error – Stack Overflow

git clone fail

遇到下面這錯誤~

$ git clone http://your_host/your_group/your_project.git
Cloning into 'your_project'...
remote: Counting objects: 426, done.
remote: Compressing objects: 100% (375/375), done.
fatal: The remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed

過程大致就如 gitlab – error: git-upload-pack died of signal 13 – Stack Overflow

所以我試過 git config –global http.postBuffer 524288000 ,也試著加過 GIT_CURL_VERBOSE ,但都看不出什麼端倪,伺服器上的 log 也沒看到,最後就如 gitlab – error: git-upload-pack died of signal 13 – Stack Overflow 的解答所說,是 permission 問題。我猜可能是我有調整 nginx user 的關係,導致錯誤。後來就是依據 log 裡的提示,調整 /var/run/nginx/proxy 下資料夾的 owner 就解決問題了。