Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a git reset --hard using gitPython?

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

like image 776
Anuvrat Parashar Avatar asked Aug 08 '12 12:08

Anuvrat Parashar


People also ask

How do I completely reset git?

To hard reset files to HEAD on Git, use the “git reset” command with the “–hard” option and specify the HEAD. The purpose of the “git reset” command is to move the current HEAD to the commit specified (in this case, the HEAD itself, one commit before HEAD and so on).

Does git reset hard delete files?

git reset --hard is a classic command in this situation - but it will only discard changes in tracked files (i.e. files that already are under version control). To get rid of new / untracked files, you'll have to use git clean !

What does git reset -- hard master do?

git reset --hard origin/master works only as a full wipe out if you are in a local branch. If you are in the master branch instead, and if you have made changes, you can only drop all of the files that you made or changed. You cannot drop the folders that you added.


1 Answers

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()
like image 59
blented Avatar answered Sep 21 '22 11:09

blented