With Git you can manage your code locally on your machine. If you're working on a bigger project, there is basically no way around making your code available for other team members though. One of the most common providers of such a service is GitHub.
You never worked with Git? Make sure to dive into our Crash Course first to get you started!
If you're just looking for the basic GitHub commands, check out the end of the article.
Related Premium Courses
#Getting Started with GitHub
How can we upload code from our local Git Repository to the cloud into a so-called remote Repository and what do we need to get started? A local Repository and an account on github.com - that's it!
#Creating a Remote Repository
After successfully creating an account, you can create a new remote Repository right here:
But we have a local Repository already, so why should we create another one? The idea is to create an empty Repository on GitHub and to then push (i.e. upload) the content of the existing local Repository into the remote one.
After providing a Repository name and an (optional) description, we have to decide if we want to create a public or private Repository.
As the name indicates, public Repositories can be seen by anybody on your GitHub page, private Repositories are available for selected people only.
For this demo project we'll select "Public" here.
"Initialize this repository with a README". Tick this box if you don't have any existing code you want to push to this new Repository (without the readme file, the Repository wouldn't contain a Branch then). With our local Git Repository we do have an existing project, therefore we will add content soon, this means we can keep this box unticked.
Clicking "Create Repository" will now create an empty remote Repository. Adding the content of the existing local Repository can be done as described on the page we automatically get redirected to:
The important command is
git remote add origin URL. This command needs to be executed in your local project folder (i.e. the folder that contains the Git Repository you want to push to GitHub):
remote add: Adds a remote Repository to our Git project. In other words, it establishes a connection between the local and the remote Repository
origin: This is the name of the remote Repository. It can be named different, but typically it's called origin so we'll also stick to this convention
URLis the URL of the remote Repository on GitHub (our data should be pushed to the correct address shouldn't it?). GitHub automatically adds the address as we can see on the screenshot
After running the command in the local project folder,
git remote displays the remote Repository in our terminal (
git remote -v also shows the URL of the Repository). This confirms that we successfully established a connection between our local and remote Repositories, but we didn't exchange any data up to this point.
git push -u origin master allows us to push (i.e. upload) the content of our local Repository to the remote one.
-u origin master instructs Git to create an upstream to a specific Branch and Repository, meaning that we want to upload data to the "master" Branch of our "origin" Repository.
GitHub now asks for our credentials and if we then reload our GitHub page, we should see that our Branch and the Commit(s) of the local Repository were pushed to GitHub.
Entering your username and password whenever you push code to GitHub is not very convenient, this step-by-step guide shows how to cache these data. Entering the username and password once is sufficient afterwards.
#Local, Remote & Remote Tracking Branches
Time to dive a bit deeper into the connection between Git and GitHub:
git branchdisplays all current Branches in our local Repository
git remoteshows the remote Repository we created on GitHub
git branch -rdisplays the remote tracking Branch
The local Branches are the Branches of our local Repository on our machine, the remote Repository is the Repository in the cloud (on GitHub in our case), I guess that's clear. But what about the remote tracking Branch?
The remote tracking Branch is the so-called local representation of the remote Branch. Previously we explicitly used
git push -u origin master to tell GitHub where we want to push our code to - the master Branch in the origin remote Repository.
After the first time we used "push" in this explicit way (the same is also true for "pull" and other commands like "clone", more on that in a second), this remote tracking Branch is created automatically.
With the information of this remote tracking Branch, Git knows that
git push should upload code to "origin/master" (our master Branch in the origin remote Repository) and with that the right code can be exchanged between the right Repositories and Branches.
This allows us to use a shorter form of these commands in the future as Git now is aware of the "address" of our remote Repository and the corresponding Branch.
Whenever we're working in our local master Branch, entering
git push is all we have to do now to update the code in our remote Repository. As a sidenote: With "push" the data in both the remote tracking Branch and the remote Branch will be updated.
We need to make sure that all three Branches (the local, the remote tracking and the remote one) are synchronized though. Let's continue with our work on GitHub to see why this is important.
#Granting Repository Access to Other Users
The "Clone or download" option in our remote Repository provides a URL that can be shared to give others users Repository access (that's the same URL we used previously when we pushed our local Repository to GitHub):
Let's create a new empty project which should contain our code from the GitHub Repository. We have the URL already, with
git clone URL ("URL" should be the URL we just grabbed from GitHub) the entire remote Repository is copied into the folder and also turns this folder into a folder managed by Git - it's as easy as that.
Also make sure to watch the video at the top of this page, where you see all these steps in even more detail!
We cloned our Repository and we're also able to push data to this remote Repository,
git push is all we need here.
git push sufficient? Because
git clone automatically created the remote tracking Branch which contains the information about the Repository and the Branch of our remote Repository. As Git knows where we cloned the data from, we can also push the data back to this "address" now.
You can always check the existence of the remote tracking Branch with "git branch -r". Whenever you use "git push" both the remote tracking Branch and the remote Branch gets updated with the information from the local Branch.
Important: You can control who has access to your GitHub Repository. If your Repository is public, people can always clone it but you can lock down pushing.
#Pulling Data from GitHub
Pushing code or cloning a Repository is great, but we're also able to receive updated information even after we cloned the data.
git fetch downloads information from a remote Repository to the remote tracking Branch. That's important: The remote tracking Branch will be updated, for the local Branch that's not the case.
As a second step we need to synchronize our local Branch and the remote tracking Branch. We learned the required command in the Git Crash Course - a merge between two Branches does the job here. After the successful merge, our three Branches are synchronized again.
A more convenient way to achieve the same result is
git pull which basically is a combination of
git fetch and
We can also delete remote Repositories. On GitHub go to this "Settings" page:
If you scroll down to the "Danger Zone" you can find the option to delete a specific Repository. After confirming the name, the Repository is deleted. But be careful, if the Repository is deleted it's gone so you can cause big trouble here!
Besides deleting the Repository from GitHub, we can also remove remote Repositories from our Git project - so the connection between Git and GitHub, the Repository won't be deleted of course. Just enter
git remote rm origin - make sure to check your remote Repositories with "git remote" first, as written above remote Repositories can also be named differently then "origin".
#GitHub Core Commands Overview
You quickly need to look up a specific term or command? Here we go:
git remote add origin URL: Establishes a connection between the local Git Repository you're currently working on and the remote Repository (URL should be the URL of this Repository)
git remote -v: Displays all remote Respositories your local Repository is connected to ("-v" also shows the URL of the remote Repository)
git push -u origin master: Upload code from your local Branch to a remote Branch in the remote Repository ("origin" is the name of the remote Repository, "master" the name of the remote Branch)
git clone: Copy or clone an entire remote Repository to your machine. Automatically creates a remote tracking Branch (see below)
git pull: Upload code to or download code from a remote Repository. Only possible after a remote tracking Branch was created
- "remote tracking Branch": Branch "in between" the local Branch and the remote Branch. Ensures that code is pushed or pulled between the right Repositories and Branches. Automatically created when using "git clone" or "git push"/"git pull" explicitly (i.e. with information regarding the name of the remote Repository and Branch)
git fetch: Updates the remote tracking Branch with the code from the remote Branch
git remote rm origin: Removes the connection between the local Repository and the remote Repository, "origin" is the remote Repository`s name