git archive
git
用法
git archive --format=tar.gz -o <file> <tree-ish>把某個提交時的檔案匯出成 tar 或 zip;不會帶上 .git 和未追蹤檔案,所以比直接壓縮資料夾更乾淨。
常用選項
| 選項 | 含義 |
|---|---|
| --format=<fmt> | tar, zip or tar.gz |
| -o <file> | write to a file; the format is guessed from the name |
| --prefix=<dir>/ | put everything under one folder inside the archive |
| --add-file=<file> | include an untracked file as well |
| --remote=<repo> | let the server build it |
| -l | list the formats this build supports |
範例
git archive --format=tar.gz -o app-1.2.0.tar.gz v1.2.0a release tarball of exactly that tag
git archive --prefix=app/ -o app.zip HEADa zip whose contents sit inside app/
git archive -o src.zip HEAD -- srconly the src directory
難的地方在於復原有好幾種:把分支的指向挪走,和補一個反向提交,不是一回事。
怎麼看
- 方括號 [ ] 表示這部分可以不寫。
- 省略號 … 表示可以寫多個。
- 選項區分大小寫——有些命令裡 -r 和 -R 不是一回事。
常見問題
Q. git archive 是做什麼的?
把某個提交時的檔案匯出成 tar 或 zip;不會帶上 .git 和未追蹤檔案,所以比直接壓縮資料夾更乾淨。
Q. 怎麼寫?
git archive --format=tar.gz -o <file> <tree-ish> —— 方括號表示可以省略的部分。
Q. 值得記的選項有幾個?
這裡列了 6 個,完整列表在 man git。這條命令屬於git。
相關命令
man 手冊: man git-archive