Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple-git requesting username and password on each git command

Here is the problem https://www.npmjs.com/package/simple-git. I am using npm module simple-git to work with a local git-repository. The local git repository is already checkout locally and I can work with the repository via git-bash (commit/push/pull all works). In the same time when any command performed on the same repository via simple-git requires me to enter username and password each time.

How can I make simple-git use my existing credentials and stop asking me for username and password on each git command ?

PS When simple-git is initialized I simply provide the path to the repository. This should be enough and it should use the .git/config file. Maybe something in this .git/config need to be / can be set to help it stop asking for username and password.

like image 309
Hivaga Avatar asked May 25 '26 10:05

Hivaga


1 Answers

The recommended way is to clone the repository with the credentials (so the remote will already contain the username and password).

If the repository is already cloned and you don't want to mess with the remote, you can pass the URL with the credentials in them to the git command. Here's an example for push.

const remotes = await git.getRemotes(true);
if (remotes.length) { // Otherwise it's a local repository, no push
    let remote = remotes[0].name; 
    if (remotes[0].refs.push.indexOf("@") < 0) { // credentials aren't in the remote ref
        remote = remotes[0].refs.push.replace("://", `://${USER}:${PASSWORD}@`);
    }
    const pushRes = await git.push(remote, "master");
}
like image 100
Motti Avatar answered May 28 '26 09:05

Motti