如何使用gitPython进行git reset --hard?

Anu*_*har 11 python git gitpython

那么标题是自我解释的.相当于git reset --hard使用GitPython模块运行(在终端上)的python代码是什么?

ble*_*ted 16

您可以使用:

repo = git.Repo('c:/SomeRepo')
repo.git.reset('--hard')
Run Code Online (Sandbox Code Playgroud)

或者,如果您需要重置为特定分支:

repo.git.reset('--hard','origin/master')
Run Code Online (Sandbox Code Playgroud)

或者在我的情况下,如果你想简单地将回购更新为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()
Run Code Online (Sandbox Code Playgroud)


lar*_*sks 6

我搜索reset文档,发现这个:

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

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

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


Nel*_* G. 5

您可以使用:

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