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

前端 未结 3 1606
伪装坚强ぢ
伪装坚强ぢ 2021-02-19 09:47

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

3条回答
  •  盖世英雄少女心
    2021-02-19 10:18

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

提交回复
热议问题