标题已经非常清楚了。使用 GitPython 模块,运行相当于在终端上运行 git reset --hard
的 Python 代码是什么?
您可以使用:
repo = git.Repo('c:/SomeRepo')
repo.git.reset('--hard')
或者如果你需要重置到特定的分支:
repo.git.reset('--hard','origin/master')
如果您想强制更新存储库到origin/master(警告,这将删除当前的更改):
# 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()
您可以使用:
repo = git.Repo('repo')
# ...
# Remove last commit
repo.head.reset('HEAD~1', index=True, working_tree=True)