4

I want to switch to remote branch, but before that i do a fetch, so that branch get available to switch. But I want this to be done providing username/password with command itself like git pull "https://<un>:<pwd>"git.com/<repo>" <branch> pull works fine with username password but how can i used the same way for fetch command

user51
  • 8,843
  • 21
  • 79
  • 158

2 Answers2

0

It seems there is no built-in way to do, but you can point credential.helper config at a program that will print the data on stdout. See man gitcredentials.

You could even specify a shell command in-line with ! but see below why that's DANGEROUS:

git -c credential.helper="!printf 'username=FOO\npassword=BAR\n'" pull REMOTE

This particular kludge relies on printf unix command, won't work on Windows.
You'll also need to quote special symbols in the password from the shell, and from printf itself (e.g. replace % with %%)...

Note that git will supply an extra get / store / erase argument, so for example if you said credential.helper="!echo username=FOO" it'd run echo username=FOO get and git will think the username is FOO get, not what you want. printf happens to ignore extra args when its first "format" arg doesn't contain any formatting specifications like %s, and I relied on this not-quite-documented property here .

⚠ Supplying passwords on the command line, or in env vars is INSECURE ⚠

Other process running on your computer may observe it from the list of executing processes, even if they run under a different user (detail vary by OS, /proc/NNN/cmdline & /proc/NNN/environ files on linux).

The accepted safe ways to pass credentials to another process are (1) send it on stdin (2) give it a file name (and ensure the file is not accessible by other users). In other words, you better use (or write) a "real" credential helper...

Beni Cherniavsky-Paskin
  • 9,483
  • 2
  • 50
  • 58
-3

git pull does a git fetch anyway (followed by a merge)

So if your command worked with git pull, the same URL should work identically with git fetch.

That being said, it is a better practice to cache those credentials in a credential helper.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • *So if your command worked with git pull, the same URL should work identically with git fetch* Yes it should, but it doesn't. Using fetch with the repository argument does update nothing as far as I can tell while git pull with the repository argument will update the local branch but not the remote branch. – Roman Reiner Mar 22 '19 at 13:36
  • @RomanReiner OK. Can you post a new question with more details illustrating your issue? That help me (and others) for proposing a solution. – VonC Mar 22 '19 at 18:37
  • git pull does things which are conceptually different than git fetch – user3480922 Jul 05 '19 at 07:02
  • @user3480922 I agree: git pull does not indeed always do a git fetch: https://stackoverflow.com/a/18924297/6309 – VonC Jul 05 '19 at 07:31