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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import git, os, shutil DIR_NAME = "temp" REMOTE_URL = "https://github.com/hasinhayder/LightBulb.git" if os.path.isdir(DIR_NAME): shutil.rmtree(DIR_NAME) os.mkdir(DIR_NAME) repo = git.Repo.init(DIR_NAME) origin = repo.create_remote('origin',REMOTE_URL) origin.fetch() origin.pull(origin.refs[0].remote_head) print "---- DONE ----" |
PS: Installing GitPython is just a breeze on *Nix with:
1 |
sudo easy_install GitPython |
17 replies on “Fetching Remote Git Repo with Python: In a few lines of codes”
Thx man, I’ve been looking forward for a good way to do that
But GitPython is outdated for three years …
It was still being used when I wrote this article in early 2012.
SO what is the latest library for interacting to git repo from python.
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. ‘
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.
Hey,
How can i find the Git binary for GitPython…. ??
i have the same problem…. —–>
—–>
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.
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 ??
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 ??
Sorry, not sure.
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
This looks like a Windows related error. I have no ideas, sorry. I haven’t used Windows for development for around 6-7 years now.
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 ?
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. : )
Awesome! thanks for your help!
Hi,
This piece of code is not working in private repository. In Private repository how to make this code to work.
Could you please help me on this.
Thanks,
Munish