An Introduction to Git and GitHub - Part 1

An Introduction to Git and GitHub - Part 1

Table of Contents

  • Version Control System
  • Types of Version Control System
  • What is Git
  • What is GitHub
  • How to Install and Configure Git
  • Initialize a Project
  • Create a Repository on GitHub
  • Git Commands to Commit files
  • Push Repository To GitHub
  • Pull a Repository in Git

Version Control System

Version control is a way of tracking and managing changes to code. Version control systems are software tools that help software teams to manage changes to source code over time. As development environments have accelerated, version control systems help software teams work faster and smarter. Version control software keeps track of every modification to the code in a special kind of database. If a mistake is made, developers can turn back the clock and compare earlier versions of the code to help fix the mistake while minimizing disruption to all team members.

Types of Version Control System

  • Local Version Control Systems
  • Centralized Version Control Systems
  • Distributed Version Control Systems

1. Local Version Control Systems

Local Version Control System is one of the simplest forms of version control and is located in our local machines. If the local machine crashes, it would not be possible to retrieve the files, and all the information will be lost. If anything happens to a single version, all the versions made after that will be lost. Also, with the Local Version Control System, it is not possible to collaborate with other collaborators.

2. Centralized Version Control Systems

In the Centralized Version Control Systems, it contains just one server and each user gets their own working copy. Users need to commit changes to the server. Others can see your changes by updating. The problem with these systems is that if the server crashes, almost everything related to the project will be lost.

3. Distributed Version Control Systems

In a distributed version control system, there will be one or more servers and many collaborators similar to the centralized system. These systems update checked the latest version of the collaborator's files and each collaborator will have an exact copy of the main repository on their local machines including the history of the file which they changed.

Each user has their own repository and a working copy. This is very useful because even if the server crashes we would not lose everything as several copies are residing in several other computers.

What is Git

Git is an open-source version control system that let you track the changes you make to your files over time. By using Git you can change your files to various states and the original file will remain the same and this will make updated versions of the same file that you changed over time. You can also make a copy of your file, make changes to that copy, and then merge these changes to the original copy.

What is GitHub

GitHub is a highly used software that is typically used for version control. It is helpful when more than just one person is working on a project. Say, for example, a software development team wants to build a website and everyone has to update their codes simultaneously while working on the project. In this case, GitHub helps them to build a centralized repository where everyone can upload, edit, and manage the code files.

How to Install and Configure Git

  • Install Git

In order to use git, you have to install it on your local machine. Here is the link to the official website git. You can download it according to your system need or by using the command line.

  • Configure Git

After installing git, Open the command prompt and run the following commands to configure git on your PC.

git --version

git config --global user.name "UserName"

git config --global user.email "Email"
  • Initialize a Project

I have created a folder on my desktop called 'Intro to Git and GitHub'. Using the command line, navigate to your new project's location. For me, I would run the following commands:

cd Documents
cd Intro to Git and GitHub

Now to initialize your project, simply run git init. This will tell Git to get ready to start watching your files for every change that occurs. It looks like this:

git init
Initialized empty Git repository in C:/Users/HP/Desktop/Intro to Git and GitHub/.git/

Create a Repository on GitHub

  • Create an Account on GitHub To create a repository you need to make an account on GitHub. Here is the link to the GitHub website.

  • Create a repository You can click on the 'New' button or the '+' dropdown on the top right to create a new repository. Add a name to the repository and click the 'Create repository' button.

s1.png

Screenshot_1.png

Git Commands to Add and Commit files

  • Add files in Git

As we initialized our project in Git but it is not being tracked by git. So we run the following commands to the command line.

echo "# username" >> README.md
git add .
git status
git commit -m "first commit"

'echo "# username" >> README.md' create a readme file while 'git add .' is used to add all files in the project but if you wanna add a specific file you can write 'git add filename' and to check whether the files are added or not used 'git status' command.

After executing this command, you should get a response similar to this:

On branch main
No commits yet

Changes to be committed:
      (use "git rm --cached <file>---" to unstage)
                new file: basics.txt
                new file: todo.txt
  • Commit files in Git

After adding files to git, the next step is to commit these files to git. The first part of the command git commit tells Git that all the files staged are ready to be committed so it is time to take a snapshot. The second part -m "first commit" is the commit message. -m is shorthand for message while the text inside the parenthesis is the commit message.

git commit -m "first commit"

After executing this command, you should get a response similar to this:

[master (root-commit) c792b82] first commit
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 basics.txt

create mode 100644 todo.txt

Push Repository To GitHub

After committing files to git, let's get back to the step where we created the repository in GitHub. As after creating the repo GitHub redirected the repository page. Click on the 'Code' button and copy the text.

git remote add origin https://github.com/user/Intro-to-Git-and-GitHub.git
git branch -M main
git push -u origin main

The command 'git remote add origin github.com/user/Intro-to-Git-and-GitHub.git' creates a connection between your local repo and the remote repo on GitHub and 'git branch -M main' changes your main branch's name to "main" and the last command 'git push -u origin main' pushes your repo from your local device to GitHub. The result will look a like following.

Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 214 bytes | 214.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/user/Intro-to-Git-and-GitHub.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.

To understanding the file stages, I will make changes to the file and then push the new version to GitHub , created a new file in my case i make a file 'new.txt'. Run the git status command to check file status.

git status

The result will look like following.

On branch main
Your branch is up to date with 'origin/main'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        new.txt

nothing added to commit but untracked files present (use "git add" to track)

After this add the file to git and then commit and push the file by using following commands.

git add .
git commit -m "update repo"
git push -u origin main

By using these step, your modified files will be pushed to GitHub.

Pull a Repository in Git

If you want to use some code which is present in a public repository, you can directly copy the contents by cloning or downloading. Pull a repo in git means to cloning a repository from GitHub to your local machine. For this go to GitHub and and search for repository you need to clone then click on 'Code' button to get link or download zip directly. Now go to any folder where you wanna keep the repository and open command line and use following code.

git clone https://github.com/user/Intro-to-Git-and-GitHub.git

This will look like following.

Cloning into 'Intro-to-Git-and-GitHub.git'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.

This will clone a repository from github to your local machine and you can use the repository for own use.

Conclusion

This article covered the basic commands that'll help get you started using Git. We also started learning how to use GitHub. By the end of this article, I hope you are familiar with the basic operations and commands of Git.