Search code examples
pythongitgit-pushgit-pullgitpython

how to pull, push with remote branch


I'm trying to automate a change process which currently creates source code that gets manually pushed to Git. I'm trying to wrap that code using GitPython:

from git import *

# create the local repo
repo = Repo.init("/tmp/test/repo", bare=True)
assert repo.bare == True

# check status of active branch
repo.is_dirty()

# clone the remote repo
remote_repo = repo.clone("http://user:pass@git/repo.git")

# compile source code into repository
# ... 

# track untracked files
repo.untracked_files

# commit changes locally
repo.commit("commit changes")

# push changes to master
remote_repo.push()

When I try running this, I get

Traceback (most recent call last):

File "git_test2.py", line 33, in

repo.commit("commit changes")

BadObject: 636f6d6d6974206368616e676573

The script is able to pull the remote repository, but fails on commit. Is there a better approach to this?


Solution

  • Some of the functions you are using may not work the way you expect them to. Generally, Repo methods are not the equivalent of the git sub-command with the same name.

    If you are trying to clone a remote repository, this can be achieved in a single line:

    repo = Repo.clone_from("http://user:pass@git/repo.git", "/tmp/test/repo")
    

    See the API Reference for more information on how to use GitPython.