Introduction to Git - Background, Installation, and Usage
Are you new to Git or need an introduction into the version control software?
This article provides a beginner friendly introduction to Git. It will give you a basic understanding of what Git is, how to install and configure Git on your machine, and how to save your code to a repository on Github.
Let's get started!
Table of Contents
Before we go over how to install and use Git, it will be useful to get some background knowledge on the software.
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:
- Simple design
- Support for parallel branches
- Support for fully distributed workflows
- 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
On macOS, you can install Git using homebrew:
brew install git
There are some additional options for installing Git on their website as well.
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 made by 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:
git config --global user.name "Bob Dole"
git config --global user.email "firstname.lastname@example.org"
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 copy, 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.
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:
cd into your new folder:
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-git in 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.
Awesome! You now know how to clone a repository to your local machine!
In the next section, we'll cover how to create your own repository on Github.
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.
You should now have an empty repository created on Github!
In the next section, we'll go through how to add some code to our repository, along with some other useful actions like pulling and pushing.
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.
Initialize Local Code Repository for Git
For each command below, we'll be using the intro-to-git directory you cloned earlier. Before we can work with the directory, we need to configure it with Git.
cd into your
And then run the
git init command to initialize it as a Git repository:
The last thing we need to do is set the remote origin for our repository. This gives Git a URL for where our code is stored remotely for this repository. When you push changes later on, this is the URL that code will be uploaded to.
You can set this URL with the following command (replace the highlighted text with the Git URL of your repository on Github):
git remote add origin https://github.com/your_github_username/name_of_your_repository.git
intro-to-git repository is now configured and ready to go!
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
-A parameter, 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's also 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
commit command 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.
Once you have completed an add and commit successfully, you are ready to send the changes to the remote repository URL we configured earlier. You would do this by issuing a
The command appears 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.
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
origin is the default name of the remote repository, and
master is 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.
But if you are the only person making changes to a code repository, you can skip the Pull step.
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!