Git Remote Repository

In our previous tutorial, we learned how to work with local git repository. We followed some of the steps below:

  • we created a new project directory
  • we initialized local git repo
  • we added some files to our local git repo
  • we committed file changes to our local git repo

Git Local Repository

What is remote git repository?

A remote git repository is nothing but a space in a remote computer which allow us to manage our project. A benefit of having a remote git repo are followings:

  • multiple developers can work remotely on same repo
  • you can push your local file changes to server

Think about this way if you only have a local git repo and what if something went wrong with your computer? What if you want other developers to work on the same project?

In this tutorial I will use github as a remote server to manage my remote git repository. If you want to play with github create a new account using following link:

Sign up for Github

How to create a github repository?

Log on to your github account. Click on + icon located on top right corner to create a new remote git repo as shown in image below:

Click on New Repository menu item. You will now see a screen shown below where you need to put repostory name, description (optional), select public or private and hit Create Repository button:

Next, it will create a new blank repo and will take you to the following screen. It will gives you following two options:

  • creating a new repo
  • adding existing repo

In our previous tutorial, we created a local git repostitory. We want to add that local git repo to our newly created git remote repo.

Let's make our local git repo to be aware of our newly created remote git repo. So that you can push your local git changes to remote git repo.

How to add remote git url?

Let's go to our local git repo and run commands one by one shown in above screen shot:

# add remote git repo url to local git repo
git remote add origin

# check the status of remote repo
git remote -v

# above command will output something like below
origin (fetch)
origin (push)​

Now, out local git repository is aware of remote repo. Now, we can do following tasks with remote repo:

  • we can fetch new changes from remote repo
  • we can push new changes to remote repo

How to fetch remote changes using git?

Now, that we have a remote git repo and we know that it is empty. We have our local git repo with some files. Our task is to move our local git changes to remote git repo.

When we created a new remote git repo by default it creates a master branch. We are at beginners level so at this moment dont worry about branches and what are they we will learn them as we move on.

For now, just think that you have only one branch called master where you need to push your local git changes. We need to use fetch command to get all new changes from remote git repo.

Let's run following command to get all the data from remote git:

# fetch remote changes/refs
git fetch

# you can also use following 
# command instead of git fetch
git pull​

What is the difference between git pull and git fetch?

Git fetch will only fetch all the remote changes to local git repo.

Git pull will first fetch all the remote changes to local git repo and performs merge operation which means if you and other developer work on the same file and both made some changes. They will be merged together with git pull.

It is always recommended to perform git pull operation before you push any changes to remote server to avoid any incoming conflicts with code.

Let's move on, when you run git pull or git fetch with our newly created github repo you may see following error:

This error occur because you may not have write access to this remote repo. With write access you can commit your local git changes to remote git repo.

You need to generate a ssh key and add to your remote github repo to resolve this error by allowing write access for the given ssh key.

Follow the tutorial below to resolve this error:

Adding SSH key to Github

Once you resolve your write access error. We can push our local git changes to remote server by running following command:

git push -u origin master​

You will see following output with above command:

We have successfully pushed our local git changes to remote git repo.

What did you learn today?

You have gained following skills by following this tutorial:

  • You learned about remote git repo
  • You learned how to create a remote repo
  • You learned how to add remote git repo url to local git repo
  • You learned how to push local changes to remote git repo

In next tutorial, we will learn more about basic git commands.