我正在尝试通过Python子进程运行git命令。我通过调用github cmd目录中的git.exe来实现这一点。
我成功地让大多数命令(init、remote、status)正常工作,但是在调用git add时出现错误。到目前为止,这是我的代码:
import subprocess
gitPath = 'C:/path/to/git/cmd.exe'
repoPath = 'C:/path/to/my/repo'
repoUrl = 'https://www.github.com/login/repo';
#list to set directory and working tree
dirList = ['--git-dir='+repoPath+'/.git','--work-tree='+repoPath]
#init gitt
subprocess.call([gitPath] + ['init',repoPath]
#add remote
subprocess.call([gitPath] + dirList + ['remote','add','origin',repoUrl])
#Check status, returns files to be commited etc, so a working repo exists there
subprocess.call([gitPath] + dirList + ['status'])
#Adds all files in folder (this returns the error)
subprocess.call([gitPath] + dirList + ['add','.']
我收到的错误是:
fatal: Not a git repository (or any of the parent directories): .git
我搜索了这个错误,大部分的解决方案都与不在正确的目录中有关。所以我猜也是这个问题。然而,我不知道为什么会有这个问题。Git status 返回的是目录中的正确文件,并且我已经设置了 --git-dir 和 --work-tree。
如果我进入 git shell,添加文件就没有问题,但我无法找出为什么事情会出错。
我不想使用Python的Git库来解决这个问题。