How to do a git reset --hard using gitPython?
Asked Answered
G

3

17

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

Glossographer answered 8/8, 2012 at 12:40 Comment(0)
B
7

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.

Barbiturism answered 8/8, 2012 at 12:46 Comment(0)
W
32

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()
Wame answered 14/6, 2014 at 19:20 Comment(0)
B
7

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.

Barbiturism answered 8/8, 2012 at 12:46 Comment(0)
K
5

You can use:

repo = git.Repo('repo')
# ...
# Remove last commit
repo.head.reset('HEAD~1', index=True, working_tree=True)
Knickerbocker answered 24/11, 2016 at 7:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.