51

I started using Swift package manager and when I add repository with https (https://github.com/Alamofire/Alamofire.git) address authentication always fails when I try to login with my github account

Xcode authentication fail

But if I'm using git@github.com:Alamofire/Alamofire.git it will get added successfully. I tried regenerating new key, deleted .ssh directory but nothing makes https work and I still get xcode authentication failed because no credentials were provided error. I could use locally ssh url but in CI I need one with https.

Lance Samaria
  • 17,576
  • 18
  • 108
  • 256
Simonas Daniliauskas
  • 2,276
  • 2
  • 10
  • 15

14 Answers14

137

It was a problem with git config. In .gitconfig file it was set to

[url "git@github.com:"]
    insteadOf = https://github.com/

After removing this section https worked correctly

EDIT: as mentioned in comments you can easily access your gitconfig in terminal with command: git config --global --edit

Lou Zell
  • 5,255
  • 3
  • 28
  • 23
Simonas Daniliauskas
  • 2,276
  • 2
  • 10
  • 15
  • 2
    I can confirm this worked for me as well, thank you. One question I have is when/how did this get inserted into the .gitconfig file, or when/how did Xcode start choking on it? Because I have not changed it and starting with Xcode 11 GM, it broke all my builds that were using SPM. – Ben Stahl Oct 01 '19 at 20:34
  • Honestly, I have no idea. Somehow it got inserted... I found this solution by pure luck. Before I tried to completely reinstall XCode, created a new github account and other things. – Simonas Daniliauskas Oct 02 '19 at 21:05
  • 2
    In my case, it was inserted automatically by go because of https://golang.org/doc/faq#git_https – rpecka Feb 04 '20 at 21:02
  • I had the same issue. I put the force ssh in my `~/.gitconfig` intentionally because I have 2 factor authentication enabled with GitHub. However, there seems to be bugs in Xcode swift package manager, and you need to remove the line to force ssh. – Paul Solt Feb 04 '20 at 22:07
  • I removed it, cleared Xcode's Github account, and then I was able to get another authentication login when I re-opened the Package.swift file. Before it was stuck downloading Swift-NIO and then all my targets would disappear. – Paul Solt Feb 04 '20 at 22:09
  • 8
    To then who are searching git config file. To locate the .gitconfig file Only write this instruction on terminal: git config --global --edit – Shoaib Bagwan Feb 25 '20 at 17:30
  • This is a bug in XCode or the swift package manager in my opinion. It should respect your git config. – Breedly Jun 28 '20 at 00:19
  • The bug in Xcode is really stupid, it doesn't respect the git config global stuff – gtxtreme Sep 18 '21 at 08:43
  • I made this change yesterday because I wanted to switch to the SSH way as I find tokens not that feasible so I referred to [this](https://gist.github.com/m14t/3056747) and this morning I ran into this issue on Xcode – gtxtreme Sep 18 '21 at 09:43
  • 2
    You are my hero! – Ilya Sep 25 '21 at 23:59
  • 1
    Had same issue on Xcode 12.4 and it worked. Thanks. – NaXir Sep 28 '21 at 10:16
  • Helped with 13.3 as well, thanks! – slxl Apr 29 '22 at 11:44
  • I personally like keeping this settings, so what I did was to limit it to repos in my `~/Developer` directory using `[includeIf "gitdir:~/Developer/**"]`. – kylejs Jun 05 '22 at 18:29
  • 1
    To delete this, in the command line run: `git config --global --unset-all url.git@github.com:.insteadof` – Lance Samaria Aug 11 '22 at 23:02
17

In my case with Xcode 11.3.1 I had the same problem and I solved changing de auth method to SSH from HTTPS in Github account preferences on Xcode.

enter image description here

Abrahanfer
  • 346
  • 4
  • 5
9

Building off of two previous answers, I solved this by doing what Abrahanfer did, setting Clone using to SSH in Xcode Preferences -> Account.

Then I used the SSH url of the repo, for example: git@github.com:AppPear/SwiftUI-PullToRefresh.git

Zack Shapiro
  • 6,648
  • 17
  • 83
  • 151
  • 1
    This is correct, if you want to find the SSH url for the repo on github, press "Use SSH" in the top right corner (after you press the "clone or download" button) – itsmcgh Jun 10 '20 at 12:45
9

what worked for me was both @SimonasDaniliauskas answer and @Abrahanfer answer

Basically in the command line I had to run:

git config --global --unset-all url.git@github.com:.insteadof

And in Xcode I had to go to Xcode > Preferences and switch my GitHub to use SSH

Btw, if you don't have ssh setup, follow this medium post or these GitHub directions. If you need to change your ssh keys follow this YouTube tutorial

Lance Samaria
  • 17,576
  • 18
  • 108
  • 256
7

you can use ssh URLs instead of https, e.g. git@github.com:ORG_NAME/REPO_NAME.git

Jasveer Singh
  • 354
  • 3
  • 10
4

I double clicked the error message Error while fetching remote repository: git@github.com:ORG/REPO-NAME.git or the The server SSH fingerprint failed to verify in Xcode's Report Navigator which then a popup appeared asking if I wanted to trust the host. After clicking that I was able to add the Swift Package using SSH.

Selecting HTTPS or SSH in the Xcode Preferences did not fix for me because Xcode seems to automatically handle SSH GitHub URLs in the SPM flow.

Raymond K
  • 41
  • 1
1

If you are facing this and your .gitconfig has below, and you want to keep it!

[url "git@github.com:"]
  insteadOf = https://github.com/

just add below two lines after above two lines in your .gitconfig

[url "https://github.com/apple"]
  insteadOf = https://github.com/apple

Duplicate above two lines and replace /apple with any other /user or /org where you might want to download your packages from

Yogesh
  • 4,546
  • 2
  • 32
  • 41
0

Try removing it and adding it again. If that won't work, remove your GitHub account from Xcode. Usually, git via ssh works better. What CI are you using?

  • 2
    If I remove and try to add it again I get `xcode authentication failed because no credentials were provided error` when i login with my github account, I tried removing it and adding it again but it still doesn't work. I'm using Github Actions. – Simonas Daniliauskas Sep 30 '19 at 10:42
0

For me, it seems more like a red herring (maybe a fellow developer accidentally checked in this small change related to swift package in project setting). I went to project setting, removed it from under "Swift Packages". It seems okay after that.

uudaddy
  • 341
  • 4
  • 9
0

I keep having this issue in Xcode 12.0.1 (12A7300).

My GitHub credentials seemed to not work, even though I applied all the suggestions above.

The way I fixed it (for now, at least) was to switch to SSH only authentication.

mokagio
  • 16,391
  • 3
  • 51
  • 58
  • Where is that setting? – Tommy Sadiq Hinrichsen Aug 26 '21 at 08:20
  • @TommySadiqHinrichsen unfortunately, it was not a setting per se, but a dialog that appeared when double-clicking on a failed authentication error from the logs in the Report navigator. It would ask me how to authenticate with GitHub and there was an option for SSH only. **After upgrading to Xcode 12.5.1, I haven't experienced the issue anymore**. – mokagio Aug 29 '21 at 20:07
0

I managed to get HTTPS working fine with CI. The solution, with bitrise, is to use 'Authenticate host with netrc', then Xcode will find private HTTPS repos properly. I am sure other CI platforms (or your own) can setup the same solution.

0

Adding repo via source tree and checking out repo through Xcode use some other tool, then adding same repo via SPM. Try creating SSH key via rsa key algorithm instead of ed255189 key algorithm. SPM tool comfortably work with rsa. Note: rsa key authentication is slower than ed25519 key authentication.

Amrit
  • 301
  • 2
  • 14
0

I followed the other anwsers here with no success. Eventually it turned out that the package was added to XCode with my username inside the https domain, like that:

https://yarden_k@bitbucket.org/private/package/path.git

so I had to adjust the accepted answer the same way (I added those lines to .gitconfig file):

[url "git@bitbucket.org:"]
    insteadOf = https://yarden_k@bitbucket.org/

And viola! It finally worked. Was a real headache to figure this one out.

Yarden
  • 45
  • 6
-2

Me too facing this problem

Check your repo access is correct and you have proper access for PUSH the code

They only gave me READ access, After facing this issue I'll informed to respective person and get WRITE access