44

How to Force git to Prompt You For a Password

 3 years ago
source link: https://seankilleen.com/2021/01/how-to-force-git-to-prompt-you-for-a-password/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

A fun git challenge! I had to make a request against a remote repository in git. The only issue is that only a shared GitHub account had access, not my own account (due to a vendor limitation). So when I occasionally needed to use this remote, I would have to log out and log in as the service account. It just seemed a bit messy.

I found a reasonable way to do this, and I’ll run through the steps below. (Have a better way? I’d love to hear about it in the comments!)

Generate an Access Token for the Shared AccountPermalink

  • Login with the shared account. Since I had the credentials, I did this via a normal browser session (in a private window since I was just logging in once.)
  • Generate a personal access token. If you’re unfamiliar with this, you can follow the steps in the GitHub docs.
  • Store in my password manager. This access token is as good as a password, so I treat it with the respect it deserves.

Add the Remote in GitPermalink

  • Add the remote if it’s not already added: git remote add REMOTE_NAME THE_URL_OF_THE_REMOTE

Unset the git credential manager temporarilyPermalink

I’m doing the steps here manually but it could almost certainly be automated in a tiny script.

  • Check the value of credential manager: git config --system credential.helper. Note this value for later; you will need it to set things back.
  • Unset the credential manager, which will prompt you for PW going forward: git config --system --unset credential.helper

Running the commandPermalink

  • Run your applicable command, e.g. git fetch upstream in my case. You’ll be prompted for a password.
  • Use the shared account username, and the personal access token as the password

Return things to normalPermalink

  • Set the value of the credential manager back to what it was, e.g manager-core in my case: git config --system credential.helper manager-core

That will let you quickly do one-time operations as the other account without needing to log out / in, mess up your other git credentials, etc.

Happy gitting!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK