When writing my last post about setting up a Static Site using GitHub Pages I realised I haven’t covered what GitHub is or what makes it so special (apart from hosting sites for free). I am here to go into more detail about GitHub so you have a better understanding.
What is GitHub?
GitHub is a system used for Version control, allowing the management of changes and also store revisions of projects as they are being developed. The core that GitHub is built on is Git, an open source project started by Linux creator Linus Torvalds.
GitHub is a Git repository hosting service, but it adds many quality of life features. Git is a command line tool while GitHub is an online system. It also allows users to set access control and collaboration features like descriptions and task management tools.
How do I start?
- First thing you will want to do is sign up for a Github account.
- Once you have an account set up, install a git on your machine. We are going to be using the CLI (Command Line Interface) for this example.
- To confirm that your git is installed open a terminal window and type
Create a new repo
Create a new repo (repository) for tracking the code changes within your project.
- Create a directory for the project.
- Go into the new directory.
- Create a file and write some code
git addto add the files
Now you have created your first Git Repo and committed some code to it.
Create a Readme
A good file to create when when starting a Git Repo is to create README.md file to describe the project. You will need to create the README.md file in the top level directory of the project. GitHub and other code hosting services such as Bitbucket and GitLab will look for your README.md and display it along with the list of files and directories in your project.
You can write the README.md file, either as plain text or with Markdown. Markdown allows you to add a bit of text markup, like hyperlinks, bold/italics, or to
indicate code. Markdown is easily converted to html for viewing in a web browser, and GitHub will do this for you automatically.
Create a repo from an existing project
What if you want to create a repo from a project already on your machine?
- Go into the directory containing the project.
git addto add all of the relevant files.
- To hide any sensitive information you will want to create a
.gitignorefile, this is used to indicate all of the files you don’t want to track. Use
git add .gitignore, too. You can read more about the .gitignore file here.
Now you have turned your project into a Git Repo and committed your code.
Connect your Git Repo to GitHub
You’ve now created a local Git repository. You can use git locally, but what you want is the repo up on GitHub.
- Go to GitHub.
- Log in to your account.
- Click the new repository button in the top-right. You will also have an option there to initialize the repository with a README file if you haven’t already.
- Click the “Create repository” button.
Now, to push your repository to GitHub run the following in the Repository directory.
$ git remote add origin email@example.com:username/new_repo $ git push -u origin master
If you don’t want to put in your password every time you push to GitHub you can set up ssh in your account. You can the push the Git repo then by running the following line in your terminal.
$ git remote add origin https://github.com/username/new_repo $ git push -u origin master
Recommended GUI Git
If you are not a fan of typing in terminal I recommend Sourcetree, an Atlassian program that allows simple interactions with GitHub.