Cloning is the process of creating a replica or duplicate of something. In our case, the replica we want to create is a repository. We'll clone it into our local computers.
There are three main ways to clone repositories from Github:
- Using HTTPS
- Using SSH
- Using Github CLI
We'll explore the three options below.
I'll be using test-ground
as the repository and gekkowrld
as the username. Be free to change to the correct username and repository when following along.
If you haven't configured Git yet, refer to this article: How to configure git
Tip If you want the folder that is to be created to have a different name from the one that it is named when cloning, you can give it another name by appending the name to the end of the git command. I'll show an example using HTTPS. But this method works for every method of cloning.
I'll change the folder that we'll be created in my system from test-ground
to test
git clone https://github.com/gekkowrld/test-ground.git test
To provide an example, let's assume my Personal Access Token (PAT) is ghp_eLhVuAdHDFRAUKDnWlICmtl46G4I983NylLs. Remember to use your own PAT instead of the one I provided, as the one I'm using is not valid. I have modified it for security reasons.
This is the easiest and fastest way to clone a repo. It is available if you are either logged in or not. You can see more about this method here.
Now let's get into the sweet stuff of cloning.
Cloning using this method can be done in two ways. Both are fine and none is "better" than the other one. However, ALX recommends one over the other. We'll start with the one recommended by ALX then we'll go over the other one.
IMPORTANT: Before you continue make sure that you have your PAT ready. If you don't have it you can follow these steps to get one.
Now let's clone our repository using the ALX recommended method.
This is the patter that you should use:
git clone https://[email protected]/username/repository.git
Now let's clone our test-ground
repository
git clone https://[email protected]/gekkowrld/test-ground.git
This will clone the repository to my local computer. We'll make some few changes.
Let's add a file named new_file.txt
touch new_file.txt
Now let's stage the file and commit the changes.
git add new_file.txt # Add new_file.txt to the staging area
git commit -m "Add new_file.txt" # Commit the file
When pushing Git will ask you for your credentials i.e., username and password. Enter your username and your token will be your password. In my case, that'll be ghp_eLhVuAdHDFRAUKDnWlICmtl46G4I983NylLs
. If you encounter any trouble, you can use this article: Troubleshooting PAT credentials.
There is another way to do it that doesn't require a complicated pattern to clone.
In this method, we'll just clone the repository by using the link provided by Github.
git clone https://github.com/gekkowrld/test-ground.git
This will clone the repository into our local development environment.
We'll add contents to a file named new_file.txt
echo "We are using HTTPS to clone our repository" >> new_file.txt
We'll stage our files and commit them.
git add new_file.txt # To add the files into the staging area
git commit -m "Add content to new_file.txt" # Commit the file
The final step is to push to Github.
Git will ask us for our username and password. The password is the token that you created in Github. In my case that'll be ghp_eLhVuAdHDFRAUKDnWlICmtl46G4I983NylLs
I have already written an article on how to configure SSH here. Make sure that you have followed the steps outlined here before coming back.
If the link doesn't work you can copy this link https://codetrybe.github.io/git-and-github/use-ssh-in-the-sandbox to your browser or go to CodeTrybe Github to read the markdown file.
After you have finished configuring, now we'll go step by step on how to clone using SSH.
First copy the link that is provided by Github.
[email protected]:gekkowrld/test-ground.git
Now let's clone the repo to our local dev environment
git clone [email protected]:gekkowrld/test-ground.git
Let's add a file named hello.sh
to our repository
echo -e "echo \"Hello, World\"" > hello.sh
Stage the changes and commit them.
git add hello.sh
git commit "Add hello.sh file"
To push, you'll do it normally.
git push
Git will push the changes to Github without asking for any further authentication. If it asks you for one. Enter the passphrase that you entered during configuration process.
For configuration, check out the official installation guide. If you are not sure, use the instructions about Debian or follow this link https://github.com/cli/cli/blob/trunk/docs/install_linux.md
To authenticate please refer to the official article
Alternatively, you can run the script provided at https://gist.github.com/gekkowrld/c267b9e7480dece7d9192cde92c2f0d1. Make sure that you have a token that has a minimum of this requirements:
The minimum required scopes are 'repo', 'read:org', 'admin:public_key'
Now to clone simply copy the code provided by Github
gh repo clone gekkowrld/test-ground
You can make changes, stage them and commit. It doesn't require a password when you commit the code.
If any of the command doesn't work or you don't understand something you can open an issue at https://github.com/codetrybe/git-and-github/issues/new.
HAPPY CODING!