2

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.

Hivaga
  • 3,738
  • 4
  • 23
  • 36
  • Possible duplicate of [Is there a way to skip password typing when using https:// on GitHub?](https://stackoverflow.com/questions/5343068/is-there-a-way-to-skip-password-typing-when-using-https-on-github) – phd Mar 08 '19 at 12:15
  • 1
    https://stackoverflow.com/search?q=%5Bgit%5D+skip+password – phd Mar 08 '19 at 12:15
  • 1
    @phd Thank you. Yes I sort of solved the issue by adding the credentials into the .git/config file as mentioned in the reply to the questions above. It solved my issue but not the perfect solution I must say, working with the credential.helper of git is also kind of pretty obscure on how exactly to control reset, and so on. I whish https://www.npmjs.com/package/simple-git npm just included autenticate to their API example: [remote "origin"] https://username:mypassword@github.com/path/to/repo.git – Hivaga Mar 10 '19 at 16:41

1 Answers1

0

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");
}
Motti
  • 110,860
  • 49
  • 189
  • 262