0

When calling git fetch --update-head-ok origin develop:develop from a Bash script I get the following error

fatal: no path specified; see 'git help pull' for valid url syntax

But if I execute this Git command from Bash prompt

$ git fetch --update-head-ok origin develop:develop

I get no error message. I have verified that origin is correctly configured and I have also tried this variant from within the script (URI ssh://git@bitbucket....:7999 is a placeholder for an internal URI to a local Bitbucket server)

git fetch --update-head-ok ssh://git@bitbucket....:7999 develop:develop

which instead gives the error message

fatal: invalid refspec 'ssh://git@bitbucket....:7999'

But the exact same command line works without any problems from Bash prompt.

If I leave out --update-head-ok in the script I instead get the error message (as expected)

fatal: Refusing to fetch into current branch refs/heads/develop of a non-bare repository

I'm currently using Git 2.35.1 on Centos 7.

Update: I have found the problem. In my script I had a command line that looked like

git fetch --update-head-ok "${force}" origin "${branch}:${branch}"

where $force could either be an empty string or the string "--force". The result of doing like I did above is an empty argument to git fetch which it get confused by...

Ermingol
  • 33
  • 4
  • Sounds like you are simply running the commands in different directories. Possible duplicate of [What exactly is current working directory?](https://stackoverflow.com/questions/45591428/what-exactly-is-current-working-directory) – tripleee May 24 '22 at 10:37
  • I have tried with -C option to git and get the exact same behavior. When running from the command prompt I can be in any directory (when using -C option), but it does not work when running from within the Bash-script even when using -C option... – Ermingol May 24 '22 at 11:35
  • Generally exactly the same commands will work from a script as from the shell prompt. There is probably something surrounding your execution of the script which is not visible in your question currently. Could you try to add some debugging details to your script, and [edit] your question to show this? – tripleee May 24 '22 at 11:42
  • Your update provides the reason: to supply an *optional* flag, you don't want double quotes like this (or for the rare case where you do, you want the sh construct `${var+"$var"}`, i.e., expand to `"$var"` if `${var}` is set; use `${var:+"$var"}` if you want set-and-non-empty as the condition). – torek May 25 '22 at 06:01

0 Answers0