Skip to content

Cannot push branch upstream #1408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
montmejat opened this issue Feb 8, 2022 · 6 comments
Closed

Cannot push branch upstream #1408

montmejat opened this issue Feb 8, 2022 · 6 comments

Comments

@montmejat
Copy link

Hello,

I have a strange issue that I have not been able to resolve... It seems to maybe come from my SSH key, but I don't have any issues with it (git in the terminal works fine).

Here's what my code looks like:

git_repo = GitRepo.init(dataset_dir)
git_repo.create_remote("origin", url=git_remote_url)

git_repo.index.add(some_files)

git_repo.index.commit("Initial commit")
git_repo.git.push("--set-upstream", "origin", "master")

But at the last line, it breaks with this error:

git.exc.GitCommandError: Cmd('git') failed due to: exit code(128)
  cmdline: git push --set-upstream origin master
  stderr: 'GitLab: The project you were looking for could not be found.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.'

Using git push --set-upstream origin master works fine in the terminal. What is going wrong here? Thanks!

@montmejat
Copy link
Author

One weird thing: restarting my computer or reinstall gitpython using pip helps...

@Byron
Copy link
Member

Byron commented Feb 9, 2022

One weird thing: restarting my computer or reinstall gitpython using pip helps...

Without knowing what caused the issue, I assume that it's resolved now. There can be a variety of issues with GitPython not being able to use ssh keys as these usually aren't authorized for use. The only way to control this is like so:

ssh_cmd = 'ssh -i id_deployment_key'
with repo.git.custom_environment(GIT_SSH_COMMAND=ssh_cmd):
    git_repo.git.push("--set-upstream", "origin", "master")

(the above is a modified sample from the GitPython tutorial).

Even though I am closing this issue as there is nothing actionable right now, please feel free to keep commenting if there is anything else that might change that.

@Byron Byron closed this as completed Feb 9, 2022
@montmejat
Copy link
Author

montmejat commented Mar 23, 2022

I'm still getting the same error with the code you provided, and sometimes, reinstalling GitPython helps, but not always. Would you have any suggestions as to what I can do to try to debug this?

@montmejat
Copy link
Author

montmejat commented Mar 23, 2022

I'm still getting the same error with the code you provided, and sometimes, reinstalling GitPython helps, but not always. Would you have any suggestions as to what I can do to try to debug this?

Some more information:

  • It also crashed with the same error when trying this (my guess is just that at some point, anything I do crashes):
new_tag = git_repo.create_tag(tag_name)
git_repo.remotes.origin.push(new_tag)
  • I just tried an easy fix, and it works:
os.system("cd /my/git/repo && git push --set-upstream origin master")

Maybe it's because of something I did before that is just braking it?

@Byron
Copy link
Member

Byron commented Mar 24, 2022

@aurelien-m Just for completeness, does git_repo.git.push("--set-upstream", "origin", "master") work the same as the os.system(…) invocation proposed above? It is expected to, but offer more convenience and potentially better error reporting.

@montmejat
Copy link
Author

montmejat commented Mar 24, 2022

Thanks for coming back to me @Byron. Yes, I expect it to behave the same, and it does indeed behave identically.

After a few tests, the following code:

os.system("cd /my/git/repo && git push --set-upstream origin master")

Returns the following error (not all the time, I'm not able to find what causes this issue):

GitLab: The project you were looking for could not be found.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

So using os.system(...) and git_repo.git.push(...) have the same effect and seem to crash identically.

I have finally added a simple time.sleep(1) before my unstable code, and it now seems to be much much more stable. After a few tests, I have not had any errors like before. Maybe I'm just having a networking latency issue, and it just needs some time to refresh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants