简体   繁体   English

如何使用gitpython还原提交?

[英]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. 对于我正在编写的python脚本(在Windows 7上使用Python 2.7),我应该能够使用给定的提交修改分支,如果缺少提交,则将其添加(樱桃选择),如果已经存在,则将其还原。

Apparently revert has not been wrapped in gitpython's Repo class, so I tried to use Git directly with: 显然,revert尚未包装在gitpython的Repo类中,因此我尝试将Git直接用于以下内容:

repo.git.revert(reference)

where reference is one of the commits returned by repo.iter_commits("master") 其中referencerepo.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 . 如果我进入工作目录并浏览存储库,我可以看到(使用git diff )执行后,即使我使用git log即使没有新提交,更改也已应用。

Any ideas about if and what I'm doing wrong? 关于我是否做错什么的任何想法?

I solved the mistery by trying to git commit the applied changes manually. 我通过尝试手动git commit应用的更改来解决问题。 Git complained about a swap file in the working directory. Git抱怨工作目录中的交换文件。

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. 因此,我需要使用no-edit选项运行revert命令。

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.) (请注意, gitpython需要使用下划线作为分隔符。此外,不需要显式使用hexsha属性,因为无论如何reference都将转换为其str()表示形式。)

It seems to work. 似乎有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM