An Introduction to Git

An Introduction to Git

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.

Originally published at https://coderrocketfuel.com

Introduction

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

  •  Background
  •  Install & Configure Git
  •  Using Git

Background

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:

  1. Speed
  2. Simple design
  3. Support for parallel branches
  4. Support for fully distributed workflows
  5. 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

Install

To install Git, run the command that matches your operating system:

Linux

Debian/Ubuntu:

$ apt-get install git

CentOS:

$ sudo yum install git

Fedora:

sudo yum install git-core

Arch Linux:

sudo pacman -Sy git

For other unix and linux distributions: https://git-scm.com/download/linux

Mac

Download Git here.

Once the download is finished, open the file and go through the installation process.

Windows

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 (replace the highlighted text with your username or e-mail):

$ git config --global user.name "Bob Dole"
$ git config --global user.email "[email protected]"

Your Git username and e-mail are now set!

Using Git

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!

About Repositories

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

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

Then cd into your new folder:

$ cd projects

Here are some links with more information on the mkdir and cd commands.

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 index.html and README.md file.

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. 

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading

How To Use Git: A Reference Guide

Setting up Gridsome with GitLab, NetlifyCMS and Netlify

Basic Git/GitHub Cheat Sheet

Getting Started Faster with Git and GitHub

git github

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Git It? How to use Git and Github

Learn how to use Git and Github 🐙🐱 Contributing to open-source is satisfying on many levels. Not only does it validate your skills for recruiters, but it leads to higher quality software that empowers developers to build better products. But submitting your first pull request on GitHub can be intimidating. The goal of this guide is to give you a clear set of steps for contributing any OS project

Basic Git Commands you need to Master

All basic commands you need to know to run git .Basic Git Commands you need to Master

Git for Beginners - Git & GitHub Fundamentals In Depth

This git tutorial for beginners is designed to teach you the git command line tool and how it works with github. This beginner git tutorial teaches github fu...

Git and GitHub first steps

Hello guys , this is my second post on medium. I am taking a data science bootcamp course at awari.com.br and I intend to write about the topics that I am going to learn. First because I want to share a little of my knowledge and it’s a great way to learn as well. Today I am going to write about Git and Github , explain what they are and describe some basics commands on git.

7 Best Practices in GIT for Your Code Quality

Git plays a significant role in software development. It allows developers to work on the same code base at the same time. Check out 7 best practices for Git.There is no doubt that Git plays a significant role in software development. It allows developers to work on the same code base at the same time. Still, developers struggle for code quality.