gitpython git authentication using user and password

后端 未结 3 1889
离开以前
离开以前 2021-01-01 18:57

I\'m using GitPython but did not find a way to push to repo using username and password. Can anybody send me a working example or give me some pointer about how to do it? W

3条回答
  •  被撕碎了的回忆
    2021-01-01 19:17

    What worked well for me (worked with GitHub, self hosted BitBucket, most likely will work on GitLab too).

    Pre-requisites

    Note, that despite the name, password here is your access token generated by GitHub and NOT your GitHub password.

    from git import Repo
    
    full_local_path = "/path/to/repo/"
    username = "your-username"
    password = "your-password"
    remote = f"https://{username}:{password}@github.com/some-account/some-repo.git"
    

    Clone repository

    This will store your credentials in .git/config, you won't need them later.

    Repo.clone_from(remote, full_local_path)
    

    Commit changes

    repo = Repo(full_local_path)
    repo.git.add("rel/path/to/dir/with/changes/")
    repo.index.commit("Some commit message")
    

    Push changes

    As mentioned above, you don't need your credentials, since they are already stored in .git/config.

    repo = Repo(full_local_path)
    origin = repo.remote(name="origin")
    origin.push()
    

提交回复
热议问题