# Day-8 : Basic Git & GitHub for DevOps Engineers

### **TABLE OF CONTENTS**

## What is Git?

**Git** is a **distributed version control system (DVCS)** used for tracking changes in source code during software development. It was created by Linus Torvalds in 2005 as a replacement for the proprietary BitKeeper system that was previously used for managing the development of the Linux kernel.

**Git** also provides several features that enable efficient collaboration, such as branching and merging, which allow developers to work on different features or versions of the code in parallel and then combine their changes seamlessly.

## What is Github:

**GitHub** is a web-based hosting service for software development projects that use the Git version control system. It was launched in 2008 and has since become one of the largest code repositories in the world.

**GitHub provides a** platform for developers to store and share their code with others, collaborate on projects, and contribute to open-source software. It also offers a range of tools and features that enable developers to track issues, manage projects, and review code changes.

## What is Version Control? How many types of version controls we have?

![- Evolution Technology Group](https://miro.medium.com/max/1018/1*E4povWZ5H-K3OaguFQJ0IQ.png align="left")

**There are three main types of version control systems:**

1. **Local Version Control Systems:** These systems store revisions of files on a local machine, and typically use a simple database to manage the changes. Examples include the RCS (Revision Control System) and SCCS (Source Code Control System).
    
2. **Centralized Version Control Systems:** These systems store revisions of files on a central server, which acts as a single point of truth for the codebase. Developers can check out files from the server, make changes, and then check them back in. Examples include CVS (Concurrent Versions System) and Subversion.
    
3. **Distributed Version Control Systems:** These systems store revisions of files on multiple machines, and allow developers to work on the same codebase independently, without relying on a central server. Changes are synchronized between machines using a peer-to-peer network. Examples include Git and Mercurial.
    

## Why we use distributed version control over centralized version control?

**There are several advantages to using a distributed version control system (DVCS) like Git over a centralized version control system (CVCS) like Subversion. Here are some of the key reasons:**

1. **Offline Work:** With a DVCS like Git, developers can work on their codebase and make changes even when they are offline or disconnected from the network. This is because the entire repository is stored locally on their machine, rather than relying on a central server. This allows for greater flexibility and productivity.
    
2. **Collaboration:** DVCS systems make it easier for developers to collaborate on the same codebase. Because each developer has their own copy of the repository, they can work on different features or branches without interfering with each other's work. This reduces the risk of conflicts and makes it easier to manage complex projects with many contributors.
    
3. **Branching and Merging:** DVCS systems like Git make it easy to create branches and merge them back into the main codebase. This allows for greater flexibility in managing multiple versions of the code, testing out new features without affecting the main codebase, and collaborating on specific parts of the project without affecting the rest.
    

**Step-1 Install Git on your Instance:**

```plaintext
apt-get install git
```

**Step-2 Check Git and its Version:**

```plaintext
which git 
git --version
```

**Step-3 Configure Username and Email id**

```plaintext
 git config --global user.name <name>
 git config --global user.email <Email.id>
```

**Step-4 Checking details:**

```plaintext
git config --list
```

**Step-5 Creat Account on Git hub :**

## Define Workspace, Staging area and repositories in Git:

1. **Workspace:** The workspace, also known as the working directory, is the directory on your local machine where you make changes to your files. This is the place where you modify, create, or delete files as you work on your project.
    
2. **Staging Area:** The staging area, also known as the index, is an intermediate step between the workspace and the repository. When you make changes to your files in the workspace, you can add them to the staging area to prepare them for committing. The staging area allows you to review and select the changes you want to commit and exclude any changes that you don't want to commit.
    
3. **Repository:** A repository is the place where Git stores the history of your project. It contains all the versions of your files and their complete change history. You can have multiple repositories for a project, including local repositories on your machine and remote repositories on servers.
    

**Step-1 Creat a Directory and Initializes it :**

![How to Resolve Merge Conflicts in Git? | Simplilearn [Updated]](https://www.simplilearn.com/ice9/free_resources_article_thumb/pull-push.JPG align="left")

```plaintext
git init
git init <name of directory>  , if you are not in working directory
```

**Step-2 Creat a file and add it to local repo:**

```plaintext
touch abhi1
git add . <file name>
```

**Step-3 Now if you your file is ready to commit , you can see the status as well.**

```plaintext
git status
```

**Step-4 You can see here this file is now ready to commit. so just commit it :**

```plaintext
git commit -m "Your message"
```

Now if you again check **Git Status** it will blank because there is no file left to commit

```plaintext
git status
```

**Step-5 Just add the central repository here so that you can push your file in Central repo:**

**At first just copy the link of your central repo on Github account:**

**Execute command:**

```plaintext
git remote add origin <https link>
```

**Step-6 Now try to push it into central repo:**

```plaintext
git push -u origin master 
git push -u <branch name>  , if you are in branch
```

it will ask for username and password just go to Github account and create token and use it for both username and password:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679830930486/1d3bb857-f335-4835-960f-145ce6d8498b.png?auto=compress,format&format=webp align="left")

go to &lt;setting&gt; than &lt;developer setting&gt;

![Creating and Uploading a GitHub Project From GUI](https://thecustomizewindows.com/wp-content/uploads/2013/02/Creating-and-Uploading-a-GitHub-Project-From-GUI.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679830987175/fdf10140-3e4b-49c8-92b6-3e7ee85d861b.png?auto=compress,format&format=webp align="left")

Generate classic token :

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679831220567/9598fbee-41e5-4c50-b2e6-49836b9c6ddb.png?auto=compress,format&format=webp align="left")

**Now you can check it onto your Git hub Account:**

**To pull it from github use following command:**

```plaintext
git pull origin master
```

**Note: At present we are on master so we are using origin master, now what if we creat a branch and then try to push it into github lets see:**

## Creating Branch :

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679834563991/3358fa37-1364-47d0-b350-2e45b66d21a7.png?auto=compress,format&format=webp align="left")

In Git, a branch is a lightweight movable pointer to a commit. Branches are used to create a separate line of development, allowing changes to be made to a codebase without affecting the main codebase until the changes have been tested and merged.

Creating a new branch in Git is a quick and easy process. When you create a new branch, you essentially create a new pointer to the current commit that you are working on. You can then make changes to the codebase, and all of those changes will be made on the new branch.

**Branch Creation:**

```plaintext
git branch <branch name>
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679833324815/bf9017da-92cc-4d35-8c3d-c0d8f231f57b.png?auto=compress,format&format=webp align="left")

**To enter into branch which you have created and to check where you are at present:**

```plaintext
git checkout <branch name>
git branch
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679833509701/0ba39064-a5a0-4fd5-acab-2bd7b1f4a8fc.png?auto=compress,format&format=webp align="left")

**Here ,You can see at first , I was in master branch denote with \* then I switch into branch1 using checkout command:**

**now creat a file in it and add it to commit:**

**Now just try to push my new branch into Central Repository:**

```plaintext
git push -u origin <branch name>
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679834051944/210fcbd0-f14a-40f2-a062-426246dc8df2.png?auto=compress,format&format=webp align="left")

**You can see it in your Github Account:**

> ***Please follow me for more Topics on Git , We will cover***
> 
> 1. ***Git Conflict***
>     
> 2. ***Git Stashing***
>     
> 3. ***Git Revert and much more***
>
