How to set git secret-key to GitPython in order to access already cloned local repo
import git
repo = git.Repo('local/already-cloned-repo-path')
repo.remotes.origin.pull()
Consider the case where git credentials are not stored in ~/.gitconfig. (This usecase is when the above code ran inside a docker container. )
On git pull, it will ask for credentials. How to set username and secret-key to GitPython context?
Related
There are many ways to clone the GitHub code repos from an organization. Now considering there are some repos which are private and public, I was looking into a solution where an automated script or code logic would solve my purpose of cloning the code repos irrespective of repo type. I tried various measures but couldn't land to simple automated solution. I tried using curl command, but it was slow as the density of code repos to be cloned was above 200+ code repos.
Can you try below python code snippet to clone all repos:
Here we use git ssh to clone each repos in a secure way to accommodate both public/private code.
Sometimes due to network issues , it may show remote hung/packet-loss messages which leads to repo partial/no cloning. to avoid that please set below parameter in the shell.
git config --global http.postBuffer 157286400
Assuming that your git global credentials are updated.
Code :
import os
# Define your repo list
list = ["repo1","repo2"]
#Loop it through repo's list
for repo in list:
#Clone the each repo using ssh git clone command in most secure way
cmd = "git clone git#<git ssh url>/{}".format(repo)
print("Starting to clone {}".format(repo))
os.system(cmd)
print("Finshed cloning {}".format(repo))
print("#####################################")
print("")
putted together info from two resources to clone all organizations private repositories:
# https://www.thepythoncode.com/article/using-github-api-in-python
# https://github.com/libgit2/pygit2/issues/554
# pip3 install PyGithub pygit2
from github import Github
import pygit2
# using an access token
g = Github('TOKEN')
org = g.get_organization('ORG NAME')
callbacks = pygit2.RemoteCallbacks(pygit2.UserPass('TOKEN', 'x-oauth-basic'))
# Clone repo
for repo in org.get_repos():
pygit2.clone_repository(
url=repo.clone_url,
path=f'/home/<Username>/PycharmProjects/Backup/{repo.name}',
callbacks=callbacks)
I need to make some git actions (e.g. commit, push, pull..) without cloning a repo on local via GitPython. Is there a way to do this or I must to use a path to my .git dir ?
Platforms like Github or GitLab for instance, propose tools to directly change your code online on their website. Would this be a solution for you?
I want to create a python script for making local repository and I have no idea how to do this, after local repository I want to do cherry-pick for specific commit. Any tips for me ?
Refer to below for what we have used to create a repo and some actions in Python:
Create repo
repo = git.Repo(os.path.abspath(repo_path))
Do checkout
repo.git.checkout(branch)
repo.git.checkout(commit_hash)
repo.git.checkout(tag_name)
Do cherry-pick commit hash 'fff1234'
repo.git.cherry_pick('fff1234')
You can refer to this URL for more details https://titanwolf.org/Network/Articles/Article?AID=0f484197-1dc3-4676-82c8-5359fad01f33#gsc.tab=0
I have a project with a requirements.txt resembling this:
-e git+https://some.gitlab.com/some_group/some_repo#egg=repo
selenium
pywinauto
I made a source secret on OpenShift with my username and password and started the build. Cloning the project goes through, but cloning some_repo fails with an Error: "Can't find Username".
I'm a bit confused because the main project was successfully cloned with the credentials provided in the secret, but it doesn't seem like Pip is reusing those.
What's more confusing is that OpenShift seems to store the credentials in a .gitconfig file, that should be known to Pip:
I0107 15:35:14.756570 1 password.go:84] Adding username/password credentials to git config:
# credential git config
[credential]
helper = store --file=/tmp/gitcredentials.324456941
Any idea ?
P.S. I wanted to try with an SSHKey but for some reason the admins doesn't want to enable this option on the company's GitLab. And I don't want to put some credentials in the url inside the requirements.txt.
Edit : I have no problem with this on my workstation
pip expects you to add the username and password as a part of the URL if you are not using ssh keys. You could set the secrets as environment variables and refer to them in your pip.conf.
[global]
index = https://$username:$password#some.gitlab.com/some_group/some_repo
I have my Django project in a Bitbucket repo that I want to merge with my newly created Digital Ocean server. The server directory contains a few basic files for a Django project for my server like env, static and manage.py. The Bitbucket repo has all my project apps etc. I've already done git remote add origin https://user#bitbucket.org/user/project.git from the server directory. So I then perform git merge master but it says Already up-to-date.. I've tried git pull master but it says:
fatal: 'master' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Any idea what I can try?
The git pull command is used like
git pull [options] [<repository> [<refspec>...]]
That means, that you if you are not using any [options] you use
git pull <repository>
which in your case would end up being
git pull origin
If you want to specify the branch then you can do it like this (for master)
git pull origin master
The command you used (git pull master) will not work, because you are telling git to pull from the repository master, which does not exist in your remotes. You can already tell my the error message 'master' does not appear to be a git repository.
If you want to get the changes and merge yourself you can use the git fetch command, which is being used my pull anyways.