How To Use Username and Password to Push Commits on GitHub?

Simple solution

Amrish Kushwaha
ILLUMINATION
2 min readNov 2, 2023

--

Photo by Roman Synkevych on Unsplash

Sometimes while doing the work, you might have come to a requirement that you need to push a first-time commit to GitHub remote.

But whenever you are trying to push the commit using this command after adding remote origin to push that commit:

git push -u origin main , it throws an access denied error.

In this article, I am going to explain to you how to push commit correctly and not have access denied error.

First thing first,

How to push commit commonly:

Here is the normal flow to push commit to remote.

  1. Have/create a repository in your remote (usually github.com)
  2. Do git init in your directory which you want to send to remote if git is not initialized.
  3. Add the very first commit using these commands.
git add .
git commit -m “initial commit”

4. Now add remote origin to local. To do so, first, you need to grab the remote git URL from the remote and then add

git remote add origin <remote-url>

5. Now push the commit:

git push -u origin main

Here main is the branch name and -u is used for setting the upstream (tracking) reference.

Above is the normal flow for publishing the commits to remote.

Why Access Denied Error?

Access denied error happens when in your system, username and password are not set, meaning that the remote(github.com) doesn’t know about your system.

If you are using a Mac, you need to add an internet password to the Keychain if it is not present.

Solving Access Denied Error:

To solve the access denied error, you would need to have your remote username (GitHub username) as well as a token( GitHub access_token).

  1. Generate the personal access token from github.com.
    https://github.com/settings/tokens
  2. Now push the commit using this command. You need to provide a personal access token as the password.
git push -u https://<username>:<access_token>@<remote-url-without-https> main

That’s all.

It will add an internet password to KeyChain (If you are using the Mac) and push the commit successfully.

Notes*:

If remote-url is https://github.com/user1/repo1.git , then

remote-url-without-https would be github.com/user1/repo1.git .

That’s it.

Thanks for reading. I hope that you will like the article.

Keep coding and keep solving problems.

--

--

Amrish Kushwaha
ILLUMINATION

Engineer & Writer | Writes about Tech, Productivity, Building Product and Success