2017-01-19 2 views
-2

로컬 git 저장소가 있습니다. 파이썬 파일을 'fibo.py'라고 부릅니다. 편집기에서 해당 파일을 편집하고 변경 사항을 저장했습니다. 그런 다음 repo에 해당 파일을 커밋하기 위해 다음 코드를 작성했습니다.gitpython 라이브러리를 사용하여 git에 파일을 커밋 할 수 없습니다.

from git import Repo 
import os 

repo_dir = 'D:\\Git Repo\\newrepo1' 
file_list = [] 
repo1 = Repo(repo_dir) 
print(os.getcwd()) 
os.chdir('%s' % repo_dir) 
dirs = os.listdir() 
print(os.getcwd()) 
# This would print all the files and directories 
for file in dirs: 
    if file != '.git': 
     print(file) 
     new_file_path = osp.join(repo1.working_tree_dir, file) 
     open(file,'r').close 
     filelist.append(new_file_path) 

commit_message = 'adding new files' 
repo1.index.add(file_list) 
repo1.index.commit(commit_message) 

커밋을 수행 중입니다. 나는 로그를 확인하지만 확인 때 그것은 나에게

['fibo.py'] 
COMMITS---> {} 
COMMITS---> {} 
COMMITS---> {} 
COMMITS---> {} 

사람이 나를 변화 파일 목록 및 file_list의 철자를 잘못의 오류를 제외하고

+0

코드를 수정하고 불필요한 혼란 제거하십시오 작업 (인쇄 디버그 출력을, 등등 ...) . 코드가 여러 이유 ('filelist'가 정의되지 않았거나, 모듈이'osp', ...가 아닙니다)로 실행되지 않습니다. –

+0

예제가 제대로 작동하고'COMMITS ---> {u'fibo.py ': {'deletions ': 0,'lines ': 0,'insertions ': 0}}' –

+0

'print (file_list)'는 무엇을 출력합니까? –

답변

-1

을 제안 도와 드릴이 출력을 제공 기록

print(repo1.untracked_files) 
for commit in list(repo1.iter_commits()): 
    print('COMMITS--->',commit.stats.files) 

을 커밋합니다. 나는 그것을 해결, 단지

for file in dirs: 
    if file != '.git': 
     print(file) 
     new_file_path = osp.join(repo1.working_tree_dir, file) 
     print(new_file_path) 
     print(repo1.index) 
     #open(file,'w').close 
     repo1.index.add([new_file_path]) 

전용 루프의 경우 후 repo1.commit을 넣어 for 루프를 편집하고는

관련 문제