How to do a git reset --hard using gitPython?

早过忘川 提交于 2019-12-05 16:29:00

问题


Well the title is self explanatory. What will be the python code equivalent to running git reset --hard (on terminal) using GitPython module?


回答1:


I searched for reset in the documentation and found this:

class git.refs.head.HEAD(repo, path='HEAD')

reset(commit='HEAD', index=True, working_tree=False, paths=None, **kwargs)

Reset our HEAD to the given commit optionally synchronizing the index and working tree. The reference we refer to will be set to commit as well.




回答2:


You can use:

repo = git.Repo('c:/SomeRepo')
repo.git.reset('--hard')

Or if you need to reset to a specific branch:

repo.git.reset('--hard','origin/master')

Or in my case, if you want to just hard update a repo to origin/master (warning, this will nuke your current changes):

# blast any current changes
repo.git.reset('--hard')
# ensure master is checked out
repo.heads.master.checkout()
# blast any changes there (only if it wasn't checked out)
repo.git.reset('--hard')
# remove any extra non-tracked files (.pyc, etc)
repo.git.clean('-xdf')
# pull in the changes from from the remote
repo.remotes.origin.pull()



回答3:


You can use:

repo = git.Repo('repo')
# ...
# Remove last commit
repo.head.reset('HEAD~1', index=True, working_tree=True)


来源:https://stackoverflow.com/questions/11864735/how-to-do-a-git-reset-hard-using-gitpython

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