简体   繁体   English

git push 使用 python

[英]git push using python

I have local git repository.我有本地 git 存储库。 I am using python to commit the local repo using gitpython library.我正在使用 python 使用 gitpython 库提交本地存储库。 I want to push the commit to github.我想将提交推送到 github。 How can I do this using gitpython or any other library.我如何使用 gitpython 或任何其他库来做到这一点。 I looked online but there was no solution available.我在网上看了,但没有可用的解决方案。 Can anyone help me with this.谁能帮我这个。 Thanks in advance提前致谢

from git import Repo,remote

rw_dir = 'path/to/your/local/repo'
repo = Repo(rw_dir)

'''Enter code to commit the repository here.
After commit run the following code to push the commit to remote repo.
I am pushing to master branch here'''

origin = repo.remote(name='origin')
origin.push()

The code which is used to commit and push to the GitHub using python as follows,用于使用python提交和推送到GitHub的代码如下,

import subprocess as cmd
def git_push_automation():
    try:
        cp = cmd.run("file path", check=True, shell=True)
        print("cp", cp)
        cmd.run('git commit -m "message"', check=True, shell=True)
        cmd.run("git push -u origin master -f", check=True, shell=True)
        print("Success")
        return True
    except:
        print("Error git automation")
        return False

If you are using Password-based authentication如果您使用基于密码的身份验证

import subprocess,os,commands

git_push = " cd /to/the/repo/directory/ ;  git add -A ; git commit -m 'my message' ; git push --repo  https://<username_here>:<password_here>@bitbucket.org/fullpath/to/your_repo.git --all "
  
git_push_status = commands.getstatusoutput(git_push)

print(git_push_status[1])

The main thing is to replace the repo URL properly,for example if your repo and credentials are主要是正确替换 repo URL,例如,如果您的 repo 和凭据是

username_here : manjunath username_here : manjunath

password_here : password@123 password_here _这里: password@123

Then git push URL should look like this, Note that special characters present in the password need to be replaced(I have replaced @ present in password to %40 in the URL)然后 git push URL 应该是这样的,注意密码中存在的特殊字符需要替换(我已经将密码中的@替换为 URL 中的%40

https://manjunath:password%40123@bitbucket.org/fullpath/to/your_repo.git https://manjunath:password%40123@bitbucket.org/fullpath/to/your_repo.git

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM