Reputation: 639
I am trying to configure a git client to checkout using git fetch followed by git checkout from a bash script.
I have a github PAT (personal access token).
My purpose is to use my github user-id and the PAT to pass to the git fetch command one time such that next git command onwards, it won't require.
I know in git clone, I can pass the password like this:
git clone https://<token>@github.com/<username>/repository.git
I also setup the credential.helper to the cache for credential caching.
git config --global user.name "git-user-id"
git config --global user.email "email"
git config --global credential.helper cache
Now I want to pass the user name and PAT to the git fetch:
git fetch --no-tags --progress --depth=1 -- https://github.com/RepoName/demo-internal.git +refs/heads/<branch>:refs/remotes/origin/<branch>
git checkout <branch>
How can I do it in the git command line?
Note: I am not looking for an interactive way like using Expect or something like that.
Upvotes: 0
Views: 1520
Reputation: 76774
You shouldn't place credentials in the URL. The Git FAQ mentions why:
While it is possible to place the password (which must be percent-encoded) in the URL, this is not particularly secure and can lead to accidental exposure of credentials, so it is not recommended.
If your goal is to set up access for your own use (say, a personal or work desktop or laptop), then that FAQ entry tells you how to set up a credential helper that will save your credentials securely long term, using an appropriate encrypted credential store for your system.
If your goal is to set up credentials for some sort of automated system, you can set up a custom credential helper to read from the environment. You could also generate an Ed25519 SSH deploy key and use that.
Note that user.name
is not a username; it is a personal name and has no effect on authentication. For example, the maintainer of Git has this value set to “Junio C Hamano.”
Upvotes: 1