git 如何更新远程仓库中的文件,而不先克隆该仓库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16077691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to update a file in remote repo, without cloning that repo first?
提问by algal
Is there a way to push a commit to a remote git repo, without first making a local clone of that repo?
有没有办法将提交推送到远程 git 存储库,而无需先制作该存储库的本地克隆?
I have a valid URL for the remote repo, I know the path of the file, and all I want to do is push an updated version of the file onto the master. Ideally I'd like this to work with any valid remote repo URL, but it would still be helpful if it worked only with https-based git URLs.
我有远程存储库的有效 URL,我知道文件的路径,我想做的就是将文件的更新版本推送到主服务器上。理想情况下,我希望它可以与任何有效的远程存储库 URL 一起使用,但如果它仅适用于基于 https 的 git URL,它仍然会有所帮助。
I'm guessing this is impossible, since it does not seem to be possible even to retrieve a single file without cloning in the general case, according to the answer How to "git show" on a remote repo?. But I'm hoping there's a workaround that uses some of the lower level git commands.
我猜这是不可能的,因为根据答案如何在远程存储库上“git show”,在一般情况下即使不克隆也无法检索单个文件?. 但我希望有一种使用一些较低级别的 git 命令的解决方法。
采纳答案by kostix
Impossible. But since a prospective commit would just need to have one single commit as its parent it's possible to employ the so-called "shallow cloning" and fetch just the tip commit of the branch you need. This will bring only a minimum amount of objects from the remote. Look for the --depth
command-line option of git clone
.
不可能的。但是由于预期提交只需要将一个提交作为其父提交,因此可以使用所谓的“浅克隆”并仅获取您需要的分支的提示提交。这只会从远程带来最少量的对象。查找 的--depth
命令行选项git clone
。
回答by Fatih Akgun
Yes you can push a new version using tagging
是的,您可以使用标记推送新版本
follow this steps
按照这个步骤
in your new project root
在您的新项目根目录中
git init
git remote add origin [email protected]:yourusername/yourpoject
git tag -a v2.0 -m 'version 2.0'
git add .
git commit -m "New Version 2.0 :rocket:"
git push -u origin v2.0
git init
git remote add origin [email protected]:yourusername/yourpoject
git tag -a v2.0 -m 'version 2.0'
git add .
git commit -m "New Version 2.0 :rocket:"
git push -u origin v2.0
now you have a new branch called v2.0 with your new project and your master branch stays untouched. After that if you wan't you can change your default branch in your github project settings.
现在您的新项目有了一个名为 v2.0 的新分支,并且您的主分支保持不变。之后,如果您不想,您可以在您的 github 项目设置中更改您的默认分支。
回答by Oleksi
No, I don't think that's possible. You need to clone the whole repository for this to work. Git needs to know about all the files and .git
to do its job correctly. This is why you can't just push arbitrary files like that.
不,我认为这不可能。您需要克隆整个存储库才能使其正常工作。Git 需要了解所有文件并.git
正确完成其工作。这就是为什么你不能像这样推送任意文件。
回答by pingwin
Just discovered this issue and thought of an update.
刚发现这个问题,想到了更新。
If you're using github just prefix your repo url with https://gitpod.io#.
如果您使用 github,只需在您的 repo url 前加上https://gitpod.io#。
It's a service that allows you to update your repo online, so you can update files without cloning locally.
这是一项允许您在线更新存储库的服务,因此您无需在本地克隆即可更新文件。
回答by kernelsmith
@algal, for your particular use-case, you mightbe able to use a submodule (http://git-scm.com/docs/git-submodule). You could clone and commit to the submodule, w/o cloning the "supermodule" which could contain the "massive" code you mentioned. The supermodule could reference the submodule for the config info you mentioned.
@algal,对于您的特定用例,您可以使用子模块(http://git-scm.com/docs/git-submodule)。您可以克隆并提交到子模块,而无需克隆可能包含您提到的“大量”代码的“超级模块”。超级模块可以引用您提到的配置信息的子模块。
回答by tarkh
Well, if you use GitHub, then you could do this easily with GitHub API
https://developer.github.com/v3/repos/contents/#create-or-update-a-file
好吧,如果您使用 GitHub,那么您可以使用 GitHub API https://developer.github.com/v3/repos/contents/#create-or-update-a-file轻松完成此操作
Basically you do:
基本上你这样做:
Create OAuth App and get Access Token
https://developer.github.com/apps/building-oauth-apps/Make request to GitHub API:
Set Authorization header with your Access Token (OAuth 2.0)
PUT https://api.github.com/repos/:owner/:repo/contents/:path
with JSON body like this:
创建 OAuth 应用程序并获取访问令牌
https://developer.github.com/apps/building-oauth-apps/向 GitHub API 发出请求:
使用您的访问令牌 (OAuth 2.0)
PUT https://api.github.com/repos/:owner/:repo/contents/:path
使用 JSON 正文设置授权标头,如下所示:
{
"message": "my test commit through API",
"content": "QmFzZTY0IGVuY29kZWQgY29udGVudCBoZXJl",
"sha": "cd220ncv229f2e4a95bce426ff48b1ae6b885b3a42"
}
Where:
message- your commit message
content- updated content encoded in Base64 format
sha- sha for updating file, you can get it by API request
GET https://api.github.com/repos/:owner/:repo/contents/:path
其中:
message- 你提交的消息
内容- 更新后的 Base64 格式编码的内容
sha- 用于更新文件的 sha,你可以通过 API 请求获取它
GET https://api.github.com/repos/:owner/:repo/contents/ :小路
:owner- repository owner
:repo- repository name
:path- path to file within repository
:owner- 存储库所有者
:repo- 存储库名称
:path- 存储库中文件的路径
Example request:
https://api.github.com/repos/nodejs/node/contents/README.md
示例请求:https:
//api.github.com/repos/nodejs/node/contents/README.md