How to use Git commands without human interaction?
Asked Answered
I

2

7

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?

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

Intemperate answered 19/1, 2017 at 15:24 Comment(0)
R
8

From git v2.3.0, you can set the environment variable GIT_TERMINAL_PROMPT=0 to avoid your script to hang on the git command expecting manual authentication.

Example:

$ 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

Source: man 1 git

Rhoades answered 11/6, 2021 at 22:14 Comment(0)
P
3

I often use --no-pager flag, for example to directly output the diff in terminal:

$ git --no-pager diff
Peaceful answered 29/11, 2022 at 12:13 Comment(1)
if you redirect the command output to a file, it sets "no-pager" automatically I supposeLaze

© 2022 - 2024 — McMap. All rights reserved.