gitcurl
v1.0.0
Published
Clone git repositories without .git and optionally pick paths
Readme
gitcurl
gitcurl 是对 git clone 的轻量包装:克隆完成后自动删除 .git 目录,适合拉取模板、脚手架或只需源码、不需要 Git 历史的场景。支持从仓库中提取指定文件或目录到本地任意路径。
环境要求
- Node.js >= 16.7.0
- Git 已安装并在 PATH 中可用
若未检测到 Git,会提示安装方式:
macOS: brew install git
Windows: https://git-scm.com/download/win
Linux: apt install git / yum install git安装
# 全局安装(推荐)
npm install -g gitcurl
# 临时使用
npx gitcurl <repository>
# 本地开发
git clone <this-repo>
cd gitcurl
npm link快速开始
# 克隆仓库,自动删除 .git
gitcurl https://github.com/user/repo.git
# 指定本地目录名
gitcurl https://github.com/user/repo.git my-project
# 指定分支
gitcurl https://github.com/user/repo.git my-project -b main
# 需要完整历史时
gitcurl https://github.com/user/repo.git --full用法
gitcurl <repository> [<directory>] [git clone 选项...]
gitcurl <repository> [<directory>] -p <仓库内路径> -o <本地路径> [git clone 选项...]| 参数 | 说明 |
| --- | --- |
| <repository> | 仓库地址,支持 HTTPS、SSH 等 git clone 支持的格式 |
| <directory> | 可选,克隆到本地的目录名,省略时使用仓库名 |
| -p <path> | 仓库内的文件或目录路径(相对仓库根目录) |
| -o <path> | 本地目标路径,不存在时自动创建 |
| --full | 克隆完整历史(默认仅拉取最近一次提交) |
| -h, --help | 显示帮助 |
| -V, --version | 显示版本号 |
功能说明
1. 包装 git clone
gitcurl 底层调用 git clone,完整透传所有 git clone 参数、行为和环境变量(如 GIT_SSH_COMMAND、http.proxy 等),用法与原生 git clone 一致,只是不需要手动输入 clone 子命令。
gitcurl [email protected]:user/repo.git
gitcurl https://github.com/user/repo.git -b develop --single-branch
gitcurl https://github.com/user/repo.git --recurse-submodules2. 浅克隆(默认)
为加快大仓库的拉取速度,默认使用 --depth 1 浅克隆,只获取最新一次提交,不拉取完整历史。
# 默认浅克隆
gitcurl https://github.com/user/large-repo.git
# 需要完整历史
gitcurl https://github.com/user/large-repo.git --full
# 自定义深度(会覆盖默认值)
gitcurl https://github.com/user/large-repo.git --depth 53. 自动删除 .git
克隆成功后,默认删除目标目录下的 .git 文件夹,只保留工作区文件。
以下情况不会删除 .git:
- 使用了
--bare或--mirror选项
4. 提取指定路径(-p / -o)
在克隆并删除 .git 之后,可将仓库内指定文件或目录复制到本地任意路径。-p 和 -o 必须成对使用。
# 提取单个文件
gitcurl https://github.com/user/repo.git -p README.md -o ./docs/readme.md
# 提取目录
gitcurl https://github.com/user/repo.git -p packages/core -o ./core
# 结合 clone 选项
gitcurl https://github.com/user/repo.git mydir -b main -p src/utils -o ./utils说明:
-p路径相对于克隆后的仓库根目录-o目标路径的父目录不存在时会自动创建- 支持文件和目录,跨平台(macOS / Windows / Linux)
- 复制完成后会自动删除克隆目录,可重复执行而不会因目录已存在而失败
示例
拉取项目模板
gitcurl https://github.com/user/vite-template.git my-app
cd my-app
npm install只拿某个子目录
gitcurl https://github.com/user/monorepo.git -p apps/web -o ./web私有仓库(SSH)
gitcurl [email protected]:org/private-repo.git私有仓库(HTTPS + Token)
gitcurl https://<token>@github.com/org/private-repo.git注意事项
-p/-o是gitcurl专用参数,会在调用git前被剥离,不会传给git clone--full是gitcurl专用参数,用于关闭默认浅克隆gitcurl的-o表示本地输出路径;若需设置 remote 名称,请使用 git 的--origin选项- 若
-p指定的路径在仓库中不存在,命令会以非零状态码退出并提示错误
与 git clone 对比
| | git clone | gitcurl |
| --- | --- | --- |
| 保留 .git | 是 | 否(默认删除) |
| 提取子路径 | 需手动操作 | 支持 -p / -o |
| 命令形式 | git clone <url> | gitcurl <url> |
跨平台支持
支持 macOS、Linux、Windows 三大平台,依赖均为 Node.js 内置模块,无第三方依赖。
| 能力 | macOS | Linux | Windows |
| --- | --- | --- | --- |
| 全局安装 (npm install -g) | ✅ | ✅ | ✅ |
| 浅克隆 / 删 .git | ✅ | ✅ | ✅ |
| -p / -o 路径复制 | ✅ | ✅ | ✅ |
| 路径分隔符 / 或 \ | ✅ | ✅ | ✅ |
前提条件:
- 已安装 Node.js >= 16.7.0
- 已安装 Git 并加入 PATH(Windows 需安装 Git for Windows)
开发
npm testLicense
MIT
