Push your code to remote repo - Getting Started with Git Basics 2
Tags: git, gitcommands
Published: 2022-11-19 09:33:08

As we all know, Git is a Distributed Version Control System (DVCS). In this article, let’s have a look at how we can push our code to a remote repo, so that others can pull our code and work simultaneously.

This is the continuation of my previous article [Getting Started with Git Basics](https://5minslearn.medium.com/getting-started-with-git-basics-68440a7843ba). If you haven’t read that, please read that article at first and comeback here.

Let’s create a repo in Github. Most people prefer to use Github as it is open and free. Github offers us to create unlimited public and private repositories for free. Gitlab is an alternative for Github, which is specifically developed for private repositories (but also supports creating public repositories).

### Create a Github repository

* Signup for [Github](http://github.com/signup)

* Navigate to [Github Dashboard](http://github.com)

![Create a new Github repository](https://cdn-images-1.medium.com/max/2000/1*V7WuasKB0j6k0aJgEZsvAg.png)

* Click on **New** button on side panel

* Enter the repository name

![Create Github Repository](https://cdn-images-1.medium.com/max/2302/1*D6fsJZbKVGhDXCA4bUonBQ.png)

* Click on **Create repository** button (Leave the rest of the inputs to default. We’ll learn about them later)

### SSH

SSH stands for **Secure SHell**
>  SSH is authenticated and encrypted secure network protocol which is used for remote communication between machines on an [unsecured open network](https://whatismyipaddress.com/unsecured-network). SSH is used for remote file transfer, network management, etc.

We need to create an SSH key and add it to Github to communicate securely to our server.

Github provides an awesome documentation for [Generating an SSH Key](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent) and [Add SSH Key to your Github account](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account).

Before reading further, please add SSH key to your Github account

### Connect Local repository with remote repository

Let’s find the current state of our repository by `git config -l` command

![Current State of Git Repository](https://cdn-images-1.medium.com/max/2120/1*hRNFXKUcrVPtfGY0vCYkEQ.png)

You can find that there are no remote repositories attached to this project.

Select **SSH** option at top

![Connect with SSH](https://cdn-images-1.medium.com/max/2570/1*mS_6aH4OpUdBX-ENfrB0ag.png)

We need to follow **Push an existing repository from the command line **steps after we create the remote repository

![Push an existing repository from the command line](https://cdn-images-1.medium.com/max/4916/1*B8LEeYUv4B-nOA6_8aAziw.png)

The command to connect to remote repository is `git remote`

Run the following command to connect our local repository to remote repository

We don’t need the 2nd command. The initial branch is Git will be `master` which was later changed to `main` . You can read more about this [here](https://www.theserverside.com/feature/Why-GitHub-renamed-its-master-branch-to-main). But `master` branch is left to be backward compatible. I prefer to stick to `master` branch.

### Push the code to remote repository

The `push` command is used to sync our local changes with remote repository.

Run the following command to push your code to remote repository

`git push -u origin master`

![Push the code to remote repository](https://cdn-images-1.medium.com/max/3538/1*Gdsd3R_ih-R6OpAG-4Pogw.png)

When you prompted to verify the authenticity of github.com, type `yes`

![Pushed the code to remote repository](https://cdn-images-1.medium.com/max/3528/1*hPSEb5W78TqI_hjUHp-Osg.png)

Success!!! Your code is now synced with remote repository.

Reload your Github repo page, you’ll be able to see your contents there.

![Github repository](https://cdn-images-1.medium.com/max/3614/1*akOWfsoe3vOVPne2-UQX3g.png)

The `-u` option in `git push` command is used to set upstream branch for all `push` and `pull` operations. This will let Git use `master` branch by default for all `push` and `pull` operations.

Connecting your remote repository is the one time process. Use just `git push` command to push your commits to remote repository next time.

### Pull the code from remote repository

Next time, when you begin your work, never forget to run `git pull` command to sync the changes done by your team mates into your local machine.

Subscribe to our [newsletter](https://5minslearn.gogosoon.com/?ref=devdotto_git_basics_to) to receive more such insightful articles that get delivered straight to your inbox.