使用python存储git更改,切换分支,提交文件,切回并撤消存储

问题描述

我将自动向git存储库的特定分支中添加大量文件。我想确定自己不会给自己造成重大问题。

问题是我有一个可以运行数百个实验的代码库。我希望将结果自动存储到其自己的分支中,同时又不影响master分支(即master分支不会跟踪实验结果)。我对stash命令的熟悉程度不如我所希望的,并希望确定自己正确使用了它。

import subprocess
from git import Repo

# Stash changes and switch to result_branch
subprocess.run(["git","stash"])
subprocess.run(["git","checkout","result_branch"],check=True) 

#add_results_to_repo() -- Calls method that finds result files and uses Repo.git.add to add them to repo
#git_commit() -- Calls method that uses Repo.git.commit to commit branches

# Return to master branch and undo stash
subprocess.run(["git","master"],check=True)
subprocess.run(["git","stash","pop"],check=True)

我使用subprocess来切换分支,因为我在使用Repo时遇到了麻烦。我使用subprocess进行隐藏,因为我很懒,并且对subprocess.run很熟悉。也许:

 repo.git.stash() # To create stash,and
 repo.git.stash('pop') # To restore stash??

我采用的方法是有效的方法,还是要冒给自己造成各种存储库问题的风险?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)