Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gitpython git authentication using user and password

I'm using GitPython but did not find a way to push to repo using username and password. Can anybody send me a working example or give me some pointer about how to do it? What I need to do is: add a file to the repository, push it using the username and password provided.

like image 699
ozw1z5rd Avatar asked Jun 27 '17 15:06

ozw1z5rd


People also ask

How do I authenticate a username and password in Git?

To connect to a Git repository with authentication over HTTP(S), every time it needs to set a username and password. You can configure Git to remember a username and password by storing them in a remote URL or by using Git credential helper.

How do I authenticate a Git user?

There are three main approaches you can take: Using a personal authentication token or password. Using an SSH key. Using your GitHub password with 2-factor authentication.

How do I stop Git from asking for username and password?

You can avoid being prompted for your password by configuring Git to cache your credentials for you. Once you've configured credential caching, Git automatically uses your cached personal access token when you pull or push a repository using HTTPS.

How do I authenticate Git GitHub?

GitHub CLIIn the command line, enter gh auth login , then follow the prompts. When prompted for your preferred protocol for Git operations, select HTTPS . When asked if you would like to authenticate to Git with your GitHub credentials, enter Y .


2 Answers

What worked well for me (worked with GitHub, self hosted BitBucket, most likely will work on GitLab too).

Pre-requisites

Note, that despite the name, password here is your access token generated by GitHub and NOT your GitHub password.

from git import Repo

full_local_path = "/path/to/repo/"
username = "your-username"
password = "your-password"
remote = f"https://{username}:{password}@github.com/some-account/some-repo.git"

Clone repository

This will store your credentials in .git/config, you won't need them later.

Repo.clone_from(remote, full_local_path)

Commit changes

repo = Repo(full_local_path)
repo.git.add("rel/path/to/dir/with/changes/")
repo.index.commit("Some commit message")

Push changes

As mentioned above, you don't need your credentials, since they are already stored in .git/config.

repo = Repo(full_local_path)
origin = repo.remote(name="origin")
origin.push()
like image 176
Artur Barseghyan Avatar answered Oct 16 '22 22:10

Artur Barseghyan


This is what I used for myself for pulling

pull.py

#! /usr/bin/env python3

import git
import os
from getpass import getpass
    
project_dir = os.path.dirname(os.path.abspath(__file__))
os.environ['GIT_ASKPASS'] = os.path.join(project_dir, 'askpass.py')
os.environ['GIT_USERNAME'] = username
os.environ['GIT_PASSWORD'] = getpass()
g = git.cmd.Git('/path/to/some/local/repo')
g.pull()

askpass.py (similar to this one)

This is in the same directory as pull.py and is not limited to Github only.

#!/usr/bin/env python3
#
# Short & sweet script for use with git clone and fetch credentials.
# Requires GIT_USERNAME and GIT_PASSWORD environment variables,
# intended to be called by Git via GIT_ASKPASS.
#
    
from sys import argv
from os import environ
    
if 'username' in argv[1].lower():
    print(environ['GIT_USERNAME'])
    exit()
    
if 'password' in argv[1].lower():
    print(environ['GIT_PASSWORD'])
    exit()
    
exit(1)
like image 28
giantas Avatar answered Oct 16 '22 23:10

giantas