Intro to Git-2 (Remotes)

This is part 2 of my Intro to Git Series. If you are unfamiliar with git basics I would recommend checking out part 1 of the series.

Remotes are an essential part of collaborating on any git project. They make it easy to share changes and collaborate with teammates.

A remote is a git repository that is hosted on a server. You may have a few of them, depending on how your team would like to work together. Working with a remote repository (“repo” for short) allows teammates to put their changes online for the rest of the team to grab. That way every member of the team can work independently without worry of what other members are working on.

There are many different hosting websites that you may use to host your remote repositories. I will use Github for this tutorial.

Go over to GitHub.com and follow these instructions to create a new git repository. You will have to sign up if you have never used GitHub before.

(source:help.github.com)

Screen Shot 2016-09-22 at 12.25.13 PM.png

After setting up the remote repository, you will see that you are given a HTTPS link ending with “.git”

Screen Shot 2016-09-22 at 12.26.58 PM.png

This link is used as a reference to the remote repository when you are working locally on your computer.

Git Clone

The next step is to clone the repository to your computer. This will create a copy of the remote on your computer for you to work with.

To clone the repository cd into the directory that you would like the project to be located, via Terminal. Then type “git clone <YOUR_URL>” where <YOUR_URL> is the URL you were given when you set up the remote.

Screen Shot 2016-09-22 at 12.34.00 PM.png

This will create a new directory with the name of your repository. cd into it.

Once you are in the cloned folder, type “git status” and you will see that you are currently in an active git repository.

When cloning a remote, git will automatically create a reference to the remote from your local repository. You can verify this by typing “git remote”

Screen Shot 2016-09-22 at 12.39.46 PM.png

When typing git remote, it returns the word “origin”. The word “origin” is an alias used to describe the remote that you cloned. To get a bit more information about “origin” type “git remote -v”, where the “-v” stands for “verbose” for more information.

Screen Shot 2016-09-22 at 12.42.29 PM.png

This command shows that the alias “origin” is actually an alias for the URL we were given earlier. This makes it easy to reference the remote without typing in the specific URL to it, we can now simple type “origin” when referring to the remote.

Working between Computer and Remote

This repository is now set up for you to work as you normally would in a local git repository. You may create commits and add new features to your project.

I added two new commits to my local repository.

Screen Shot 2016-09-23 at 10.35.06 AM.png

In order to get these commits on my remote, I just need to type one simple command. Type “git push”. The command “git push” pushes the commits in your local repository to your remote. By default it pushes to origin. You will see something similar to this.

Screen Shot 2016-09-23 at 10.36.13 AM.png

If you visit your GitHub repository, you should see the commits and files you just pushed.

screen-shot-2016-09-23-at-10-41-32-amscreen-shot-2016-09-23-at-10-41-43-am

Working with a team

To share your remote with a team, you need to add their GitHub usernames as collaborators in the GitHub repository settings. Then they will be able to push and pull from your remote normally.

Screen Shot 2016-09-23 at 10.53.06 AM.pngIf you are working on a team and some of your teammates have pushed their new changes onto the remote, you will need to grab those changes and update your local repository. To grab any new changes pushed on the remote that you simple type “git pull”. This will give you any commits that are not on your local repository.

Screen Shot 2016-09-23 at 10.45.52 AM.png

 

 

This workflow of pushing and pulling is very common in git. Usually a team is constantly pushing to a remote and you will need to pull to grab the latest changes. This works well because everyone on the team can individually work on their own features and when they are done they can push it for everyone else to grab.

1 Comment

  1. […] Intro to Git-2: Remotes […]

Comments are closed.