Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python隐藏git更改、切换分支、提交文件、切换回和撤消隐藏_Python_Git_Gitpython - Fatal编程技术网

使用python隐藏git更改、切换分支、提交文件、切换回和撤消隐藏

使用python隐藏git更改、切换分支、提交文件、切换回和撤消隐藏,python,git,gitpython,Python,Git,Gitpython,我将自动向git存储库的特定分支添加大量文件。我想确定我不会给自己造成重大问题 问题是我有一个代码库,我用它运行了数百个实验。我希望结果自动存储到它们自己的分支,同时使主分支不受影响(即主分支不会跟踪实验结果)。我对stash命令不太熟悉,我想确定我是否正确使用了它 import subprocess from git import Repo # Stash changes and switch to result_branch subprocess.run(["git",

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

问题是我有一个代码库,我用它运行了数百个实验。我希望结果自动存储到它们自己的分支,同时使主分支不受影响(即主分支不会跟踪实验结果)。我对
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", "checkout", "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??

我所采用的方法是否有效,或者我是否有可能给自己造成各种存储库问题?

是否要为每个结果创建单独的分支?还是要将迭代的每个结果存储在相同的
result\u分支中
。另外,您希望创建这些分支的原因是什么?用例是什么?您是否考虑过git patch?@或Y-所有结果都保存到同一个分支。我想创建它,这样我就可以跨机器共享结果,但在完成后就可以轻松地删除它们(删除分支),而不会使实际代码开发的历史变得复杂。我建议避免在任何类型的自动化脚本中使用
git stash
。该命令有太多奇怪的角落情况,您必须做一些特殊的事情。根据您使用的Python包装器,它可能会对这些问题进行一些补偿,但总的来说,这似乎是个坏主意。使用
git-worktree
添加一个单独的工作树,在其中您可以完全控制所有内容,因为没有其他人使用该工作树,这是一个更好的方法。@user1245262听起来您可以使用
git-patch
。然后,您可以为每个结果创建修补程序并将其保存到修补程序文件中,您可以在需要时应用修补程序文件中的更改,然后仅签出它们。当您不再需要这些结果时,只需删除补丁文件。