I'm trying to read a branch from a Git repository without having a local copy or cloning the repo.
The GitPython API seems to assume a repo either exists locally or you with to clone it to read it.
All i'd like to do is access all the commits from a branch, for a repo, without it existing locally.
I'm after something like this:
branch = git.Repo(https://github.com/MyOrg/MyRepo, 'master')
for commit in branch.commits:
print(commit)
I'm looking at the API and I just don't see anything like this.
Is this possible?
Related
So I am trying to push my code from a local repo to the Origin branch of a Git Repo and I am having trouble. Every time I try to push my code it says my Repo isn't valid.
repo = git.Repo.init("C:\\Documents\\testfolder")
origin = repo.create_remote('origin', "https://github.com/my_username/testrepo.git")
repo.index.add('README.md')
repo.index.commit("My commit message")
repo.git.push("--set-upstream","origin","new_branch")
I tried looking at the documentation but found it a little confusing and this is what I understood from it. What I am trying to do is push all of the files from my local repository on my disk to a remote repository on github.
For automating some of our workflows I am implementing some Python scripts which create project templates. The content of a project template is located in a folder which also is a local git repository. So the project in the end will be linked and pushed to a remote repository on our company server. Each project additionally uses some of our own created libraries which are added as a git submodule into the project folder and is part of the project git repository.
So my script should now first create the local project git repository which I do by using Gitpython with the following code:
Repo.init(os.path.join(sRepoPath, '.git'), bare=True)
After the repository is created, I want to add one or more remote repositories as a submodule and then clone them into the project folder. But I can't find out how to do it from the Gitpython documentation. It does not cover such a case with remotes as a submodule.
I've already tried several code combinations from the Gitpython documentation. Something like (of course not working):
mainRepo = Repo(sDestinationRepoPath)
test_remote = mainRepo.create_remote('submodule', "ssh://git#bb.mycompany.corp:1234/fwlib/system_lib.git")
mainRepo.create_submodule(test_remote)
But I can't sort it out how it should be done. Can anyone tell me how to do it?
I recently downloaded an open-source project from github into my server using
$ git clone www.github.com/project_url
Then I added comments and descriptions inside the multiple files here and there.
When I went to the github repository of the original project github page,
many of the files were updated. But I'm hesitating to clone again because I think it would just overwrite every local files to which I wrote many of the comments.
Is there anyway I can download up-to-date files in the github,
but leave the comments that I added not being removed at the same time?
There are multiple strategies to go around this problem. A strategy is already explained. Another could be
Commit your changes (git commit -am <message>)
Do a git pull (git pull -r)
Resolve conflicts if any
Also, you can keep your changes separate in a separate branch. Here is what else can be done:
Create a separate branch in your local repository (git checkout -b <branch-name>, eg. git checkout -b new-branch)
commit changes in that new branch (git commit -am <message>)
Either you can merge now from your new branch to the old branch
git checkout old-branch; git merge new-branch.
resolve conflicts if any
OR
Raise a Pull Request after resolving any conflicts if any
You can use git stash for this. This will store everything you changed locally. Then you pull the changes from the original repository and then with 'git stash pop' your local changes will come back.
git stash
git pull
git stash pop
Conflicts can come up and need to be resolved.
I have to clone a set of projects from one repository and push it then to a remote repository automatically. Therefore i'm using python and the specific module GitPython. Until now i can clone the project with gitpython like this:
def main():
Repo.clone_from(cloneUrl, localRepoPath)
# Missing: Push the cloned repo to a remote repo.
How can i use GitPython to push the cloned repo to a remote repo?
it's all in the documentation:
repo = Repo.clone_from(cloneUrl, localRepopath)
remote = repo.create_remote(remote_name, url=another_url)
remote.push(refspec='{}:{}'.format(local_branch, remote_branch))
see also the push reference API. You can avoid the refspec setting if you set a tracking branch for the remote you want to push to.
It should work like this
r = Repo.clone_from(cloneUrl, localRepoPath)
r.remotes.origin.push()
provided that a tracking branch was setup already.
Otherwise you would set a refspec:
r.remotes.origin.push(refspec='master:master')
I'm not familiar with Git, I only know how to download a master or dev branch of a python software project from Github and install into my system (I do not really use Git locally).
Now there is a "pull request" by a GitHub user which contains a feature I find useful but not in the main or dev branch. How can I use that in my local copy?
And if I manage to include it into my local copy, would it be lost if I later updated to the latest maaster/dev branch?
see: Git fetch remote branch
git fetch
git checkout <the name of the remote branch>
You can revert to any commit anytime you want, those changes are kept in the repository so you can visit different versions of it during the development on all branches. You need to do a merge command, which if, there's not any conflict with the patch contained in the pull request, will combine the code of the patch with the code of the repository. If there's conflicting code, like shared files for example, you will have to do the proper refactors and select which changes get inside or not.
Here you have basic information about merging different branches:
http://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging
Updating will only do a merge with the changes and your master. If you break something you can stash the changes and get to the original state.
Also I would suggest to checkout a fresh branch and apply that feature on the freshly created branch. If every thing works fine merge it to your master otherwise delete and forget it :P
if you dont know the branch name
use git fetch first to know the branch name
and then git checkout
Also maybe you can find this link useful