GitPython push to remote repo - python

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.

Related

GitPython Read a branch without it existing locally or cloning

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?

error: failed to push some refs to when pushing with a certain command

I keep getting this error when I do git push -u origin master
. git push works but that doesn't It's stuck with heroku too. When I do git push heroku master I get this same error message. All the answers on Stack Overflow say that it's because I didn't commit first but I did commit. I don't know what's wrong. If I don't solve this, I won't be able to follow along with my book and I'm tired of using the heroku GUI to push everything.
First thing: never use git push only until and unless you are 100% sure of which branch you are pushing to.
GitHub has a concept of default remote branch. so when you do git push you are actually pushing the code to the default branch.
You can check the default branch for your GitHub repository as below:
Now coming to your doubt
If the branch you are currently in on local is different (other than the one you want to push to remotely) lets say main, then if you do git push origin main while being on the master branch, Git will try to push your local master branch to the remote master branch, and if local master branch is behind the remote master branch in terms of commit or if their is any discrepancies (maybe someone forced push something or did a rebase), then you will get the issue error: failed to push some refs to when pushing with a certain command
So there are two resolutions to it:
recommended: Change to the branch you want to push to remote repository, pull the changes from remote, resolve the conflicts if any, do the rebase and then push it to the remote
If you are 100% sure that your code from local is correct and you want to replace the remote branch completely with your local branch you can do a force push as git push origin <BRANCH_NAME> -f. But this is too dangerous, at times it can lead to code loss especially in big projects where multiple developers are working on the same repo

How to update personal cloned github?

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.

How to push to remote repo with GitPython

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')

Using ac2git followed by bitbucket

I am using ac2git to convert my accurev depot to git repository. I am able to successfully make the conversion however when I follow the steps after creating the new repository I am unable to push the changes representing the accurev transactions that are now commits.
What I mean is I loose the history, I am only able to see the hist and the diff files when I check the commit options on bitbucket.
I followed the following steps:
python ac2git.py
cd existing-project
git add --all
git commit -m "Initial Commit"
git remote add origin http://****#bitbucket.******.git
git push -u origin master
I am new to bitbucket so I am not sure what the problem is? Has anyone tried this accurev->git->bitbucket before?
In other words, how do I move my git repository on my local created as a result of ac2git to a new repository on bitbucket ?
Your first step is to create a repository in BitBucket. Then you simply need to follow BitBucket's instructions:
cd /path/to/my/repo
git remote add origin http://****#bitbucket.******.git
git push -u origin --all # pushes up the repo and its refs for the first time
git push origin --tags # pushes up any tags
I'm not 100% certain that this is what has happened but it is worth noting that the ac2git script stores a lot of metadata under refs/ac2git/* refs, including a refs that will store a hist.xml, streams.xml and diff.xml in their commit history for each of your stream's transactions. See how_it_works.md which explains about refs/ac2git/depots/<depot_number>/streams/<stream_number>/info.
Though this ref acts as a branch, it shouldn't have been pushed up by invoking git push origin -u -all since the documentation claims that this only pushes things under refs/heads/. However, in case of errors the script may not correctly checkout a branch that was converted and it may leave your local repository in a detached HEAD state where it will have actually checked out one of the internal refs.
I don't know what a git push origin -u --all would do in this case for a brand new Bitbucket repo but if it had pushed your HEAD ref up to the repo then you would get this metadata on the remote.
However, for now this is just a theory and hopefully someone will be able to use this information to piece together a more clear solution for you.

Categories

Resources