How to do git reset -hard using gitPython?

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

+7
source share
3 answers

I searched 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 for this commit, not necessarily synchronizing the index and the working tree. The link we are referring to will also be set to commit.

+6
source

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 just want to update the repo to the source / wizard (warning, this will immediately change 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() 
+10
source

You can use:

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

All Articles