0

I want to create a short git workflow to do a few things using a python library for git (GitPython).

If anyone has successfully used GitPython with ssh keys to do basic things like a git pull, please post.

Following the documentation does not work.


The documentation is very confusing, it jumps right into the most advanced use cases that I doubt anyone, anywhere would use, and going back and forth between using a call like: repo.function() and git.function() is confusing.


I only need to do the simple usual things: pull, add, commit, push, merge etc. but am really struggling to dig through the documentation to find how to do the most common things - items that I would expect to be at the very front of any documentation.

In addition, I do not understand why anyone would use GitPython to automate things and then pause to enter their credentials each time they hit a hosted repo. Who are these people?

The most common use case would be to do a pull/push etc., anything that operates on the remote with a ssh key.

If anyone has successfully used GitPython with a ssh key, please share, it would be greatly appreciated.


I would be very glad to help shred the current documentation, start over, with the most common use cases first, and add ssh key use functionality up front, but I need to gather all of this information first and build a library of simple functionality before getting involved there (and I will - it needs to be done).

3
  • I believe GitPython is just a wrapper around Git. Using an ssh key with GitPython should be no different than using one with Git. A search finds instructions on how to specify a specific ssh key but that should not be necessary. Commented Dec 9, 2020 at 22:08
  • 1
    This did not work, I have not been able to get ssh keys to work with GitPython. Maybe it's just not implemented. Commented Jan 4, 2021 at 21:52
  • There should be nothing to implement. How does it "not work"? Please edit the details into the answer. Commented Jan 4, 2021 at 23:07

1 Answer 1

1

GitPython is just a wrapper around the Git executable. Using an ssh key with GitPython is the same as using one with Git itself.

Here is a demonstration. I have removed my ssh key from my ssh agent.

>>> from git import Repo
>>> repo = Repo("/Users/schwern/devel/ruby")
>>> origin = repo.remote("origin")
>>> for fetch_info in origin.fetch():
...     print("Updated %s to %s" % (fetch_info.ref, fetch_info.commit))
... ^D
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/git/remote.py", line 797, in fetch
    res = self._get_fetch_info_from_stderr(proc, progress)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/git/remote.py", line 676, in _get_fetch_info_from_stderr
    proc.wait(stderr=stderr_text)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/git/cmd.py", line 408, in wait
    raise GitCommandError(self.args, status, errstr)
git.exc.GitCommandError: Cmd('git') failed due to: exit code(128)
  cmdline: git fetch -v origin
  stderr: 'fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.'

As you can see, it's just calling git fetch -v origin. I get the same error if I run git fetch -v origin.

$ git fetch -v origin
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Once I've added my ssh key, origin.fetch() succeeds.

>>> for fetch_info in origin.fetch():
...     print("Updated %s to %s" % (fetch_info.ref, fetch_info.commit))
... ^D
Updated origin/master to 7a3322a0fd660d676f1918bd7c4a37676b44e1c2
...etc...

Make sure git fetch works. Then GitPython should work.

Sign up to request clarification or add additional context in comments.

2 Comments

This answer is the closest thing I've had to success with GitPython, but the keys are forgotten on exit. Since python is instancing a new command-line/process for each clone_from call. Could you specify how to setup a bash script that loads an ssh-key on a process by process basis? It seems like all the examples have a one-liner bash script inputted as SSH_COMMAND e.g. !#/bin/bash "ssh -i /etc/.ssh/your_key" but that is insufficient to auto-load keys. When I add more to my bash script it bails out.
@bathMarm0t Use an ssh-agent. atlassian.com/git/tutorials/git-ssh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.