Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Username and password authentication #1695

Answered by Byron
krupis asked this question in Q&A
Discussion options

Hello. I recently came across this library for git on Python. I am working on a project where client needs to have a script which he can run to fetch/pull all the changes from my git repository.

The most important thing is that the computer will not have any SSH keys associated with gitlab therefore it will need to use username and password for authentication. I havent managed to find a way how to do this using GitPython.

I can use the following script to commit and push to the repository:

import git
full_local_path = "C:/Users/test/Desktop/WORK/Test/test_main/testfw"
branch_name = "test_branch"
repo = git.Repo(full_local_path)
origin = repo.remote(name="origin")
# COMMIT & PUSH
repo.index.commit("Some commit message123")
origin = repo.remote(name="origin")
origin.push()
print("Script complete")

The above script will work without any issues on the machine that has SSH keys for authentication.

However, my other Raspberry PI device will not have SHH keys and I want to be able to authenticate using username and password.
Currently, I manually have to fetch and pull all the changes as following:

My_raspberry@raspberrypi:~/DEV/testfw/fw $ git push
Username for 'https://xxx-gitlab.xxxxxx.lt': name.surname@xxxxxx.lt
Password for 'https://name.surname@xxxxxx.lt@xxx-gitlab.xxxxxx.lt': 
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 4 threads
Compressing objects: 100% (8/8), done.
Writing objects: 100% (8/8), 974 bytes | 487.00 KiB/s, done.
Total 8 (delta 7), reused 0 (delta 0), pack-reused 0
remote: 
remote: To create a merge request for test_branch, visit:
remote: https://xxx-gitlab.xxxxxx.lt/test/testfw/-/merge_requests/new?merge_request%5Bsource_branch%5D=test_branch
remote: 
To https://xxx-gitlab.xxxxxx.lt/test/testfw.git
 761a21c..c10b27e test_branch -> test_branch 

and it will work fine without any issues. I was hoping to automate this process using GitPython.

Any help is appreciated

You must be logged in to vote

The tutorial contains an example for how to configure SSH connections. As for HTTPS, I'd expect such credentials to be stored and retrieved by git's built-in credential helpers.

URLs are allowed to contain username and password, and these could be used through GitPython as well. It's also possible to call credential managers by hand and store a user and password for a URL programmatically. The format is specified in the git documentation - please be aware though that no tooling exists to make interacting with them easier, at least not in GitPython.

I hope that helps.

Replies: 2 comments 2 replies

Comment options

The tutorial contains an example for how to configure SSH connections. As for HTTPS, I'd expect such credentials to be stored and retrieved by git's built-in credential helpers.

URLs are allowed to contain username and password, and these could be used through GitPython as well. It's also possible to call credential managers by hand and store a user and password for a URL programmatically. The format is specified in the git documentation - please be aware though that no tooling exists to make interacting with them easier, at least not in GitPython.

I hope that helps.

You must be logged in to vote
0 replies
Answer selected by Byron
Comment options

@Byron
You mentioned I can use username and password within the URL. Isnt't that the solution to my problem or that still wont work?

Could you provide any code examples?

I assume something like that: (Notice I have full_url that includes my account username and password). How can I use this full url in GitPython?

import git
full_local_path = "/home/raspberrypi/DEV/testfw/fw"
username = yyyyyyy
password = yyyyyyyyyy
#I am not sure how to use full_url variable?
full_url= f"https://{username}:{password}@xxx.gitlab.xxxxxxxx.lt/testfw/fw.git"
repo = git.Repo(full_local_path)
repo.index.commit("Some commit message")
origin = repo.remote(name="origin")
origin.push()
You must be logged in to vote
2 replies
Comment options

Byron Oct 5, 2023
Maintainer

GitPython allows to interact with git mostly like one would on the git command-line. I recommend trying to do what you want on in the terminal using git and then transfer that knowledge to GitPython.

Comment options

I can do everything I want using terminal easily.
See the logs below:

My_raspberry@raspberrypi:~/DEV/testfw/fw $ git push
Username for 'https://xxx-gitlab.xxxxxx.lt': name.surname@xxxxxx.lt
Password for 'https://name.surname@xxxxxx.lt@xxx-gitlab.xxxxxx.lt': 
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 4 threads
Compressing objects: 100% (8/8), done.
Writing objects: 100% (8/8), 974 bytes | 487.00 KiB/s, done.
Total 8 (delta 7), reused 0 (delta 0), pack-reused 0
remote: 
remote: To create a merge request for test_branch, visit:
remote: https://xxx-gitlab.xxxxxx.lt/test/testfw/-/merge_requests/new?merge_request%5Bsource_branch%5D=test_branch
remote: 
To https://xxx-gitlab.xxxxxx.lt/test/testfw.git
 761a21c..c10b27e test_branch -> test_branch 

As you can see from above, I execute git push command and I am prompted to type in username and password. I want to do the exact same thing in GitPython. Have you got any clues how can this be achieved?

Since you said that GitPython allows interacting with git like git command line, there should be away to perform username and password authentication using https. It would be great if you could point me in the right direction and provide some example code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1694 on October 04, 2023 17:22.

AltStyle によって変換されたページ (->オリジナル) /