I have two repositories on github, using gitpython I'm trying to push a file from one repository to another remote repository. I've managed to do it using git but struggling with the gitpython code.
git remote add remote_to_push git#bitbucket...
git fetch remote_to_push
git checkout remote_to_push/master
git add file_to_push
git commit -m "pushing file"
git push remote_to_push HEAD:master
I've managed to create a repo object of the remote I think with the following
from git import Repo
repo = Repo('path/to/other/git/repo')
remote = repo.remotes.origin
I can't figure out how to add something to then push, if i call
remote.add("file_to_push")
Then I get errors about the create() function
TypeError: create() takes exactly 4 arguments (2 given)
Trying to follow what they have done in How to push to remote repo with GitPython with
remote.push(refspec='{}:{}'.format(local_branch, remote_branch))
I assume it should work with using master and master as the remote branches as they both must exist but it's giving me the error
stderr: 'error: src refspec master does not match any.'
Thanks
Solved it.
First created a remote of the other repo
git remote add remote_to_push git#bitbucket...
Then the gitpython code
from git import Repo
repo = Repo('path/to/other/git/repo') #create repo object of the other repository
repo.git.checkout('remote_to_push/master') #checkout to a branch linked to the other repo
file = 'path/to/file' #path to file to push
repo.git.add(file) # same as git add file
repo.git.commit(m = "commit message") # same as git commit -m "commit message"
repo.git.push('remote_to_push', 'HEAD:master') # git push remote_to_push HEAD:master
Aside from the docs, I found the following to be quite helpful if anyone's struggling with gitpython as I found it quite a pain
Python Git Module experiences?
http://sandlininc.com/?p=801
Git push via GitPython
How to push to remote repo with GitPython
Related
I want to create a python notebook in databricks that will do the following -
Connect to Azure Devops Git repo
Make couple of changes in a yaml file
Commit the changes in the master branch
Push the changes back to repo
I tried the below code to achieve step 1:
import git
repo_url = <repo url>
pat_token = <token>
dbfs_path ='<dbfs_path>'
repo = git.Repo.clone_from(repo_url, dbfs_path, branch='master')
But I got the below error:
GitCommandError: Cmd('git') failed due to: exit code(128)
cmdline: git clone --branch=master -v <repo_url> <dbfs_path>
stderr: 'Cloning into <dbfs_path>...
fatal: could not read Username for <repo_url>: No such device or address
I am not sure where to provide the username.
i am not able to add a file
to git via windows 10 and also it is giving me this error
for checking the status also
fatal: not a git repository (or any of the parent directories): .git
make sure the remote url exists in the directory where you are trying to add the file.
Run the following command:
git remote -v
If it still stays `fatal: not a git repository (or any of the parent directories): .git
`
then it means git is not configured to your code.
git init
git remote add origin <HTTPS URL FOR GIT REPO>
then you will be able to add the file
I have a django project under development on my windows computer (dev-machine). I am using pyCharm for development.
I have set up a server (server-machine) running ubuntu. And now want to push my project to the server.
So in my project folder on the dev-machine I have done the git init:
$ git init
$ git add .
$ git commit -m"Init of Git"
And on the server-machine I have made a project folder: /home/username/projects
In this folder I init git as well
$ git init --bare
Back on my dev-machine, I set the connection to the server-machine by doing this
$ git remote add origin username#11.22.33.44:/home/username/projects
And finally pushing my project to server-machine by typing this command on my dev-machine
$ git push origin master
It starts to do ome transfer. And here's the problem.
On the server-machine when I check what's been transferred, it's only stuff like this
~/projects$ ls
branches config description HEAD hooks info objects refs
Not a single file from the project is transferred. This looks much like what the .git folder contains on the dev-machine.
What am I doing wrong?
What you see is the directory structure git uses to store your files and meta data. This is not a checked-out copy of the repository.
To check whether the data made it into the repository use git log in ~/project
Okay, so I understand now where I went wrong.
With git push I am not setting up the project.
To set up the project I need to do git clone.
This is how I did it.
1.
So I made a folder for git repositories on the server-machine. I called it /home/username/gitrepos/
2.
Inside there, I made a folder for my project, where I push the git repository into. So path would look like this for me /home/username/gitrepos/projectname/
3.
Being inside that folder I do a 'git init' like this
$ git init --bare
4.
Then I push the git repo to this location. First setting the remote adress from my dev-machine with this command. If adding a remote destination new use this:
$ git remote set nameofconnection username#ip.ip.ip.ip:/home/username/gitrepos/projectname
if changing the adress for a remote destination use this:
$ git remote set-url nameofconnection username#ip.ip.ip.ip:/home/username/gitrepos/projectname
To se with remote destinations you have set type this:
$ git remote -v
5.
Now go back to server-machine and clone the project into a project folder. I made a folder like this /home/username/projects/
When being inside that folder I clone from the gitrepo ike this:
$ git clone /home/username/gitrepos/projectname
Thank you all for the help! <3
I'm trying to initialize a git repo and then serve it using the git daemon. Everything with GitPython.
Initializing the repo works:
temp_dir = '/tmp/something'
repo = git.Repo.init(temp_dir)
Starting the daemon as well:
gd = Git().daemon(temp_dir, enable='receive-pack', listen='127.0.0.1', port=GIT_DAEMON_PORT,as_process=True, export_all=True)
gd.proc.wait()
But I'm not able to access the repo:
git clone git://127.0.0.1:9418/something
Cloning into 'something'...
fatal: remote error: access denied or repository not exported: /something
I'm not sure if I have to initialize the repo as bare, or if I have to specify the base_path when starting the git daemon... tried it all. Does anybody have some pointers?
PS: I've seen a similar approach here:
https://github.com/gitpython-developers/GitPython/blob/master/git/test/lib/helper.py
Figured it out, Thanks to user3159253.
import git
import tempfile
tmpdir = tempfile.TemporaryDirectory(suffix='.git')
repo = git.Repo.init(tmpdir.name, shared=True, bare=True)
repo.daemon_export = True
gd = git.Git().daemon(tmpdir.name,
enable='receive-pack',
listen='127.0.0.1',
port=9418,
as_process=True,
verbose=True
)
gd.proc.wait()
Now you can clone that repo using:
$git clone git://127.0.0.1:9418/tmp/<name-of-tmpdir>.git
I'm looking at a flask app which I would like to import into an openshift project (https://github.com/lpolepeddi/intro-to-flask) . This contains a a series of 'checkpoints'(branches) as part of a tut. I want to grab the final code which is at https://github.com/lpolepeddi/intro-to-flask/tree/20_visibility_control for the starting point of a project . Is the a way to get a unique git url for this branch of the form
https://github.com/lpolepeddi/intro-to-flask.git
So that I can pull it in with a command like:
git remote add upstream -m master https://github.com/shekhargulati/flask-login-openshift-quickstart.git
Although as far as I know "single branch GitHub url" doesn't exist, you could just clone entire repo and later change branch:
git clone https://github.com/lpolepeddi/intro-to-flask.git
cd intro-to-flask
git checkout 20_visibility_control
or as stated in this answer, clone only one branch:
git clone --branch 20_visibility_control --single-branch https://github.com/lpolepeddi/intro-to-flask.git