Tutorial 4 : Git clone, remote and push

Gaurav Patil
3 min readJul 30, 2022

--

Photo by Gabriel Heinzer on Unsplash

Missed previous tutorial ? — click here.

We will cover following topics in this tutorial :

git clone

git remote

git push

Run all the following commands in command prompt.

git clone :

Cloning means copying the Git repository and storing it in local machine (PC or laptop).

git clone <repo-url>

For example, let me clone my repository by running following command in command prompt(you can try this too):

git clone https://github.com/gaurav1patil/Web-Scraping-In-Python

Above cloning will download all the code files from that repository and store them in my PC.

If you want the repository to be stored at particular location, then you will have to specify that folder path as given below :

git clone <repo-url>  <folder-path-to-store-cloned-repo>

For example, let me clone my repository by running following command in command prompt(you can try this too):

git clone https://github.com/gaurav1patil/Web-Scraping-In-Python C:\Users\Gaurav\OneDrive\Desktop\new_folder

git remote :

The command starting with “git remote” is to communicate with remote repository.

Local repositories and remote repositories :

Local repositories are the ones that reside in the computers and remote repositories are hosted on a server (or internet) that is accessible for all.

Run following command in command prompt :

git remote

Output :

origin

“origin” is the default name of the remote repository.

Let’s understand the use of ‘git remote’ by creating a new repository and pushing a code file in it :

First create a new repository.

Then create a sample folder in your PC and create a sample1.html file :

<html>
<head>
<h1>This is first heading</h1>
</head>
</html>

To add this file in repository, run following commands in command prompt as we have already done before:

git initgit add sample1.htmlgit commit -m “sample1.html file added”

Now we will have to add a new remote for newly created repository:

git remote add origin https://github.com/<username>/<repo_name>.git

Name of my repository is “git_try_repo”, so I will run following command :

git remote add origin https://github.com/gaurav1patil/git_try_repo.git

To push above sample1.html file in newly created repository in your github account, go through commands as discussed below.

git push :

Now that we added a new remote and origin is the default name of URL, instead of writing entire URL we will simply write origin as done below :

git push -u origin <branch_name>

As sample1.html file, by default, is in master branch, run following command :

git push -u origin master

Above command pushes master branch of the local repository to the remote repository (github account), and as we know ‘origin’ is the default name of the remote repository.

Now check your github account to see if above file is pushed to the newly created repository or not.

As you can see above, it worked in my case.

Congratulations !!!

We will practice pushing more files to the repositories in upcoming tutorials. For the time being just understand the use of “git push” and “git remote”.

But what if I have created one file in master branch and I have different version of the same file in another branch. If I want to combine those two versions of a file from different branches in one file in one branch. This can be done by merging about which you will learn in next tutorial.

--

--

Gaurav Patil
Gaurav Patil

Written by Gaurav Patil

Machine Learning Engineer drawing insights from mathematics.

No responses yet