Skip to content

4. Starting a new project with GitHub

Scott Campit edited this page Feb 21, 2020 · 1 revision

Now that we've downloaded Linux and Git to our machines, let's use our new version control software to track a new project we'll make in this tutorial, as well as work with existing projects.

What is GitHub?

In short, GitHub is a place to share code and packages with other people. It's like the LinkedIn for software developers, in that you can track your daily contributions to your projects and can show off some projects you're working on.

The real power in GitHub is that it allows us to save multiple versions of our code, and access them at any point, as long as we have saved those changes in a push (more on that later).

We will be developing everything in this tutorial using the command line. I will be using basic Linux commands, but for a more comprehensive explanation, check out the suggested reading.

Creating a new project

Let's assume that you're starting from scratch - you have not even made the folder yet! To make your project folder, which we will call test-repo, type in the following to the Linux terminal:

mkdir test-repo

1. Tracking a folder using git init

A repository or repo is a fancy name for a folder that is tracked using Git. To actually start tracking the file with Git, enter the new folder in the command line and initialize git:

# Enter test-repo via the command line using "cd"
cd test-repo

# Initialize git
git init

Adding a file using git add

Great, now we have a directory that is tracked using Git. Let's make an empty file, just to illustrate how to track a file using Git.

touch empty_file.txt

touch is a Linux command that makes an empty file. Now that we have made empty_file.txt, we need Git to track it. We can do this by:

git add empty_file.txt

This specifically makes git track changes we made in test-repo, specifically the addition of empty_file.txt. Note that it is a good practice to continually add files to git as we make them. This ensures that we are aware of what we're adding to a given folder, and not cluttering the workspace. However, if we wanted to track multiple files and not specify them, we would type in:

git add .

This is not recommended, and should only be done if all the files in the directory are ones you want to track using Git.

Telling git to track our changes using git commit

Next, we will run git commit. A commit is a way to capture a snapshot of the changes we've made to the repository using git add. Git does not add your changes unless you tell it to do so. We can type in the following:

git commit

This will pull up the text editor (in our case, nano). Here are some best practices about writing commits:

  • The first line should be a subject line stating the changes we're making to the repository. It should be short, concise, and in one sentence.
  • The remainder of the message should be a detailed summary of our changes. Specifically, it should address the following prompts:
    1. What was the problem with the current code / what is the issue we're trying to solve with our addition?
    2. A brief summary of how you implemented a solution to those problems
    3. Future directions/bugs that are still in the code.

In our case, we added an empty file, which is a relatively small contribution to the project. We can make a commit that is short, and concise in the following way:

git commit -m "First commit; Added empty_file.txt to test-repo"

If your changes are short and sweet, like adding a single text file, you should use the shorter commit syntax. For more complex changes, you should use the full commit to describe the changes.

Adding our changes to GitHub using git push

Finally, we can get to adding our changes to GitHub. First set up your GitHub profile, and make sure your user name does not contain any spaces. You will be assigned a specific base url: git@github.com:UserName.git.

To make a folder that will be saved onto GitHub, type in the following, substituting the UserName with your username:

git remote add origin git@github.com:UserName/test-repo.git

This creates a new folder that is on the GitHub server - a remote repository. Another note: origin is simply the url to your project (git@github.com:UserName/test-repo.git). We are calling it origin in this command so we don't have to type in the url every time we make changes to the remote repository.

Now that we have a place to store our changes on GitHub, we can finally push our changes out from our local system to the remote repository:

git push -u origin master

This command pushes our changes to the remote url (origin) onto the master branch. Two important notes about branches:

  • A branch is a specific container for our code.
    • By making multiple branches, you can have several different variations of the same project. More on that in the later sections.
  • The master branch is a special default branch that can be considered the main version of your code that everyone will see. It is essentially the reference project.

Congrats! You made your first project and added it onto GitHub! This is the basic framework for tracking any local files in your system and adding them to a remote server. Next, we'll cover a more complex task - working on an existing project with multiple people.

Clone this wiki locally