[英]Git clone without .git directory
進行克隆時是否有要傳遞給git
的標志,比如不要克隆.git
目錄? 如果沒有,如何在克隆后刪除.git
目錄的標志?
利用
git clone --depth=1 --branch=master git://someserver/somerepo dirformynewrepo
rm -rf ./dirformynewrepo/.git
dirformynewrepo
不再是 Git 存儲庫。由於您只想要文件,因此不需要將其視為 git repo。
rsync -rlp --exclude '.git' user@host:path/to/git/repo/ .
這僅適用於本地路徑和遠程 ssh/rsync 路徑,如果遠程服務器僅提供 git:// 或 https:// 訪問,則可能無法正常工作。
對於那些懷疑--depth 1
解決方案的人,因為它仍然下載.git
目錄並且您需要在之后手動刪除它,也許您需要知道git clone 實際工作原理。
當你通常克隆一個 repo 時,git 將你所有的文件(跨越提交)下載到.git
目錄中。 當您使用--depth 1
進行克隆時,git 只會將最新版本的文件下載到.git
中。 之后,git 會將這些文件從.git
checkout
或檢索到工作目錄中(不再下載)。
通常,由於.git
中的文件對象是壓縮的,因此您可以通過使用git clone --depth 1
下載文件而不是下載未壓縮的文件來節省更多帶寬。 對於一些網速慢的人來說,這是值得的(需要運行rm -rf
)。
我個人認為git archive
解決方案更好,但由於 GitHub 不支持它, --depth 1
是要走的路。
git clone --separate-git-dir=$(mktemp -u) --depth=1 <repo> <dir> && rm <dir>/.git
我更喜歡這個解決方案,因為我不喜歡rm -rf
自動處理事情。 它只是rm
sa .git 文件,這意味着它永遠不會意外rm -rf
錯誤的 .git 目錄
它依賴於mktemp
命令,因此它可以在 *nix 系統上運行(據我所知,這需要進一步的工作才能使mktemp
在 MacOS 上運行,所以如果有人想評論一個可行的解決方案,我會添加它)
在 zsh 中,我創建了一個函數,因此我確保定義了一個dir
值:
alias np='node-project'
function node-project() {
dir=${1:-.}
git clone --separate-git-dir=$(mktemp -u) --depth=1 <my-node-repo> $dir && rm $dir/.git
}
--separate-git-dir
標志允許您指定 .git 目錄的路徑。 生成的“項目”將有一個 .git 文件(不是目錄),其內容將是一行:
gitdir: <the dir you specified in the flag>
因為我們在mktemp
命令中使用了 tmp 目錄,所以實際的.git
目錄內容最終會出現在 tmp 目錄中。 我們還使用了--depth=1
,因此它在 tmp 目錄上占用的空間更少。
git archive --remote
已經實現了這一點。
git clone --depth=1 --branch=master git://someserver/somerepo dirformynewrepo1
rd /s /q .\dirformynewrepo1\.git
這適用於 Windows 系統
如果存儲庫包含一些標簽,您可以簡單地下載該標簽的 zip:
curl -L https://github.com/<user>/<repo-name>/archive/refs/tags/v1.2.3.zip | tar xz
這將下載廣告解壓縮克隆的存儲庫,但沒有 .git 文件夾。 生成的文件夾將是<repo-name>-1.2.3
你總能做到
git clone git://repo.org/fossproject.git && rm -rf fossproject/.git
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.