自动提交存储库中的更改

Auto commiting changes in a repository

我们正在尝试自动提交对遗留目录的更改,人们不想在其中使用版本控制之类的东西(叹息)。

我每天晚上都在使用 gitpython 提交这些更改:

repo = git.Repo(local_directory)

changed_files = [change.a_blob.path for change in repo.index.diff(None)]
if changed_files or repo.untracked_files:

   if changed_files:
      repo.git.add(update=True)

   if repo.untracked_files:
      repo.git.add(repo.untracked_files)

   repo.git.commit(message='Auto committed')
   repo.remotes.origin.push(repo.head)

有时提交失败并显示“'git commit --message=Auto committed' 返回退出代码 1”- 我无法重现

我是不是哪里做错了?我读过也许我应该使用 repo.index 进行提交?

最好的, 克里斯托弗

您完全正确,您需要使用 repo.index 进行提交。这是我使用 GitPython 编写的脚本的工作示例(顺便说一下,它可以帮助我们进行版本控制)

repo = Repo(repo_dir)
index = repo.index

然后是我的 commit_version 函数:

def commit_version(requested_version, commit_msg, index, version_file):
    """Commits version to index"""

    print "Committing: ", requested_version

    index.add([version_file])
    index.commit(commit_msg)

所以您可以将 "Auto committed" 作为您的 commit_msg 传入。希望这对您有所帮助!