In GitHub lingo, a “collaborator” is someone given access by the repository owner to contribute to a specified repository. You can perform various tasks like pushing changes or accessing a repository’s code as a collaborator. In this article, we’ll guide you through how to clone such a repository.
1. Accept the Invitation
You’ll receive an invitation email from the repository owner. Click ‘View invitation’ to accept it. You’ll also see which repository you’ll be the collaborator of before you accept the invitation.
2. Create a Personal Access Token
Log into your GitHub account, and click your profile image in the upper right corner. Then click Settings. Then click ‘Developer Settings’ on the left sidebar. Open Personal Access Tokens –> Tokens(classic). Step by step screenshot can be found in Create a Personal Token. Alternatively, you can directly navigate to the proper GitHub page by accessing https://github.com/settings/tokens.
Please note that you’ll need to use a classic Github token. The new fine-grained token can only grant access to your repositories and won’t work for others’ repositories.
Please give it a proper name, then set the expiration date to a year as a good practice. Check at least ‘repo’ for the scope, and include others if necessary.
You have only one chance to copy the token away after it’s created. Save it in a safe place, such as Notion.so. It is a beautiful notebook to write personal notes or collaborate with coworkers. Please consider to use our affiliate link to register for Notion to support
3. Find the Repository you Collaborate
It’s a little tricky to find the repositories you collaborate with. GitHub shall design it more intuitively. Since it’s not your repository, you won’t be able to see it in the ‘Your repositories’ section on your profile. Instead, click ‘Settings’ –> ‘Repositories’. The repositories you collaborate with have a ‘leave’ button beside it.
Alternatively, follow this link https://github.com/settings/repositories to access the page directly. Click the repository you would like to clone. You’ll see the clone options.
4. Clone the Repository
Now, you are ready to clone the repository. Copy the HTTPS repository URL from the repo, then add your GitHub username and the personal token you have created.
git clone https://your-github-username:classic-personal-token@github.com/other-username/repo.git
Now you can work on the repository using pull, push, etc. You can stop here if you are okay with using a personal access token. The next two sections describe two more advanced ways to clone a repository.
5. Using an SSH Key Pair
For those who are not that familiar with an SSH key pair, I drew a diagram to illustrate the process better. You need to generate an SSH key pair, including a public key and a private key. The public key is going to be installed within the GitHub user’s account. The private key is installed with the SSH agent where you use git.
5.1 Create an SSH Key pair
There are numerous ways to create an SSH key pair. We list one possibility here. In Linux, use the following command. Replace the email with your email.
ssh-keygen -t ed25519 -C "youremail@gmail.com"
You’ll need to enter a file name to save the key. Let’s say ‘ssh-for-github’. Use a passphrase if you would like to protect the key pair with it. Otherwise, enter to ignore to set a passphrase.
Then, two files are generated. ssh-for-github holds the private key; ssh-for-github.pub holds the public key.
5.2 Add the Public Key to Github Account
Click your profile picture, Setting, SSH and GPG keys, New SSH key. Alternatively, click https://github.com/settings/ssh/new.
Choose a proper title, and keep the Key type as Authentication Key. Copy the content of ssh-for-github.pub into the Key textbox. Finally, click ‘Add SSH Key’.
5.3 Add the Private Key to SSH Agent
In your local machine, check whether the ssh-agent is running. If not, bring up one in the background by the following command.
eval "$(ssh-agent -s)"
Copy your private key to ~/.ssh/. Then, add it to the ssh-agent.
ssh-add ~/.ssh/ssh-for-github
Now, you shall be able to test your SSH connection.
ssh -T git@github.com
You’ll get a success message as follows.
5.4 Clone the Repository using SSH
Now, you are ready to clone the repository. Since the SSH agent will handshake with the GitHub server to finish the authentication, you no longer need to use your username.
git clone git@github.com:other-username/repo.git
In order to use the private key every time you reboot, you also need to follow the instruction to auto start ssh-agent upon login. This extra work is beyond our discussion, and please refer to the StackOverflow discussion.
As you can see, using an SSH Key Pair needs more preparation, but it provides extra security than a simple token. It’s up to you and your team to choose a proper authentication method.
6. Summary
In summary, you may use a classic personal token or an SSH key pair to access other people’s GitHub repository when you are a collaborator. A classic personal token is fairly simple, and SSH needs more setup. Choose whichever is suitable for your team.
Feel free to add any comments below if you have any questions.
[Credit: Featured image is proudly generated by Midjourney]