git - 如何在不首先克隆整个 repo 的情况下将文件添加到远程 Git repo (Github)

标签 git svn github

这个 Git 问题与 another one 非常相似关于SVN。

我有一个充满大文件的存储库,我需要向其中添加一个文件。这在 SVN 中曾经非常容易。

svn import -m "Adding just a file"file_name http://path/to/svn/repo/file_name

如何在 Git 中完成这个简单的任务?

最佳答案

对于 GitHub(不是 git 本身),the GitHub API提供了一种无需克隆即可创建文件的方法:

https://developer.github.com/v3/repos/contents/#create-a-file

PUT /repos/:owner/:repo/contents/:path

# Parameters #
----------------------------------------------------------------
Name     Type    Description
------   ------  -----------------------------------------------
path     string  Required. The content path.
message  string  Required. The commit message.
content  string  Required. The new file content, Base64-encoded.
branch   string  Branch name. Default: repo's default branch.

最小示例 JSON 输入:

{
  "message": "my commit message",
  "content": "bXkgbmV3IGZpbGUgY29udGVudHM="
}

所以你可以编写一个脚本来对文件内容进行 base64 编码,然后让它使用 curl 或类似的方式将上面的一些 JSON 发送到 https://api.github.com/repos/:owner/:repo/contents/:path

如果成功,您将获得 a JSON response back包括所创建文件的 GitHub URL。

无需克隆也可以更新:https://developer.github.com/v3/repos/contents/#update-a-file

关于git - 如何在不首先克隆整个 repo 的情况下将文件添加到远程 Git repo (Github),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19619450/

相关文章:

php - Composer 更新删除了我的更改

git filter-branch 删除文件夹失败

linux - 安装 insights 时出错(安装 bower){ OpenEdX,Ubuntu Server 12.04 }

svn - 如何抢先推迟 svn 合并冲突?

github - GitHub 和要点有什么区别?

具有克隆访问权限的 github 个人访问 token

git - 使用 capistrano 和 git 将相同的代码库(略有差异)部署到多个服务器

linux - CentOS 6.x 上 Subversion 1.8 的新存储库中缺少 .svn 目录

Github - 需要有关发出拉取请求的帮助

git svn 克隆 SVN 仓库的单个目录