How to commit and push files using python library GitPython
Asked Answered
C

0

6

Requirement:

  • Commit and push files to GitHub repository from a python script.
  • The credentials should be included in the script.

Issue:

  • If credentials are provided in the script the commit operation is executing and throwing the following error,
Traceback (most recent call last):
  File "/home/amith/example.py", line 14, in <module>
    repo.index.add(folder_path)
AttributeError: 'Repository' object has no attribute 'index'
  • If credentials are, not provided in the script the commit operation is working properly by providing it on the terminal.

I need to integrate this script in Django application which should accept the credentials from the configuration file.

I have tried the following links but nothing has worked for me yet. - link1 - link2 - link3

from git import Repo
from github import Github
from pdb import set_trace as bp

repo_dir = '--------'
repo = Repo(repo_dir)

# using username and password

g = Github("-----", "------")
folder_path = '----------'
commit_message = 'Add New file'
repo.index.add(folder_path)
repo.index.commit(commit_message)
origin = repo.remote('origin')
origin.push()

So, I am getting this error "AttributeError: 'Repository' object has no attribute 'index'".

Complete error -

Traceback (most recent call last):
  File "/home/amith/example.py", line 14, in <module>
    repo.index.add(folder_path)
AttributeError: 'Repository' object has no attribute 'index'
Culex answered 5/6, 2019 at 6:41 Comment(5)
Please post the gitpython package version number, the exact error message (copy-pasted) AND the full traceback (idem). Also you may want to edit your question's title to reflect your real issue.Extragalactic
GitPython==2.1.11 - version, Complete error message I have editedCulex
And where are the exact error message and full traceback ?Extragalactic
So what the repo variable actually contains? Ideally, just before the exception is thrown, i.e. on line 14?Aerostatics
repo = Repository(full_name="my repo name")Culex

© 2022 - 2024 — McMap. All rights reserved.