# git-test **Repository Path**: tongstyle/git-test ## Basic Information - **Project Name**: git-test - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-07-14 - **Last Updated**: 2025-07-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Git 部分克隆工具 这个工具可以帮助你从一个Git仓库克隆特定目录,并将其推送到另一个仓库的相同目录下。 ## 功能特性 - ✅ 使用Git sparse-checkout功能,只克隆指定目录 - ✅ 支持不同分支的克隆 - ✅ 自动处理目标仓库的文件更新 - ✅ 支持自定义提交信息 - ✅ 自动清理临时文件 - ✅ 支持私有仓库(需要配置认证) ## 安装要求 - Python 3.6+ - Git(已安装并配置在PATH中) - 对目标仓库的写入权限 ## 快速开始 ### 基本用法 ```python from git_partial_clone import GitPartialClone # 创建克隆器实例 cloner = GitPartialClone() # 执行目录传输 success = cloner.transfer_directory( source_repo="https://github.com/username/test.git", target_repo="https://github.com/username/test1.git", directory_path="people/student/computer" ) if success: print("✅ 传输成功!") else: print("❌ 传输失败!") ``` ### 高级用法 ```python # 指定分支和自定义提交信息 success = cloner.transfer_directory( source_repo="https://github.com/username/test.git", target_repo="https://github.com/username/test1.git", directory_path="people/student/computer", source_branch="develop", # 从develop分支克隆 commit_message="Sync computer directory from develop branch" ) ``` ## 使用场景 1. **项目模块同步**:将一个大型项目中的特定模块同步到另一个仓库 2. **代码库拆分**:从单体仓库中提取特定目录到新的仓库 3. **跨团队协作**:在不同团队的仓库间同步共享代码 4. **备份特定目录**:定期备份重要目录到备份仓库 ## 认证配置 ### 方式1:使用Personal Access Token ```python # 在仓库URL中包含token source_repo = "https://username:your_token@github.com/username/repo.git" ``` ### 方式2:使用SSH密钥 ```python # 使用SSH格式的URL source_repo = "git@github.com:username/repo.git" ``` ### 方式3:配置Git凭据存储 ```bash # 配置Git记住凭据 git config --global credential.helper store ``` ## API 参考 ### GitPartialClone 类 #### transfer_directory(source_repo, target_repo, directory_path, source_branch='main', commit_message=None) 完整的目录传输流程。 **参数:** - `source_repo` (str): 源仓库URL - `target_repo` (str): 目标仓库URL - `directory_path` (str): 要传输的目录路径(相对于仓库根目录) - `source_branch` (str, 可选): 源分支名称,默认为'main' - `commit_message` (str, 可选): 提交信息,默认自动生成 **返回值:** - `bool`: 成功返回True,失败返回False #### clone_partial_directory(source_repo, target_path, branch='main') 克隆指定目录。 #### push_to_target_repo(target_repo, target_path, commit_message) 推送到目标仓库。 #### cleanup() 清理临时文件。 ## 注意事项 1. **权限要求**:确保你有目标仓库的写入权限 2. **网络连接**:需要稳定的网络连接来克隆和推送 3. **Git配置**:确保Git已正确安装和配置 4. **路径格式**:目录路径使用正斜杠(/),即使在Windows上 5. **分支名称**:确保指定的分支在源仓库中存在 ## 错误处理 工具会自动处理常见错误: - 网络连接问题 - 认证失败 - 分支不存在 - 目录不存在 - 权限不足 所有错误信息都会打印到控制台,便于调试。 ## 示例文件 查看 `example_usage.py` 文件获取更多使用示例。 ## 许可证 本项目采用MIT许可证。