Git & GitHub

Git & GitHub

Git

Git is a distributed version control system that is commonly used for tracking changes in source code during software development. It was created by Linus Torvalds in 2005 to manage the development of the Linux Kernel. Git allows multiple developers to collaborate on projects efficiently by providing mechanisms to track changes, merge code from different contributors, and revert to previous versions if needed.

Some key features of git include:

  1. Distributed : Each developer working on a project has their own local copy of the entire repository, including its history. This allows developers to work offline and make commits without needing to be connected to a central server.

  2. Branching and Merging : Git provides robust support for branching, allowing developers to create separate lines of development for new features or experiments. Branches can be easily merged back into the main codebase when ready.

  3. Lightweight and Fast : Git is designed to be fast and efficient, even with large repositories and extensive history. Operations such as committing changes, switching branches, and merging code are typically performed quickly.

Repository

A repository (commonly referred to as repo) is a collection of source code. A repository has commits to the project or a set of references to the commits (i.e., heads).

Commits

A commit logs a change or series of changes that you have made to a file in the repository. A commit has a unique SHA1 hash which is used to keep track of files changed in the past. A series of commits comprises the Git history.

Branches

A branch is essentially a unique set of code changes with a unique name. Each repository can have one or more branches. The main branch — the branch where all the changes eventually get merged into - is called the master. This is the official working version of your project and the one that you will see when you visit the project repository at github.com/yourname/projectname.

Working directory, staging area, and local repo

Every local repo has three different virtual zones. These are:

  • Working directory

  • Staging area

  • Commit area The working directory is where new files are created, old files are deleted, or where changes are made to already existing files.

Once changes are made, they are added to the staging area. The staging area is also sometimes called the index.

Once the changes are complete, the staging area will contain one or more files that need to be committed. Creating a commit will cause Git to take the new code from the staging area and make the commit to the main repository. This commit is then moved to the commit area.

GitHub

GitHub, on the other hand, is a web-based hosting service for git repositories. It provides additionaly features on top of Git, such as collaboration tools, issue tracking, pull requests, and project management. GitHub makes it easy for developers to share their code with others, contribute to open-source projects, and collaborate with teams remotely.

Some key features of GitHub include:

  1. Remote Repository Hosting : GitHub allows developers to host their Git repositories in the cloud, making it accessible from anywhere with an internet connection.

  2. Collaboration Tools : GitHub provides features such as pull requests, code reviews, and issue tracking to facilitate collaboration among developers working on the same project.

  3. Community and Open Source : GitHub is widely used by the open-source community, and many projects are hosted on GitHub. It provides a platform for developers to discover, contribute to, and participate in open-source projects.

In Summary, Git is a version control system used for tracking changes in source code, while GitHub is a web based hosting service for Git repositories that provides additional collaboration and project management features. Together, they form a powerful combination for managing and sharing code in software development projects.

How to Install Git?

Check if Git is installed

First, you will want to check to see if you have Git command line tools installed on your computer. If you have been making repositories of your own code, then you likely have Git installed on your local machine. Some operating systems also come with Git installed, so it is worth checking before you install.

You can check whether Git is installed and what version you are using by opening up a terminal window in Linux or Mac, or a command prompt window in Windows, and typing the following command:

    git --version

However, if Git is not installed, you will recieve an error similar to the following:

    -bash: git: command not found
    'git' is not recognized as an internal or external command, operable program or batch file.

In this case, you should install Git into your machine. Let's go through installation for several of the major operating systems.

Installing Git on Linux

By far the easiest way of getting Git installed and ready to use is by using your version of Linux’s default repositories. Let’s go through how to install Git on your local Linux machine using this method.

Installing Git on Ubuntu or Debian, you can use the apt package management tools to update your local package index. Afterwards, you can download and install the program:

sudo apt update
sudo apt install git

Setting up Git

Now that you have Git installed, you need to do a few things so that the commit messages that will be generated for you will contain your correct information.

The easiest way of doing this is through the git config command. Specifically, we need to provide our name and email address because Git embeds this information into each commit we do. We can go ahead and add this information by typing:

    git config --global user.name "Your name"
    git config --global user.email "youremail@domain.com"

We can see all of the configuration items that have been set by typing:

    git config --list