git-clone-y-remote-add

Starting a Project with git clone or git remote add

  • 4 min

We’ve already learned to use Git locally, we master commits and reverts. We also have our remote (GitHub or GitLab) configured. Good job.

Now, how do we start uploading our files? Depending on your case, the path is different.

We have two possible scenarios:

SituationCommand
I already have code. I started locally and want to upload it to a new repo.git remote add origin <url>
I don’t have the code. It’s on the Internet and I want to start working on it.git clone <url>

Today we are going to see both options git remote add and git clone.

Uploading Your Local Code (git remote add)

Scenario 1️⃣, you have created a folder on your PC, you have done git init, you have programmed for weeks and have a history of 50 commits.

That is, you created the project and have it on your computer and you want to upload it to the cloud for the first time.

The process consists of 3 steps:

Go to GitHub/GitLab and create a “New Repository”.

Create the repository completely empty. Do not check the boxes for “Add a README file”, .gitignore or License.

If you create it with files, GitHub will have a different history than yours and you will have conflicts when trying to upload your code (the famous refusing to merge unrelated histories error).

Once created, GitHub will give you the URL. Now, in your terminal (inside your project), run:

git remote add origin [email protected]:your-username/your-project.git
Copied!
  • remote add: Adds a remote reference.
  • origin: The name we give it (remember, it’s a convention).
  • <url>: The server address.

If you run git remote -v, you will see it is already connected. But you haven’t uploaded anything yet. You have only configured the remote.

Now it’s time to send your files. Since it’s the first time, we need to configure the “default path”. Run:

git push -u origin main
Copied!

If you work with a GUI, like VSCode, when you create your repository, it’s normal for a button like ‘Publish branch” to appear, which will allow you to do all of the above in one click.

Downloading a Project (git clone)

Scenario 2️⃣, the project already exists in the cloud and you want to download it to your computer (for example, you start working at a company or want to collaborate on Open Source).

Then we clone the remote repository.

git clone <repository-url>
Copied!

For example, using the SSH URL we learned to get in the previous article:

git clone [email protected]:luisllamas/my-project.git
Copied!

If you want the folder to be named differently than the original repo, put the name at the end: git clone <url> my-custom-folder

Common Error: Cloning inside a repository

Sometimes, by mistake, we do a git clone while located inside a folder that is already a repository.

/MyProject  (has .git)
   /ExternalLibrary (we do git clone here)
      /.git
Copied!

This creates “nested repositories” (undeclared Submodules). Git will get confused and probably won’t track the inner folder correctly.

Always make sure to do git clone in a neutral folder (like Desktop or Documents/Projects), never inside another Git project.