Repetition of the same task is boring and painful for programmers like us. Isn't it?

One of such tasks is interactive with GitHub's private repository. What'southward nigh it? You know what it is. And yous are searching for means to resolve it and landed on this article. You will stop searching after reading this commodity.

So, here nosotros are going to talk about accessing GitHub individual repository without a password. Without further ado, let'south become started.

There are two means to access any GitHub repository. They are HTTPS andSSH. Most of you lot are usingHTTPS. Simply, at present you come to know that it's not an efficient way to use the HTTPS method for cloning the private repositories.

Accessing includes cloning, pushing, pulling, etc..; annihilation that'south related to updating our repository in the remote.

There is no trouble when it comes to accessing the public repositories. But, we need to authenticate ourselves while accessing a private repository. In that location are different means to authenticate ourselves.

Let's start with the nigh familiar i…

If using HTTPS

Yous should probably know about the HTTPS method and looking for others. Permit'due south speedily see how to access a private repository using information technology.

  • Copy your private repository link.
GitHub Private Repository
GitHub Individual Repository
  • Open the terminal or cmd in your machine.
  • Paste the command git clone link to clone the private repository.
  • Supervene upon the linkwith your private repository link.
  • It'll enquire the states to authenticate ourselves. So, nosotros have to enter our GitHub credentials.
  • First, it'll ask us to enter our GitHub username. Enter your GitHub username and hit Enter.
Authentication Username
Authentication Username
  • Now, we need to enter the password. Blazon your GitHub password and hit Enter.
Authentication Password
Authentication Password

That'south it; we have cloned the individual repository using the HTTPSmethod. Now, update something in the repository, commit and push them to remote.

What did you notice?

It's once again asking for the hallmark.

Push Authentication
Push Authentication
Push Authentication
Button Authentication

Isn't information technology a wearisome and heavy task to enter credentials every time nosotros interact with the private repository?

Aye, it is.

Nosotros can't enter our GitHub credentials whenever nosotros interact with our private repository. Information technology's a time-taking process and slows down our work.

At that place are different ways to go rid of the higher up trouble. The best way to exercise information technology is to use SSH. But, there are other ways to practise it. Let'south expect at all of them one by one.

.git config

All the information about our repositories versions is stored in .git directory. It'southward a hidden folder. There is a config file in it that allows us to configure the settings. Simply, it's not recommended in general.

We tin clone a private repository by adding ourusernameandpasswordin the repository URL equally follows.

          git clone https://<stiff>username:countersign</strong>@github.com/<strong>username</potent>/<stiff>repository_name</strong>.git                  

Update theusername,password, andrepository_namewith appropriate details. Since we have given our credentials in the URL, information technology won't inquire for authentication as we accept seen before.

So, we are going to follow the above method of hallmark and update our repository configuration appropriately. Allow's see the steps to go rid of repetitive hallmark by updating the URL.

  • Open the .git binder in your cloned repository.
.git Folder
.git Folder
  • You'll detect a file with the name config. Open information technology using any text editor of your choice.
  • At that place will exist a line with our repository link as follows.
Repository Link in config
Repository Link in config
  • Update the URL past adding yourusernameandpassword, as seen above.
Repository URL Update
Repository URL Update

Now, once again update something in the repository, commit and button them.

Do you observe anything?

It shouldn't have asked for your GitHub credentials this time. And so, we have solved our trouble by updating our repository setting.

You might accept noticed that it'due south not secure. Every bit we are exposing our credentials. And this method won't work in case your GitHub password contains @character.

So, in that location are some critical disadvantages of using this method. Hence allow's ignore information technology and motility to the next method.

credential.helper

The credential.helper allows u.s.a. to store the credentials forever in ~/.git-credentials file.

It volition shop our credentials when we enter them for the first time. Again when nosotros try to access the individual repository, it won't ask for credentials until it'south stored in ~/git-credentials file. So, that's ane of the means to avoid our problem. Let's see it in activity with precise steps.

  • Outset, nosotros need to actuate the choice to store our credentials with the command git config credential.helper shop.
  • After activating the option, try to access the private repository with your username and password.
  • Once you lot have entered your username and password, it will store it in ~/.git-credentials file with your GitHub credentials as follows.
git-credentials
git-credentials

At present, again the same process to check whether it'south working correctly or not. Update, commit and push button. I am sure information technology won't ask you lot for the credentials if y'all have followed the above steps to store your credentials.

Going fine…

What if you want to save the credentials for4hours instead of forever?

The credential.helper provides a way to store our credentials temporarily for a sure corporeality of time. We use cache instead of store to store the credentials for a sure amount of time.

The cache will store our credentials for 15 minutes past default. After 15 minutes, the git will once again enquire for credentials. But, we can change the default fourth dimension using the following command.

          git config credential.helper 'cache --timeout={time_in_seconds}'        

Don't forget to requite the time in seconds. Let's see information technology in action.

  • Commencement, we demand to activate the option to enshroud our credentials with the command git config credential.helper cache.
  • Access the private repository with your username and password.
  • Once yous have entered your username and password, it volition cache your GitHub credentials for a specified time.

Now, update, commit and push. Again information technology won't ask for your credentials equally nosotros have said information technology to enshroud them.

We have shown you the commands to work a git initialized repository. We can update the git configuration globally for all projects by adding the --global flag in the higher up commands.

Personal Access Tokens

The personal access tokens are used to give access to the GitHub API. The personal access tokens are similarOAuth tokens. So, they can exist used for basic authentication instead of a password for git. Hence, we can use the personal access tokens to resolve our problem.

Let's see how to do information technology.

  • Log in to your GitHub business relationship.
  • Become to theSettings.
GitHub Settings
GitHub Settings
  • Now, become to theDevelop settings from the left navbar.
GitHub Developer Settings
GitHub Developer Settings
GitHub Developer Settings
GitHub Developer Settings
  • Click on thePersonal access tokensto reach our final destination. Y'all will encounter the personal admission tokens as follows.
GitHub Personal Access Tokens
GitHub Personal Admission Tokens
  • Click on theGenerate new tokento generate a new ane.
Generate new token
Generate new token
  • Enter the Note for the token. You lot can think of information technology as curt notes for the token to remember.
Personal access token Note
Personal access token Notation
  • Select the permissions for the token. The programs using the token will grant access to all the selected permissions. In our instance, select the repo.
Repository Permissions
Repository Permissions
  • Scroll down to the lesser and clickGenerate tokenbutton.
Generate token Button
Generate token Push
  • It'll testify the personal access token only in one case as follows. Nosotros can't run into our personal token again. So, copy it and salve information technology somewhere securely. Use a password manager if you lot need to.
Personal access token
Personal admission token
  • We have successfully created thePersonal admission token.
  • Now, information technology's time to use it to access our private repository.
  • Update the repository URL in .git/config file every bit https://{personal_access_token}@github.com/hafeezulkareem/private_repository.git similar to the first method.
Personal access token in config
Personal access token in config

At present, try to access the individual repository.

Did information technology ask y'all for the hallmark?

No, it won't ask you for the authentication until the token is agile. Let'due south movement to the terminal mode to resolve our problem.

SSH

SSH is used to authenticate ourselves. You lot notice the full document about SSH in GitHub here.

The thought is simple. Generate an SSH key, add it to the GitHub business relationship and enjoy passwordless authentication.

Let's see these 3 steps in detail.

  • Open last or cmd in your system.
  • Enter the command ssh-keygen -t rsa to generate a new SSH key.
  • It volition ask for the directory to salvage the key. Press Enter to select the default directory. But, you lot may also change the directory based on your preference. Here, we are going with the default directory.
SSH Directory
SSH Directory
  • At present, nosotros accept to enter thepassphraseto protect our SSH primal. But it'southward optional.
    • If we choose a passphrase for the SSH key, we must enter information technology first whenever we power on our system.
    • If we didn't choose the passphrase, and then in that location is no need to enter it first.
SSH Passphrase
SSH Passphrase
  • Enter the passphrase once more to ostend it.
SSH Passphrase
  • Finally, it volition generate a new SSH cardinal for us every bit follows.
SSH Key
SSH Fundamental

Nosotros have successfully generated a new SSH cardinal in our systems. It'll create two files as follows (if you have inverse the path, then the file names may vary).

SSH Key Files
SSH Key Files

Now, information technology'southward fourth dimension to connect with our GitHub business relationship. The contents in the file with the extension .pubneed to be copied to our GitHub account for the connexion. In my case, it's id_rsa.pub .

  • Log in to your GitHub business relationship.
  • Open theSettings.
GitHub Settings
GitHub Settings
  • Click onSSH and GPG Keys to reach our concluding destination.
SSH and GPG Keys
SSH and GPG Keys
SSH and GPG Keys
SSH and GPG Keys
  • Click on theNew SSH Keyto add our newly generate SSH cardinal.
New SSH key
New SSH cardinal
  • Yous will navigate to the post-obit screen.
New SSH Key
New SSH Key
  • Add the appropriate title for the SSH cardinal. The SSH keys are dissimilar for each system. So, choose based on it is 1 of the adept options. But, it's not the only selection. You may choose based on other things equally your preference.
  • After choosing the title, copy and paste the .pubcontents into the second field.
New SSH key
New SSH key
  • Finally, hitting theAdd SSH keyand confirm the admission with your GitHub password.
  • The newly added SSH key will look every bit follows.
New SSH key
New SSH fundamental

We have added our newly generated SSH primal to GitHub. Now, we have to authenticate the SSH connectedness to savor passwordless hallmark afterward. To practice it, enter the post-obit control in last or cmd.

          ssh -T [electronic mail protected]        
SSH Connection
SSH Connection

It will ask for confirmation. Ostend it. And that'southward it, and we are washed.

Now, clone your private repository. It won't inquire for whatsoever authentication this time.

Update, commit and push. In that location you become. It won't enquire you for the authentication anymore. Enjoy information technology.

Decision

Phew! nosotros have covered various methods to access private repositories without entering credentials all the time. You may employ any method. But, the general and best practice is to use theSSHmethod for authentication.

Again information technology's upward to your preference; in that location'south no strict dominion to use the simply SSH method. Just, most businesses use the SSHmethod for authentication, which is secure and saves a lot of time. And make sure your credentials are safe.

Happy Developing 🙂