How to Use GitHub The Essential Guide
Github is the social file hosting platform for version controls. Founded in 2008, GitHub blossomed to become the world’s largest open source collaborative platform. At the time of this writing, stats show over 12 million users and over 31 million repositories – impressive. From jQuery to AngularJS, HTML5 Boilerplate to Bootstrap, node.js to Linux Kernel, you can find them here.
If you are a developer then Github is the place to be. In this how to use GitHub article, we will show you how to use all essential GitHub features that you need to know to start using GitHub like a pro.
GitHub is powered by Git, a source code version control software engineered by the genius Linux founder Linus Torvalds himself. It was originally developed for Linux kernel development.
Git works on the command line. Projects on GitHub can be accessed and changed using standard Git commands. GitHub is basically and extended version of Git with collaborative features that work on the web.
A repository is storage space (a directory) on a local computer or on a remote server. This is where all your code files reside. A repository can contain all sorts of files including other directories.
Sometimes you will hear the term repo which is just another term for a repository. A GitHub repository means a folder located on the GitHub server.
Version control or source control keeps track of files as they are changed. They help us retain older copies of code with information on what each copy contains. This makes it easy to switch back to an older version of code when something goes wrong.
Software version control is what both Git and GitHub do. If you have ever used the save as option then you have already used your own version control.
Get started with getting yourself a GitHub account. Account for public projects is free while private projects are paid. Once you have your account, download a copy of Git for your platform and install it. We are on Windows. If you are on Linux, then install it using your package manager instead.
Git is a command line tool. If you are not used to the command line then it may take a while to get used to Git environment. But its very easy once you get used to it.
Setting up Git
Once Git is installed, we need to configure it so that it can properly communicate with GitHub. Open Git shell and type the following command replacing “YOUR NAME” with you guessed it, your name
git config --global user.name "YOUR NAME"
Git will use this information to label commits. Next, type the following command replacing YOUR EMAIL ADDRESS with the email you used when creating your GitHub account
git config --global user.email "YOUR EMAIL ADDRESS"
This command associates your email account with Git.
By default, Git will prompt you to enter your account login details every time you perform some online activity. To cache your GitHub password in Git, type the following command in Windows
git config --global credential.helper wincred
Note: Throughout this tutorial, all Git commands from here on should be entered in the Git bash shell.
Creating a GitHub repository
Now that you have your GitHub account and Git installed on your computer, time to create your very first repository. We will use our official GitHub account for this tutorial. Log into your GitHub account. Click the + button next to your username on the upper right corner of the screen.
Enter a name for your repository. You can use the inspirational suggestion if you want. We decided to go with the classic hello-world.
The description is optional. Enter description if you want.
Choose the repository visibility, we are keeping it public.
We will keep Initialize this repository with a README unchecked. If you keep it checked, then GitHub will generate an empty readme file for you. We will manually create our readme file and add it to Git later.
Finally, click the “Create Repository” button. Congrats, you just created your very first repository.
On the next screen, you will receive the address of your new repository.
Creating a Local repository
To create a local repository, we use the Git init command. This command uses the following syntax
git add [project-name]
On your local computer, create a directory for all your Git projects. We are naming it github. Open Git Bash and navigate to the directory you just created. Type the following command to create a new hello-world project
git init hello-world
This command will initialize a new project, create a directory called hello-world with a hidden .git folder inside it. Git uses the .git folder to store all files that it needs to keep track of your project.
Alternatively, you can create the hello-world folder yourself, navigate inside that folder and type the git init command without a project name. It’s a matter of choice.
Cloning a Repository
Instead of creating a local repository every time, you can just clone an online repository and start working right away. This is easily done with the following command
git clone [URL]
Of course, you will need to replace URL with your GitHub repository URL. This will copy the entire remote repository to your local file system. You don’t need to create a separate folder for the project, one will be automatically created for you. To clone the hello-world GitHub repository that we created earlier, use the following command replace the URL with your own
git clone https://github.com/Layerpoint/hello-world.git
Git provides the status command to check how it’s tracking your project files. Project files can reside in many states. Navigate inside the hello-world project folder in the bash shell and type this command
he first thing that status command tells you is the branch you are using. We will look at branching later. For now all you need to know is that we are currently on the default branch which is the master branch.
The next line informs you that we are yet to make our first commit. But wait, we don’t have anything in our project yet. And that’s what the command tells us in the final line.
Every project should contain a readme file. Let’s create one. It doesn’t matter how the readme file is created. Let’s create one using the following command
The .md extension represents Markdown language file. Anyway, let’s check the status once again using the status command
We have a new section called Untracked files which lists all files that Git has found in the current project. Git doesn’t really care about any of this files unless you explicitly tell it to.
The final message has also changed. This time, Git is telling you that there are untracked files which can be added for a commit.
Adding a file
Git provides a multipurpose add command which performs different actions based on when you are using it. It has the following syntax
git add [filename]
where filename is the name of the file or a directory. If the filename is a directory, then all contents of the directory are recursively added. For an untracked file, the add command tells Git to start tracking the file.
The add command records a historical snapshot of the file at the time you run the command. This means that when you commit next time, whatever that was in the file at the time you ran the command will be recorded in the historical snapshot.
Simply put this command tells Git – “Hey Git buddy, add whatever is inside my file file right now to the next commit”. What it does not tell Git – “Add the newest version of this file to every commit bro”.
Let’s start tracking our readme file using the following command
git add README.md
Use the status command once more to see our file under Git version control
The untracked section is now gone. Instead, we have a new Changes to be committed section and our readme file is located there.
Note: In Git, a staged file represents a version of the file which is ready for commit.
Add something to the readme file, save it and run the status command again
This time, we have yet another new section called Changes not staged for commit. All files which have changed since the last time they were added to Git version control will be present here.
Git has noticed that changes were made to our readme file. At this point, we have two options, we can use the add command to update the Git staged version of our readme file to match the current version or we can undo our changes to the readme file and restore the file with the Git staged version using checkout command.
When you commit, you record your changes permanently in the Git repository. This command uses the following syntax
git commit -m 'a message'
Every commit needs a message, a short summary that describes the changes you are making.
Now it’s time for your first commit. Add the current version of the readme file using the add command and type the commit command
git commit -m 'Finish readme'
If you check type the status command now, you should see something similar to this
Git reports a clean working directory with nothing to commit. You are so cool! Made your first commit as if it was nothing.
Connect your local repository to GitHub repository
Making changes in the local file system is fast and time-saving. We don’t need any internet connection. But if we want other people to see what we have been cooking, we need to host it on GitHub.
To connect to our GitHub repository, we use the remote add command that has the following syntax
git remote add [name] [URL]
Where name represents a remote name like origin and URL is the URL to your GitHub repository. Type the following command replacing the URL with your GitHub project URL
git remote add origin https://github.com/Layerpoint/hello-world.git
Git is now connected online. To check the list of tracked repositories, you can use the following command
git remote -v
Now that we are connected to GitHub, we can finally upload our project to GitHub. We do this by using the push command. It uses the following syntax
git push [alias] [branch]
where alias represents the destination of the push operation. It can be an absolute URL or a named remote GitHub repository that was added using the remote add command. Branch represents the branch of your project for which the change has to be recorded. Type the following command
git push origin master
hat command will update the master branch of the online repository identified by origin. And there you go, your project is now hosted on GitHub, ready for the world to see your greatness.
Before we wrap up this tutorial, we want to talk about branches. The entire GitHub workflow is based on branching, a key concept of Git. Branching is the way to work with different versions of the repository at the same time. We use branches to test out new features before adding them to the final product. Branching is also the way how other people work on projects together.
By default, all projects use the default master branch. When we create a new branch, we create a clone of the master branch at that point in time. If someone changes the master branch, while we are working on our branch, we can pull the changes made to the master branch.
We can create a new branch using the checkout command with the following syntax
git checkout -b branch-name
where branch-name is the name of the branch we want to create. This command does two things, it creates the new branch and then it does a checkout. You can also achieve the same result using a combination of the branch and checkout commands
git branch branch-name
git checkout branch-name
Let’s create a new branch. We will call it license-branch. Type the following command
git checkout -b license-branch
You work with your own branch just like you do with the master branch. Whenever you add, edit, delete or commit, you are changing your branch. The changes you make to your branch do no affect the master branch in any way. The commits keep track of the changes made to your branch.
Let’s add a license file to our branch now. Use the following commands to create a license file and add it to Git (making sure you are on the lincense-branch)
git add license.txt
Now let’s commit our branch. You how to do it right?
git commit -m 'Add license'
At this point, if you switch back to the master branch, you won’t find the license file. That’s because it’s not available in the master branch.
If you are working on a community project, then you submit a pull request for your branch. You can create a pull request at any point to start a discussion, even when your code isn’t ready.
The pull request initiates the discussion phase. Other developers will discuss and review your code. This is where the commit messages come in handy. They explain what you have done and why.
Finally, when the code is verified, its’ merged into the master branch. To merge a branch, we use the git merge command that uses the following syntax
git merge [branch-name]
where branch-name is the name of the branch you want to add. Before you run this command, you need to checkout the branch where you want to apply the changes.
Let’s add our license branch to our master branch. Use the commands below
git checkout master
git merge license-branch
Our license branch has merged with our master branch. You can now find your license file in the master branch. At this point, we can delete the license branch. To delete a branch, we use the following syntax of branch command
git branch -d branch-name
To delete our license-branch, this would be
git branch -d license-branch
Go on now, create other branches and test them out for yourself.
Just like GitHub says, celebrate! You have just learned how to use one of the hottest things in the programming world today – GitHub. Both Git and GitHub are immensely powerful tools, with an equally powerful community behind them. No wonder they are so popular.
We hope you found it useful. Let us know how you liked it in the comments sections below. Keep learning and have fun.