Sintrias
Sintrias

Reputation: 586

Can't Access Private Git Repo With Personal Access Token

I've never used a private Git repo until recently. I'm having trouble with gaining access to my private repo from my local machine. Last month, GitHub updated the way you access private repositories. Now you can't use your username and password. I'm given this message when I try with the personal access token: remote: Repository not found. fatal: repository 'https://github.com/[Org]/[Repo].git/' not found.

I've already initialized my local repository. Now I just need to connect it to my private remote repository. I'd like to use my personal access token instead of ssh.

Here are the commands I'm using on Windows:

git remote add origin https://[UserName]:[Token]@github.com/[Org]/[Repo].git
git add .
git commit -m "Commit" ---> Output: On branch master nothing to commit, working tree clean
git push --set-upstream origin master ---> Output: Gives me the error

Upvotes: 3

Views: 10592

Answers (3)

Nick Muise
Nick Muise

Reputation: 430

A slightly different answer worked for me. I found that I was suddenly unable to push to one of my repos, but could still push to all the others with no issue. I thought it was something wrong with my token or my personal permissions, but I confirmed that I had admin + write permissions, and that the token I was using had permissions on the repo in question and was unexpired. I even thought something might have gotten corrupted locally, or that updating git might have broken something, but I couldn't find anything wrong. Finally, I checked my remote URL on the problem repo, and it was https://github.com/<my_user>/<my_problem_repo>.git. I then checked the remote URL on one of the repos I was still able to push to, and it was [email protected]:<my_user>/my_repo_that_I_can_push_to>.git. I changed the remote URL in the problem repo to be in git@github format using git remote set-url origin <url> and was finally able to push to it again.

I believe the reason why I was "suddenly" unable to push to that one repo is that I had originally been using password authentication, so that https URL it was using was no problem, but since I last pushed to that repo a long time ago, git deprecated password authentication (?), and I switched to using a PAT in my system's GITHUB_ACCESS_TOKEN env var for authentication. But you can't use PATs with HTTPS URLs (you need to use a git@github URL or maybe? an SSH url). I must have updated the remote URLs on my other repositories but forgot to update this one, explaining why I could push to every repo except one.

If anyone comes to this question because they're mysteriously not able to push to one or only a few of their repositories, check if your remote url is one of the formats that supports PATs (NOT HTTPS) with git remote get-url remote.

Upvotes: 1

Sintrias
Sintrias

Reputation: 586

So the main takeaway from my issue is to make sure the role you're assigned is allowed to push to the private repository. My team and I assumed that an admin would be able to push, but that doesn't seem to be the case. Also, you can use either of these for your origin:

https://[Token]@github.com/[Org]/[Repo].git

or

https://[UserName]:[Token]@github.com/[Org]/[Repo].git

Upvotes: 1

sego
sego

Reputation: 126

I don't think your statement "Last month, GitHub updated the way you access private repositories. Now, instead of using your password, you use a personal access token. " is true at all. You can do that, right.

To use the token add your repo url like so:

git remote add origin https://[Token]@github.com/[Org]/[Repo].git

Upvotes: 1

Related Questions