Fetching Remote Git Repo with Python: In a few lines of codes

In my old posts, I have always made rants about how cool and comfy Python is. Here’s a tiny code snippet that shows how I fetch remote Git repos with Python and the awesome GitPython library:

PS: Installing GitPython is just a breeze on *Nix with:


15 Comments Fetching Remote Git Repo with Python: In a few lines of codes

  1. bharath

    After trying this, I got couple of errors…

    *** -> project path and project name.

    Traceback (most recent call last):
    File “<pyshell#29>”, line 1, in <module>
    git.Repo.init(‘F:\***\***’)
    File “C:\Python27\lib\site-packages\gitpython-0.3.6-py2.7.egg\git\repo\base.py”, line 759, in init
    git.init(**kwargs)
    File “C:\Python27\lib\site-packages\gitpython-0.3.6-py2.7.egg\git\cmd.py”, line 431, in <lambda>
    return lambda *args, **kwargs: self._call_process(name, *args, **kwargs)
    File “C:\Python27\lib\site-packages\gitpython-0.3.6-py2.7.egg\git\cmd.py”, line 778, in _call_process
    return self.execute(make_call(), **_kwargs)
    File “C:\Python27\lib\site-packages\gitpython-0.3.6-py2.7.egg\git\cmd.py”, line 607, in execute
    raise GitCommandError(command, status, stderr_value)
    GitCommandError: ‘git init’ returned with exit code 1
    stderr: ”git’ is not recognized as an internal or external command,
    operable program or batch file. ‘

    Reply
    1. heaje

      That looks like the GitPython library can’t find the git binary. Verify that your PATH environment variable includes the location of the git binary.

      Reply
  2. Kalysto

    Hey,

    How can i find the Git binary for GitPython…. ??

    i have the same problem…. —–>

     

    —–>

     

    Reply
    1. maSnun

      Please make sure you have git installed and the “git” binary is in your $PATH. Open Terminal and type in “git” – if that works, it’s likely that you have it setup.

      Reply
      1. Kalysto

        Sure “Git” is Installed and “Git” is in my $PATH = /usr/local/git/bin/(git)
        with the Terminal it works too. It comes some Info´s.

        what do you mean with setup? to reinstall ??

        Reply
        1. Kalysto

          or can i Use the Full PATH of “git”

          in bash i can run some App´s with the full path of it for example:

          /usr/local/git/bin/git.Repo.init(DIR_NAME)

          is there a way to do this in Python ??

          Reply
  3. Amit

    I Got following Error: please Help me to solve this

     

    Traceback (most recent call last):
    File “D:\Python_Program\clone.py”, line 13, in <module>
    repo = git.Repo.init(path)
    File “C:\Python27\lib\site-packages\git\repo\base.py”, line 656, in init
    output = git.init(**kwargs)
    File “C:\Python27\lib\site-packages\git\cmd.py”, line 227, in <lambda>
    return lambda *args, **kwargs: self._call_process(name, *args, **kwargs)
    File “C:\Python27\lib\site-packages\git\cmd.py”, line 456, in _call_process
    return self.execute(call, **_kwargs)
    File “C:\Python27\lib\site-packages\git\cmd.py”, line 335, in execute
    **subprocess_kwargs
    File “C:\Python27\Lib\subprocess.py”, line 710, in __init__
    errread, errwrite)
    File “C:\Python27\Lib\subprocess.py”, line 958, in _execute_child
    startupinfo)
    WindowsError: [Error 2] The system cannot find the file specified

    Reply
  4. krishnadass

    Traceback (most recent call last):
    File “test.py”, line 2, in
    git.Git().clone(“git://gitorious.org/git-python/mainline.git”)
    File “/usr/local/lib/python2.7/dist-packages/git/cmd.py”, line 424, in
    return lambda *args, **kwargs: self._call_process(name, *args, **kwargs)
    File “/usr/local/lib/python2.7/dist-packages/git/cmd.py”, line 873, in _call_process
    return self.execute(call, **_kwargs)
    File “/usr/local/lib/python2.7/dist-packages/git/cmd.py”, line 687, in execute
    raise GitCommandError(command, status, stderr_value, stdout_value)
    git.exc.GitCommandError: Cmd(‘git’) failed due to: exit code(128)
    cmdline: git clone git://gitorious.org/git-python/mainline.git
    stderr: ‘Cloning into ‘mainline’…
    fatal: unable to connect to gitorious.org:
    gitorious.org[0: 64.13.172.37]: errno=Connection refused

    how solve ?

    Reply
  5. Noël

    Do you propose in your snippet to delete any directory that happens to have the same name and already exists? This is dangerous and unecessary. I understand you want a clean workspace for the git repository, but either do I in a clean way (non-destructive, interactive) or don’t show it in the example. At least that is my opinion.

    “Fetching” in git means just looking if anything has changed in ORIGIN, not cloning the whole repository again. That is called “cloning”.

    But thank you for your example, because the part that is actually about gitpython was very helpful. : )

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *