avatarAmrish Kushwaha

Summary

The article provides instructions on how to push a commit to GitHub using a username and password, particularly focusing on resolving the "access denied" error by using a personal access token.

Abstract

The article titled "How To Use Username and Password to Push Commits on GitHub?" addresses a common issue faced by developers when pushing commits to GitHub for the first time. It explains the typical process of initializing a repository, adding commits, setting up a remote origin, and pushing to the main branch. However, it emphasizes the solution to the "access denied" error that occurs due to the absence of a username and password in the system's credentials. The author guides readers through generating a personal access token on GitHub and using it in place of a password to authenticate and push commits successfully, which also adds the credentials to the Keychain on macOS systems.

Opinions

  • The author assumes that readers may be familiar with basic Git operations but need guidance to troubleshoot authentication issues.
  • The article implies that using a personal access token is a secure and effective method to authenticate Git operations on GitHub.
  • The inclusion of a Keychain reference suggests that the author is aware of macOS-specific challenges and provides a solution tailored to Mac users.
  • The author encourages persistence in coding and problem-solving, indicating a belief in the value of continuous learning and improvement in software development.

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

Simple solution

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.

Git
Github
Access Denied
Personal Access Tokens
Technology
Recommended from ReadMedium