2017-01-19 54 views
-2

我有一个本地git存储库。我有一个名为'fibo.py'的python文件。我在编辑器中编辑了该文件并保存了更改。然后,我编写了以下代码来提交该文件进行回购。无法使用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) 

它正在执行提交。我检查日志,但是当我检查了提交记录

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

它给了我这个输出

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

谁能帮我修改建议

+0

请修正你的代码,并删除不必要的混乱(打印调试输出,等...) 。你的代码甚至不会因为多种原因运行('filelist'没有定义,没有模块'osp',...) –

+0

删除了所有的混乱你的例子为我工作,我得到''COMMITS --->', {u'fibo.py':{'deletions':0,'lines':0,'insertions':0}}' –

+0

'print(file_list)'输出什么? –

回答

-1

除了文件清单和file_list中拼错的错误。我解决了它,只需编辑for循环

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循环和它的作品