GitPython nothing appears in working copy after pull

有些话、适合烂在心里 提交于 2019-12-08 04:34:20

问题


I'm new in PythonGit and I have problem with pulling and pushing. I created locally bare repo and pushed to it an initial commit. After that I tried to init new user repo using PythonGit, fetch it and pull from it. I have no problem with initialize the repo however I can't get anything from remote/bare repo. My code:

import git

repo = git.Repo.init('.')
origin = repo.create_remote('origin', '/home/paweber/git/my-repo.git')
origin.fetch()            
repo.create_head('master', origin.refs.master).set_tracking_branch(origin.refs.master)
origin.pull()

In ipython console for fetch and pull I get:

In [5]: origin.fetch()
Out[5]: [<git.remote.FetchInfo at 0x7f4a4d6ee630>]

for fetch and

In [6]: origin.pull()
Out[6]: [<git.remote.FetchInfo at 0x7f4a4d6e6ee8>]

for pull. After pull action, nothing is pulled at all and repo is still empty but exists. What I'm doing wrong?


回答1:


pull() doesn't do anything as master is already at its destination commit, the one pointed to by origin/master.

This code will work as expected:

import git

repo = git.Repo.init('.')
origin = repo.create_remote('origin', '/home/paweber/git/my-repo.git')
origin.fetch()
# the HEAD ref usually points to master, which is 'yet to be born'            
repo.head.ref.set_tracking_branch(origin.refs.master)
origin.pull()



回答2:


I don't know how properly resolve this problem but the only idea is as phobic say to reset hard repo after create_head.

import git

repo = git.Repo.init('.')
origin = repo.create_remote('origin', '/home/paweber/git/my-repo.git')
origin.fetch()            
repo.create_head('master', origin.refs.master).set_tracking_branch(origin.refs.master)
origin.pull()
repo.head.reset('--hard')

After that all further pulls should work properly.



来源:https://stackoverflow.com/questions/28209329/gitpython-nothing-appears-in-working-copy-after-pull

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!