How to use git commands without human interaction?

时间:2017-01-19 15:24:18

标签: git repository bitbucket

I have a remote machine with a program which downloads some source codes from some repos using a only-read svn user for that program. Now is working with svn without human interaction doing thinks like this:

Doing checkouts:

p = Runtime.getRuntime().exec("svn checkout --non-interactive --trust-server-cert --username "+Constants.svnUser+" --password "+Constants.svnPassword+" "+repositoryURL+" ." , null , dir);

Updating code:

p = Runtime.getRuntime().exec("svn update --non-interactive --trust-server-cert --username "+Constants.svnUser+" --password "+Constants.svnPassword+" ", null , dir);

It works perfectly, but now I'm trying to download source code from Git repositories using a read only git user, and I can't find the way to do the same non interactive behaviour with git.

Given a git repo, a git user and a git password, how to init git and clone, checkout or pull source code without human interaction?

Can't find any info about how to achieve that give these three items.

Thank you so much by advance.

2 个答案:

答案 0 :(得分:0)

Git命令没有--non-interactive开关,您只需要确保不以可交互的方式调用命令。这个e。 G。表示您不应使用git pull,因为这会根据您的配置和参数执行mergerebase,这可能会导致需要手动解决的冲突。相反,您应该使用git fetch,然后使用一个命令来替换工作树,该命令来自遥控器,如git reset --hard master origin/master或类似的东西。

您可能会从http://gitolite.com/deploy.html获得一些关于您想要实际使用哪些命令的指示。

答案 1 :(得分:0)

从 git v2.3.0 开始,您可以设置环境变量 GIT_TERMINAL_PROMPT=0 以避免您的脚本挂在需要手动身份验证的 git 命令上。

示例:

$ GIT_TERMINAL_PROMPT=0 git clone https://git.example.com/my/repo.git
Cloning into 'repo'...
fatal: could not read Username for 'https://git.example.com': terminal prompts disabled
$ echo $?
128

来源: man 1 git