MFH Labs : Git to work with Remote Repositories

Steps to work with remote repository:

  1. At start, we already have a local repository in our machine.
  2. Create a remote repository in GitHub, well known cloud service for Git repository.
  3. Push local repository to remote repository.
  4. Pull from remote repository to another local repository for simulating cooperation between teammates.
  5. For convenience, create short name for remote repository.

OK. Let’s get started!

Review What we Already Have?

We have built a project, and initialized a local repository to do a bit of version control on this project. If you haven’t done it, refer to previous tutorial.

For now, we have a well-managed local repository.

Create a Remote Repository in GitHub

GitHub is the most popular cloud service for hosting Git repository. It’s free and widely applied in the enterprises. Sign up a new account if you haven’t had one.

It’s easy to create a repository in GitHub. Just input a repository name and create a new blank repository.

Input repository name and click create repository button.

Push Local Repository to Remote Repository

Now, we have two repositories: one is in the local machine, the other is in the remote. Repository in the local machine is up-to-date, but repository in the remote is still blank.

We have to synchronize these two repositories by pushing local repository to remote. There are two steps to make it done:

Repository URL is shown in the GitHub repository page.
  1. Find out the remote repository URL in GitHub web page.
  2. Use command below:
git push [Remote_URL] master

For example:

git push https://github.com/cwpeng/training.git master

You may be asked to enter username and password of your GitHub account for verification. Just do it.

Keyword master in this command means repository branch, another important management tool of Git system. It will be introduced in the next tutorial. We always use master branch in this tutorial for simplicity.

Push local repository to remote repository.

Pull from Remote Repository to Another Local Repository

If you are the only one developer in the project, it’s done. You can backup and tracking commit history of your project in GitHub. Congratulations!

However, how do we cooperate with teammates if we have a team? The remote repository is the key: every teammate has their own local repository, communicating with each other through the single remote repository.

For learning purpose, we can create another blank project folder in the same machine as original one, just as there is a new teammate creating a project folder in his laptop. Then, follow instructions below to simulate a simple working flow for synchronization between two local repositories.

First, repeat what we have done in the first local repository: move working folder of command line interface to the new creating project folder, and initialize it:

git init

Second, pull remote repository down for synchronization:

git pull [Remote_URL] master

For example:

git pull https://github.com/cwpeng/training.git master

If everything went well, we totally have three synchronized Git repositories: two in the local for development respectively, and one in the remote for synchronization.

Please notice that, in practical use, every local repository will be managed by different developer in the different machine.

Cooperation between two developers.

Let’s think about a simple working flow for our global team: In the daytime, a developer in India changes something in his local repository and push to the GitHub. 12 hours later, another developer in North America can pull latest version from GitHub and go further.

It works like a charm!

Create Short Name for Remote Repository

For now, we use repository URL directly in the git push/pull commands. It’s time to make things better.

Every local repository can have a list of short name for remote repositories. It’s easy to create a short name for remote repository by git remote add:

git remote add [Short_Name] [Remote_URL]

We can use short name to simplify push and pull commands:

git push [Short_Name] master
git pull [Short_Name] master

For example:

git remote add origin https://github.com/cwpeng/training.git
git push origin master
git pull origin master

In the example above, you can replace origin by any other name you like. Finally, we can check name list anytime by appending v flag to remote command:

git remote -v
Create and use short name to replace repository URL in the git push command.