16

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()
3
  • 2
    try to use access token on the git URL as HTTPS_REMOTE_URL = 'https://<access_token>:[email protected]/username/your-project' in your git config file. Commented Apr 7, 2019 at 18:49
  • 3
    This is answered in 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. Commented Nov 11, 2021 at 16:40
  • 3
    Does this answer your question? gitpython git authentication using user and password Commented Nov 11, 2021 at 16:41

2 Answers 2

1

If I understand your question correctly, you are trying to write a python script to update files stored at your repository in GitHub. The alternative to using GitPython is to just directly import github

Here is the alternative to your code:

Get file content from GitHub repo:

from github import Github

token = "your_github_token_here"
g = Github(token)
repo = g.get_repo("username/repository")
contents = repo.get_contents("")


while contents:
    file_content = contents.pop(0)
    if file_content.type == "dir":
        contents.extend(repo.get_contents(file_content.path))
    else:
        print(file_content.path)
        file_content = repo.get_contents("path_to_your_file_in_github")
        file_content_decoded = (str(file_content.decoded_content.decode()))
print(file_content_decoded)

Write file content to GitHub repo:

# Write contents to file in GitHub repo:
token = "your_github_token"
g = Github(token)
file_path = "path_to_your_file"
content = "This is the content of your file."
try:
    file = repo.get_contents(file_path)
    repo.update_file(file.path, "Updating file", content, file.sha)
    print(f"Updated {file_path}")
except:
    repo.create_file(file_path, "Creating new file", content)
    print(f"Created {file_path}")
    file = repo.get_contents(file_path)
    repo.update_file(file.path, "Updating file", content, file.sha)
    print(f"Updated {file_path}")

I am not sure if that fully answers the question, but it works for me!

Sign up to request clarification or add additional context in comments.

Comments

0

You might want to consider using git (https://www.git-scm.com) and then you could use OS to send the console messages to push as if it were the user, Github has some guides on how to use git (https://github.com/git-guides). Hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.