1

I need to pass arguments to git with user and password as exemplified below.

NOTE I: Wa can not use git config --global credential.helper 'cache --timeout=1800', for example.
NOTE II: The branch [BRANCH_NAME] must be set up to track a remote branch [BRANCH_NAME] from origin.

#!/bin/bash

USR_CREDENTIALS="https://[USER_NAME]:[PASSWORD]@[GIT_URL]/PROJECT.git"
BRANCH_NAME="[BRANCH_NAME]"
git fetch $USR_CREDENTIALS --all --prune

# ERROR: fatal: fetch --all does not take a repository argument

git checkout -b $BRANCH_NAME origin/$BRANCH_NAME

# ERROR: fatal: 'origin/[BRANCH_NAME]' is not a commit and a branch '[BRANCH_NAME]' cannot be created from it

What am I doing wrong in the code above? Some workaround?

2
  • Can you show how you cloned your repo before you got to the fetch command? Passing a whole repo URL to git fetch seems odd to me, it looks more like a git clone argument. Commented May 25, 2019 at 22:09
  • Simply omitting --all should work (as the error message suggests). Commented May 25, 2019 at 22:26

1 Answer 1

1

You have a couple of issues here that should be addressed.

First, you cannot use git fetch --all and specify a remote. --all means to fetch all known remotes, which doesn't make sense when you specify just one remote on the command line. If you need to pass credentials to Git for an existing remote, write the URL you have into a temporary file, and then use the -c option to pass a credential helper on the command line, like so:

DIR=$(mktemp -d -t "$TMP/tmp.XXXXXX")
echo "$USR_CREDENTIALS" >$DIR/creds
git -c credential.helper="store --file=$DIR/creds" fetch --all --prune
rm -fr "$DIR"

If you want to fetch just a single remote, then you can do the same thing as above, just replacing --all with the name of the remote corresponding to the URL.

Note that the -c option here must come before the fetch subcommand.

You can also accomplish this by running the following, but be aware that anyone on the system and anyone with access to the repo can see your password in plain text if you do this:

git fetch "$USR_CREDENTIALS" "$BRANCH_NAME"
git checkout -b "$BRANCH_NAME" FETCH_HEAD

It will also not fetch the remote tracking branches for origin.

Second, you cannot mix options and non-option arguments like you're doing. It may happen to work in some cases, but you'll find that there are edge cases where it's broken. Unless you're passing configuration options (i.e., git -c), all options (i.e., those things starting with -- and -) need to come before any non-option arguments and after the subcommand.

Third, the issue you're seeing with git checkout means that origin/$BRANCH_NAME doesn't exist on the local system. It's possible that it's on the remote server, but not on the local machine. If your fetch succeeds and the remote server contains a server with the appropriate branch name, your command should work.

Sign up to request clarification or add additional context in comments.

3 Comments

I do not really need to pass any URLs. I do this just because git "forces" me to be able to pass user and password. If there is a way to only pass user and password, for me it will be perfect! Thanks! =D @bk2204
I've updated with some more options. In general, using the credential manager is the right way to handle this, as it doesn't expose your credentials to everyone on the system. However, if you don't care about that, there is an alternative.
Your response was the only one on the internet that met my need. I'll test that better and give you a return. Actually to the result of this will be a bash script that I will publish in github with permissive license. Thanks! =D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.