第二个过程在第一个过程完成之前开始。当第二个 git 进程启动时,git 会识别出同一目录中已经有一个 git 进程在运行,这可能会导致问题,因此会出错。如果您从第一个进程读取错误流,您将看到如下内容:
fatal: Unable to create 'path/to/dir/.git/index.lock': File exists.
If no other git process is currently running, this probably means a
git process crashed in this repository earlier. Make sure no other git
process is running and remove the file manually to continue.
如果您在开始第二个之前等待第一个完成,那应该可以。像这样的东西:
def workingDir = new File("path/to/dir/")
def p = "git add .".execute(null, workingDir)
p.waitFor()
p = "git reset --hard".execute( null, workingDir )
p.text.eachLine {println it}
println p.exitValue()