如何使用gitPython执行git reset --hard操作?

17

标题已经非常清楚了。使用 GitPython 模块,运行相当于在终端上运行 git reset --hard 的 Python 代码是什么?

3个回答

29

您可以使用:

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

6

我在文档中搜索了reset,找到了这个

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

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

将我们的 HEAD 重置为给定的提交,可选择同步索引和工作树。我们所指的引用也将设置为提交。


5

您可以使用:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接