Introduction to Git - Background, Installation, and Usage
Welcome! This article will give a basic understanding of Git and teach you how to save your code to a repository on Github.
Let's get started!
Table of Contents
What is Git?
Git is a free and open-source distributed version control system. In other words, it's an application that keeps track of your code and all the changes that have been made to it in the past. Also, it allows you to share your code with others and collaborate on it without overwriting each other's changes.
Why bother using source control software?
Source control is a very important part of any software development process. Source control management systems are designed to keep history and revision information about the files and projects stored in a repository. By storing this information, you'll have a complete history of everything that happened to the projects within the repository. In the event of a catastrophe, you'll be able to recover a previously saved version. In addition, by saving your changes, you will be able to easily share your work with other developers and collaborate on projects regardless of location.
Origins of Git
Git was created by Linus Torvalds, known as the father of Linux. Linus became frustrated with the process being used by the Linux development community and wanted to create a better way to share code with other open-source developers who were working on the platform. It came into being in 2005 with five primary goals: speed, simple design, support for parallel branches and fully distributed workflows, and the ability to handle large projects. Since its inception, Git has evolved and matured into a world-class source repository and has been adopted by both the open-source community and enterprise development teams.
Why Choose Git?
There are many reasons for choosing Git. Git is cross-platform; thus, it works on Windows, Mac, and Linux. Git is an open-source project and does not have any licensing costs. You may need licensing if you choose to use an established online provider or if you choose to purchase third-party tools, but the system itself does not have licensing costs. Git is also easy to learn and can be very simple, but it offers complex features that you can take advantage of if you need them or as you grow and require more of their use. With its flexibility and availability, it makes for a very compelling choice.
Install & Configure Git
To install Git, run the command that matches your operating system:
$ apt-get install git
$ sudo yum install git
sudo yum install git-core
sudo pacman -Sy git
For other unix and linux distributions: https://git-scm.com/download/linux
Download Git here.
Once the download is finished, open the file and go through the installation process.
Download Git here.
Once the download is finished, open the file and go through the installation process.
Configure Git Username & Email
Since Git is intended to store code on a server where multiple developers can work on it simultaneously, it's important to let Git know who you are so it can track the changes to you.
Once you have Git installed, you can set your e-mail address and username. This information will be saved in the Git configuration at the machine level and will also be used for each of the repositories you create locally.
To set your username and e-mail, open a terminal window and execute the two following commands (replace the highlighted text with your username or e-mail):
$ git config --global user.name "Bob Dole"
$ git config --global user.email "email@example.com"
Your Git username and e-mail are now set!
In this section, we'll go over the role of repositories in the software development process, clone an existing GitHub repository, create a new GitHub repository, and add files to an existing GitHub repository.
Let's get started!
Previously, we mentioned that Git is a distributed source control system. Which means copies of your source code can be stored on multiple computer systems throughout your network and across the globe. You can have a local Git repository on our own machine and have it sync with repositories on other systems to keep each of them up to date.
Okay, so what is a repository then? In its basic form, a repository is a collection of files. When you work on coding projects, you'll be creating various files. A repository is a container for those files.
In Git, every time you make a series of changes that you want to store as a version, you perform a "commit." Further, a Git repository is a series of commits, with each commit being a snapshot of the contents of the files in your project at the point in time you made the commit.
The point-in-time idea is really important to understanding Git. It means that we decide what, how, and when our changes are shared with other users. In Git, each user has his or her own copy of the repository, with all its history of changes. Each user makes changes to that local coy, commits the changes, and then pushes them to a central, master repository. Once pushed, other users can pull your commits so that they get the latest version of the code files.
In a way, using Git is similar to using other file-sharing services such as Google Drive or Dropbox. The main difference is that you sync your files manually instead of it happening automatically using their software.
GitHub is a company that provides free and paid hosting for Git repositories. Most people are not interested in installing and configuring their own Git servers, so GitHub provides servers for use as a subscription-based service. This is convenient because you, as the user, are not responsible for installation, configuration, backups, security, or any other concerns that come from running your own private server.
In addition, since GitHub is a public-facing website, it makes it easy for other developers to download and work with your repositories. In this scenario, the GitHub servers are used as the central source that any user around the world can access with an Internet connection.
Repositories in GitHub can be marked as public or private. Public repositories on GitHub are intended for use with open-source projects that anyone in the world is allowed to search for and clone (download a copy of). By cloning the repository, a user gets a local copy of all of the files to do with what he or she wishes.
Open-source projects use this to allow people to view and edit their code files. However, GitHub also provides controls that you can set up as to who can commit updates to the repositories. Very rarely should you allow just anyone to make updates, since a malicious (or incompetent) user could damage your code files. However, you often want people anywhere in the world to be able to make a suggestion for your codebase. This is called a “pull request.” A user will suggest changes, and the owner of the repository can review the changes and decide whether or not he or she wants to overwrite the central, GitHub copy of the code files with the requested changes.
Private repositories are used for companies or individuals to host their code to share only with people to whom they explicitly grant permissions. They are not searchable or cloneable by unauthorized users.
There are other companies that do version control if you're looking for alternatives. Some of these include Gitlab, BitBucket, SourceForge, Launchpad, and more.
Clone a Repository
For demonstration purposes, we have created a public repository on Github that contains just a basic HTML file. In order to get a copy of the repository on your machine, you'll need to clone the repository.
The cloning process associates a directory on your local machine with a repository hosted on a server (such as GitHub). As part of the cloning process, you will specify where on your computer you wish to store the repository's files and information, and Git will then copy the repository into that location. As part of this process, it will create a hidden directory called “.git” inside the repository folder that is used by Git to keep track of all your local changes. This is a key piece of how the versioning system works.
To clone the sample repository, you will first need to decide on a location on your local drive where you want to keep your repository. A professional developer is usually working on or referencing many different projects that can be stored in many different repositories. Because of this, most developers create a top-level folder (often referred to as a root directory) that will contain all of the repositories you have cloned. This makes it quick and convenient to find the various repositories you are working on.
Steps to Clone the Sample Repository
First, create a folder where you will store all your coding projects (i.e. "repos" or "projects"). In the terminal, you can do so with this command:
$ mkdir projects
cdinto your new folder:
$ cd projects
Now, we can clone the sample repository from GitHub to your local machine. You can see this repository here: https://github.com/coder-rocket-fuel/intro-to-git.
This is a public repository, so anyone can see it.
On that page, you should see both a
To clone the repository, make sure you're inside the directory you created and run the following command:
$ git clone https://github.com/coder-rocket-fuel/intro-to-git.git
You should now see a directory called
intro-to-gitin your folder. The default behavior of the git clone command is to create a subfolder with the same name as the repository in your current directory and copy the files from the server source into that directory.
Creating Your Own Repository on GitHub
Now let's create a repository on GitHub that you can upload projects to.
First, go to https://github.com/.
If you don't have a GitHub account, you'll need to create one.
Once you're logged in, click the "Start a Project" button.
After you press the "Start a Project" button, you will be redirected to another page where you can name your repository, give it a description and more.
When you're done filling out the form, press the "Create Repository" button.
Once you have completed the creation of your repository on GitHub, you can repeat the steps above to clone it to your local hard drive.
You will, however, need to substitute the URL for the git clone command in Git Bash with the URL to your repository. To find that, you can navigate to your repository page in GitHub and click the “Clone or download” button on the page. From there, you can copy the URL to paste in the Bash window. You can see where the URL may be found below:
You can then add code and other files to the directory that is created on your system at the location where you cloned the repository. You will then be able to sync that code with the remote repository, following the steps we will discuss in the next lesson.
Add, Commit, Pull, & Push
The purpose of a repository is to add files and commit them, creating a save point in the history of your code. These save points are how you ensure that files do not get lost, changes are saved, and developers can continue to collaborate on a single project.
Let's go through the four basic commands you'll use most with Git. These commands represent the basic functionality of Git. There are far more commands available, but you should not need them until you begin working with other developers in larger teams or in more complex situations.
To add a file to the repository, you can use the
In your terminal window, you can navigate to the repository you're working with and run the following command:
$ git add -A
By using the
-Aparameter, you're telling Git to stage all of the files in your repository, including new files, modified files, and any removed from the repository.
This command is good to get you in the habit of calling in this manner. It is important to note that the git add command is the only way to add new files to the repository.
It is important to note that even though a file may physically exist in the directory, it will not be tracked by Git and included in your repository until it is "added" to the repository using the add command.
commitcommand will create a new save point in the history of the repository. These are the points we review and care about when it comes to the repository. They provide the save points in the repository where we can get back to the exact state of the files as they existed at that moment we did the commit.
$ git commit -m "ADD_MESSAGE_HERE"
You would replace "ADD_MESSAGE_HERE" in the command above with your own description for this save point in the repository. The message should explain why you are committing this code. Whether you are describing the new page you added or a bug that you fixed, this information will help your collaborators understand the purpose of the changes.
When you or someone else has checked code into a repository from another computer and we need to get those changes on our local machine, you will pull those changes from remote. You can achieve this by running the
git pull command.
The command will appear like this:
$ git pull origin master
Originis the default name of the remote repository, and
masteris the name of the branch we wish to pull from.
It is a good idea to get in the habit of always pulling your repository after you have done a commit but before you do a push. This pattern of add, commit, pull, push will allow you to do the following:
- Add: Start tracking new files and changes when you finish the code you are working on.
- Commit: Create a save point for your changes in the repository.
- Pull: Retrieve other code changes from the remote repository AND merge those changes with your own. This is important because if there are any conflicts between the code you have written and committed and the code that you have pulled from the remote repository, you will need to fix it before you send it to the remote server.
- Push: Send the committed code to the remote server.
Once you have completed an add, commit, and pull successfully, you are ready to send our changes to the remote repository. You would do this by issuing a
Pushing is similar to pulling in that you can specify the remote repository and the branch. It is a good habit to get into. The command would appear as follows:
$ git push origin master
Here again, if we leave off the remote (origin) and the branch (master), then the default remote for the current branch will be used. In either case, this command will take the local changes and attempt to save them on the remote repository.
Git has far more commands than the ones we covered here. It is a complex system with many options for tracking our code changes over time and allowing collaborative work on application source code. However, using a simple workflow like the one we described here works well for most small teams and projects.
If you want to learn more about these commands and the other features Git has, checking out their documentation would be a good place to start: https://git-scm.com/docs
You now have a solid base of understanding of Git and how to use source control in your software development process.
Good luck in your coding endeavors!