Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a convenient way to add gh cli as a git credential helper #3796

Closed
jongio opened this issue Jun 7, 2021 · 16 comments · Fixed by #4246
Closed

Provide a convenient way to add gh cli as a git credential helper #3796

jongio opened this issue Jun 7, 2021 · 16 comments · Fixed by #4246
Labels
enhancement a request to improve CLI help wanted Contributions welcome

Comments

@jongio
Copy link

jongio commented Jun 7, 2021

Describe the feature or problem you’d like to solve

Right now, I have to google and trial and error to get the gh cli wired up as a git credential helper.

This command works:

git config --global 'credential.https://github.com.helper' ''
git config --global --add 'credential.https://github.com.helper' '!gh auth git-credential'

But I would prefer this be baked into the cli.

Proposed solution

Something like:

gh auth setup-git-credential-helper

Which would execute the commands above or the appropriate commands for a given os/arch combination.

Additional context

Add any other context like screenshots or mockups are helpful, if applicable.

azsdke2e
azsdke2e2

@jongio jongio added the enhancement a request to improve CLI label Jun 7, 2021
@mislav
Copy link
Contributor

mislav commented Jun 10, 2021

Thanks for the suggestion! This is a good idea, as I often find myself suggesting these manual instructions to our users 👍

@vilmibm vilmibm added the help wanted Contributions welcome label Jun 22, 2021
@despreston
Copy link
Contributor

i.e. expose this as a new command? https://github.com/cli/cli/blob/trunk/pkg/cmd/auth/shared/git_credential.go#L58 If that's all there is, I'd be happy to take this.

@vilmibm
Copy link
Contributor

vilmibm commented Jul 16, 2021

@despreston yup, that is the idea.

@mikeboiko
Copy link

mikeboiko commented Aug 25, 2021

Thanks for the suggestion! This is a good idea, as I often find myself suggesting these manual instructions to our users +1

Thank you all! It took me a good half an hour of googling to stumble upon this command.
I would imagine most git & GitHub users could benefit from it.

@jongio
Copy link
Author

jongio commented Aug 26, 2021

@mikeboiko - Could you do me a favor and comment with all of the google search terms you used to try to find this info? If you post them here then they will be indexed for the next dev to find it faster. Thanks!

@mikeboiko
Copy link

@mikeboiko - Could you do me a favor and comment with all of the google search terms you used to try to find this info? If you post them here then they will be indexed for the next dev to find it faster. Thanks!

Great idea! I searched the following in google:

  • how to use gh in .gitconfig
  • how to use gh as credentials in .gitconfig
  • using gh with git credential
  • how to push from github-cli

@jongio
Copy link
Author

jongio commented Aug 26, 2021

Thanks. I also added to my blog post that led to filing this issue https://blog.jongallant.com/2021/06/codespaces-gh-cli-git-credentials/

Hopefully this helps get people here!

@despreston
Copy link
Contributor

What is the purpose of the git auth git-credential command now? Is that being used for anything other than what OP is asking for? Wonder if it makes sense to refactor git auth git-credential to do what this issue is asking for.

@mislav
Copy link
Contributor

mislav commented Aug 30, 2021

gh auth git-credential command was designed to be used as a git credential helper, i.e. to be invoked and used entirely from a git process. The user should never have to directly interface with this command; that's why it's "hidden".

I would leave the git-credential command as-is and create a new command to set up git with gh as a credential helper. Something like:

$ gh auth setup-git
#=> writes configuration to `~/.gitconfig`

For simplicity, I imagine that the command should be non-interactive. There is now a question for which GitHub hosts should the command set up a credential helper? I would propose that all hosts that gh is currently authenticated with should be covered. Additionally, there could be a setup-git --hostname flag to override that.

despreston added a commit to despreston/cli that referenced this issue Aug 31, 2021
Adds a new command `gh auth setup-git [<hostname>]` that sets up git to
use the GH CLI as a credential helper.

The gist is that it runs these two git commands for each hostname the
user is authenticated with.

```
git config --global 'credential.https://github.com.helper' ''
git config --global --add 'credential.https://github.com.helper' '!gh auth git-credential'
```

If a hostname flag is given, it'll setup GH CLI as a credential helper
for only that hostname.

If the user is not authenticated with any git hostnames, or the user is
not authenticated with the hostname given as a flag, it'll print an
error and return a SilentError.

Closes cli#3796
despreston added a commit to despreston/cli that referenced this issue Aug 31, 2021
Adds a new command `gh auth setup-git [<hostname>]` that sets up git to
use the GH CLI as a credential helper.

The gist is that it runs these two git commands for each hostname the
user is authenticated with.

```
git config --global 'credential.https://github.com.helper' ''
git config --global --add 'credential.https://github.com.helper' '!gh auth git-credential'
```

If a hostname flag is given, it'll setup GH CLI as a credential helper
for only that hostname.

If the user is not authenticated with any git hostnames, or the user is
not authenticated with the hostname given as a flag, it'll print an
error and return a SilentError.

Closes cli#3796
despreston added a commit to despreston/cli that referenced this issue Sep 3, 2021
Adds a new command `gh auth setup-git [<hostname>]` that sets up git to
use the GH CLI as a credential helper.

The gist is that it runs these two git commands for each hostname the
user is authenticated with.

```
git config --global 'credential.https://github.com.helper' ''
git config --global --add 'credential.https://github.com.helper' '!gh auth git-credential'
```

If a hostname flag is given, it'll setup GH CLI as a credential helper
for only that hostname.

If the user is not authenticated with any git hostnames, or the user is
not authenticated with the hostname given as a flag, it'll print an
error and return a SilentError.

Closes cli#3796
@jongio
Copy link
Author

jongio commented Sep 7, 2021

Just a heads up that I created a GitHub CLI extension for this - just as an experiment with CLI extensions
https://github.com/jongio/gh-setup-git-credential-helper

The extension also clears the GITHUB_TOKEN env var and runs gh auth login.

I'll move the extension to the new setupgit cmd once it is ready. Looking forward to it!

@gibfahn
Copy link

gibfahn commented Oct 1, 2021

As part of this, it would be great for the docs for this feature to note that gh will act as its own credential helper on systems where there isn't one by default (as @mislav explained in #2449 (comment)).

@sjackman
Copy link

sjackman commented Mar 11, 2022

[credential "https://github.com/"]
	helper = 
	helper = !/usr/local/bin/gh auth git-credential
[credential "https://gist.github.com/"]
	helper = 
	helper = !/usr/local/bin/gh auth git-credential

What's the reason for the empty value in helper =?

git config --global credential.helper '!gh auth git-credential' worked for me FWIW.

@mislav
Copy link
Contributor

mislav commented Mar 11, 2022

@sjackman The blank line is intentional to break the chain of previously configured global helpers when handling authentication for these hosts. Otherwise, git would have used a whole stack of each configured credential helper, copying the tokens between them, and we didn't want that for github.com since it could make it hard to debug for the user.

@elyobo
Copy link

elyobo commented Sep 9, 2022

Just wanted to say that this is an excellent feature, thanks folks.

JeremyChuaWX added a commit to JeremyChuaWX/dotfiles that referenced this issue Apr 28, 2023
@akwotom
Copy link

akwotom commented May 31, 2023

This is 2023, and gh auth setup-git isn't working. It returns no output, and changes nothing.

@mislav
Copy link
Contributor

mislav commented May 31, 2023

@akwotom To see what setup-git does under the hood, activate the verbose mode:

GH_DEBUG=1 gh auth setup-git

You should see that it invokes git config --global to edit global git configuration (usually in ~/.gitconfig). If there are any bugs with this, you are welcome to open a new issue and provide more information about your circumstances.

@cli cli locked as resolved and limited conversation to collaborators May 31, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement a request to improve CLI help wanted Contributions welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants