If you are new to using Git, it may seem a bit overwhelming. Don’t worry this post will take you step-by-step through the basics of Git and creating your first repository.
What is Git?
Git is a version control system that keeps track of the changes made to your source files over time.
Remember the days of constantly copying your files and directories (making manual backups) just in case if you had to revert back to some old working code?
Well thanks to Git, those days are gone.
Not only will Git keep backups of all your changes but Git will also allow you to collaborate with your peers and handle merging all the code back together.
Before we start let’s take just a minute to discuss the difference between Git and GitHub.
What is GitHub?
As I mentioned Git is a version control system that allows you to keep track of changes made to your files on your computer.
GitHub is website that allows you to host your repositories as a way of sharing them with the public (if you choose to) and a way to back them up in the event your computer crashes.
Let’s Get Started
Step #1 – Create a GitHub Account
Start by signing up for a free account on GitHub.com.
Pick your username, enter your email address and a password, and click Sign up for GitHub.
Once signed in, it will look something like this:
Step #2 – Create a New Repository
For each new project, you will want to create a new repository.
A repository is a place for you to store your code files.
To create a new repository, select New Repository from the + sign dropdown menu
Enter a name for your repository and click the Create Repository button. For now don’t worry about changing any of the other options.
Pat yourself on the back! You have created your first repository on GitHub.com.
Step #3 – Create a File
Once your repository is created, you will see a screen that looks like this. This may seem overwhelming, but just focus on the section that starts with “…or create a new repository on the command line,”
Open the Terminal program on your computer and type
If you see a version displayed, that means you already have Git installed. If not, go and install Git on your computer.
Once you have Git installed, create a new directory for your project using the mkdir command. For this exercise we will pretend our project name is demo.
Change your terminal to that directory you just created.
echo “#Demo” >> README.md
This will create a a file named README.md and place the text #Demo inside of it.
To verify this file was created, you can run
This will display the contents of the file README.md.
Next to tell your computer that this directory should be managed by Git, type the following command.
This will create a .git directory which will hold all the git related information for your project.
Checking The Status of Your Files
You can run the following command to check the status of your files:
You can see this is telling me that I have untracked files (files not being tracked yet by git).
Telling Git To Track Files
Next to tell Git to track this file and any future changes to it, type
git add README.md
Now if you run git status again:
You will see your file is now being tracked by git.
You can also use the following command to add all files (instead of specifying the file(s) name).
git add -A
Step #4 – Make a Commit
So you have told Git to track your file, but Git will not store a new version of your file until you issue a commit.
Here is how you issue a commit
git commit -m “created readme.md”
Whenever you issue a commit, you should add a comment indicating a brief summary of what was done since the last commit. This will help you if you ever need to figure out which version to restore back to if need be.
Step #4 – Push Your Changes to Your GitHub Repo
Now we need to connect out Git with our GitHub repository.
Pushing our changes up to GitHub will allow us to have a backup location for our code as well as serve as a location where other team members can access the code as well.
To connect to GitHub, run the following command:
git remote add origin https://github.com/<your_username>/Demo.git
This is telling Git to add a remote called origin with the address https://github.com/<your_username>/Demo.git (i.e., the URL of your Git repo on GitHub.com).
This will allow you to connect with your GitHub repo using the word “origin” instead of always having to type out the URL. If your curious, you don’t have to use the word “origin”, you could use anything you would like in its place.
Now that the remote origin has been added, all that is left is to push our code up to GitHub.
You can do this with this command:
git push -u origin master
Now if you go to https://github.com/<your_username>/Demo, you will see your code has been pushed up to GitHub!
That is it, you have created your first GitHub repository and pushed your demo project up to it.