Dek Dekku
Dek Dekku

Reputation: 1461

How do I revert a commit with gitpython?

For a python script I'm writing (using Python 2.7 on Windows 7) I should be able to modify a branch with a given commit, that is adding it (cherry pick) if the commit is missing, or reverting it if it's already present.

Apparently revert has not been wrapped in gitpython's Repo class, so I tried to use Git directly with:

repo.git.revert(reference)

where reference is one of the commits returned by repo.iter_commits("master")

What happens is that the script locks on that command and becomes idle; I then have to kill the command prompt window. If I go in the working directory and explore the repository, I can see (with git diff) that after the execution, the changes have been applied even tho' no new commit is visibile if I git log.

Any ideas about if and what I'm doing wrong?

Upvotes: 1

Views: 3208

Answers (1)

Dek Dekku
Dek Dekku

Reputation: 1461

I solved the mistery by trying to git commit the applied changes manually. Git complained about a swap file in the working directory.

So, the problem was that the command was being executed as if it was run from a terminal, hence waiting for me to somehow edit the commit message! So I needed to run the revert command with the no-edit option.

I changed the method invocation to:

repo.git.revert(reference.hexsha, no_edit = True)

(notice that gitpython requires the underscore as a separator. Also, using explicitly the hexsha property is not required, since reference would be converted to its str() representation anyway.)

It seems to work.

Upvotes: 1

Related Questions