10

I am trying to write a python script that when run, will push files to one of my GitHub repositories. I'm using the package GitPython. I want to use an access token to log in into my GitHub account (instead of entering my username and password) because I have two-factor authentication. I've created the token, but I can't figure out how to add it to my GitPython code.

Here is my code so far:

def push(repo_dir):
    import os
    from git import Repo

    # set working directory
    os.chdir("XXX")

    #repo_dir = 'Pantone'
    repo = Repo(repo_dir)
    file_list = [
        "index.html",
        "Data/colors.csv"
    ]
    commit_message = 'Adding new color'
    repo.index.add(file_list)
    repo.index.commit(commit_message)
    origin = repo.remote('origin')
    origin.push()
BetaDev
  • 4,516
  • 3
  • 21
  • 47
polk54
  • 111
  • 1
  • 3
  • 1
    try to use access token on the git URL as `HTTPS_REMOTE_URL = 'https://:x-oauth-basic@github.com/username/your-project'` in your git config file. – BetaDev Apr 07 '19 at 18:49
  • 1
    This is answered in [gitpython git authentication using user and password](https://stackoverflow.com/questions/44784828/gitpython-git-authentication-using-user-and-password). As it states there "_Note, that despite the name, password here is your **access token** generated by GitHub and NOT your GitHub password._" Note: I wouldn't consider it a duplicate question, considering OP explicitly asks for authentication with access token, whilst the referred answer asks for username/password. – Juliën Nov 11 '21 at 16:40
  • 2
    Does this answer your question? [gitpython git authentication using user and password](https://stackoverflow.com/questions/44784828/gitpython-git-authentication-using-user-and-password) – Juliën Nov 11 '21 at 16:41

0 Answers0