[英]Gitpython to initialize and push a new repo
So I'm trying to use gitpython ( https://gitpython.readthedocs.io/en/stable/intro.html ) to do something relatively simple but having trouble.所以我试图使用 gitpython ( https://gitpython.readthedocs.io/en/stable/intro.html ) 来做一些相对简单但有问题的事情。
So I've got a blank brand new repo on my bitbucket server, then I'm using gitpython to initialize a local repo, add a file, and commit successfully.所以我的 bitbucket 服务器上有一个空白的全新 repo,然后我使用 gitpython 初始化本地 repo,添加文件并成功提交。
However, where I'm having trouble is pushing these changes to the brand new blank remote bitbucket repo I have.但是,我遇到麻烦的地方是将这些更改推送到我拥有的全新空白远程 bitbucket 存储库。 I've tried several things but I always get
git push --set-upstream origin master
as the error returned.我已经尝试了几件事,但我总是得到
git push --set-upstream origin master
作为错误返回。 But when I navigate to the the repo directory, I can see it's on the master branch, and I can see the remote repo URL when I run git status
and git remote -v
.但是当我导航到 repo 目录时,我可以看到它在 master 分支上,并且当我运行
git status
和git remote -v
时我可以看到远程 repo URL。
def commit_files(url):
repo_dir = os.path.join(os.getcwd(), 'tmp')
file_name = os.path.join(repo_dir, 'Jenkinsfile')
repo = git.Repo.init(repo_dir)
open(file_name, "wb").close()
repo.index.add([file_name])
repo.index.commit("initial commit")
repo.create_remote("origin", url=url)
repo.remote("origin").push()
All of the documentation and SO posts I've found only seem to go over pushing to an already existing repo after cloning it.我发现的所有文档和 SO 帖子似乎只是在克隆后推送到已经存在的存储库。
This one gave me a hard time too, since I tried the same as you, apparently:这个也给了我一个艰难的时刻,因为我和你一样尝试过,显然:
Create a new remote Git repository.创建一个新的远程 Git 存储库。
Create a new local Git repository (not cloning the remote one).创建一个新的本地 Git 存储库(而不是克隆远程存储库)。
Add some changes to my local repository.向我的本地存储库添加一些更改。
Tried to push my changes to the remote repository.试图将我的更改推送到远程存储库。
I finally found the reason in this to fail in the missing refspec between the local and remote repository:我终于找到了在本地和远程存储库之间缺少refspec失败的原因:
In the default case that is automatically written by a
git remote add origin
command, Git fetches all the references underrefs/heads/
on the server and writes them torefs/remotes/origin/
locally.在
git remote add origin
命令自动写入的默认情况下,Git 会获取服务器上refs/heads/
下的所有引用,并将它们写入本地的refs/remotes/origin/
。
It can be solved by adding a refspec to the .push()
method like this:可以通过向
.push()
方法添加一个 refspec 来解决它,如下所示:
repo.remote("origin").push('+refs/heads/*:refs/remotes/origin/*')
This should be able to be avoided, but I could not figure out how;这应该可以避免,但我不知道如何避免; the author seems to prefer for people to apply a workaround than to answer the question on how to do this at Github Issue #549
作者似乎更喜欢人们应用变通方法,而不是在Github Issue #549上回答有关如何执行此操作的问题
Managed to find a work around just by cloning the blank repo, making the changes, and pushing.设法通过克隆空白存储库,进行更改和推送来找到解决方法。 Not what I wanted but a sufficient solution.
不是我想要的,而是一个足够的解决方案。 Also needed to provide auth credentials in my case.
在我的情况下还需要提供身份验证凭据。
def commit_files(url):
repo_dir = os.path.join(os.getcwd(), 'tmp')
file_name = os.path.join(repo_dir, 'Jenkinsfile')
repo = git.Repo.clone_from(url, repo_dir, env={"GIT_SSH_COMMAND": 'ssh -i /PATH/TO/KEY'})
open(file_name, "wb").close()
repo.index.add([file_name])
repo.index.commit("initial commit")
repo.remote("origin").push()
return
你需要提供目标分支
repo.remote("origin").push("master")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.