# Git & Git-Hub

![Git And GitHub](https://media.licdn.com/dms/image/D4D12AQGkDoEi7nFdpw/article-cover_image-shrink_720_1280/0/1683568482776?e=1691625600&v=beta&t=XB6MgiM4eC6RFcyFfGdg1LRZhMTm2JrXwJtt4Yfp2cg align="left")

## ⚡Version Control system:

The version control system helps us keep track of our changes by storing all the version history.

There are two types of VCS-

1\. Centralised VCS - Where the work is done directly in a remote repository. To check in the changes and share them with the team, one has to connect to the internet. Ex. TFVC, Subversion, etc.

![No alt text provided for this image](https://media.licdn.com/dms/image/D4D12AQGRT2itBeZIBg/article-inline_image-shrink_1500_2232/0/1683572518026?e=1691625600&v=beta&t=tBdyEVTDYv_Eu9363QVhkNDgA1yp5YmecH7ekjl8-ns align="left")

2\. Distributed VCS - Where we have a remote repository at the centralized location as well as a copy of it can be stored in every user's Local machine which is called a local repository. Users can commit changes locally and push them to the remote side all at once, by connecting to the internet. Ex. GitHub, Azure Repos Git, GitLab, etc.

![No alt text provided for this image](https://media.licdn.com/dms/image/D4D12AQGsqo9pRaVe0Q/article-inline_image-shrink_1500_2232/0/1683572619744?e=1691625600&v=beta&t=k4RQiYYnsh3Qf17JgAfbBTSHzMoTYlI6gmNAqUdoe_0 align="left")

## ⚡What is Git:

Git is an open-source distributed version control system, which is used to store our source code, and helps in keeping track of our history of changes by storing all the versions of our files.

It was created by Linus Torvalds in 2005 for the development of the Linux kernel and has since become one of the most widely used version control systems for software development.

### Why Git is important:

Git is an essential tool for developers, some of the reasons are as below -

version control - It helps them to keep track of their history of changes and check the previous versions whenever required.

Collaborate - It makes it easy for all the team members to clone a copy in their local machine and contribute according to the assigned task then send their changes to the main code base. It also provides a tool for reviewing changes and resolving conflicts when multiple developers are working on the code.

Branching and Merging - It allows developers to create multiple branches to isolate different stages like dev, release, hotfix, etc. as well as their work to avoid conflicts with others. Once the work is done on a branch it can be merged with other branches. There are many ways of merging the code, which gives more control over the history of the codebase and how you want to share your changes with others.

Backup and Recovery - By storing code and its history in a repository, developers can easily recover previous versions of their code if something goes wrong.

## ⚡Difference between Git and GitHub:

![No alt text provided for this image](https://media.licdn.com/dms/image/D4D12AQHcDUwQUny9aQ/article-inline_image-shrink_1500_2232/0/1683573680158?e=1691625600&v=beta&t=XoBQo9WECsKfsQvrZffogAEX6zpdlcMPM3VH4lrZu3o align="left")

## ⚡Difference between the Master branch and Main Branch in Git:

In git **Master** and the **main** branch refer to the same branch. Initially, the default branch was named Master but over the period some people raised a concern about the name Master due to its association with 'Slavery'. In response, the Git project decided to rename the default branch to "main" starting with Git version 2.28.0. So now the default branch name is "**Main**".

The purpose of this branch is to be the main branch of development and to have the latest stable version of the code.

## ⚡Difference between Remote and Local repo:

* The Local repo is stored on a local machine while the remote repo is hosted on remote servers like GitHub.
    
* There can be multiple local repositories for one remote repo. All team members can clone the remote repo in their local machines and have a copy of it.
    
* Local repo is used for individual development contributions while remote repo is for collaborative development where multiple developers can share their changes to the same codebase.
    
* The local repo can only be accessible from a local machine while the remote repo can be accessed from anywhere with an internet connection and can be integrated with other tools like CI-CD to automate the software development lifecycle.
    
* A remote repository serves as a backup for your local repository, providing a centralized location where changes can be pushed and pulled from.
    

### ⭐Connect local repo to remote repo:

If we have initialized a repository locally and want to connect it to our existing remote repo, we can use the below steps-

1. Initialise repo locally -
    

> git init

2\. Add a remote repo as the origin -

> git remote add origin &lt;remote repo URL&gt;

3\. Check the remote repo for this local repo -

> git remote -v

### ⭐Git Configuration settings:

There are 3 types of git configurations-

**System Git configuration** file settings apply to all users and repos on your computer.

> Git config --list --system

**Global Git configuration** file settings apply to the current user and their repos.

File location - C:\\Users\\\\.gitconfig

> Git config --list --global

**Local Git configuration** file settings apply to a local repo. To view those settings, run the following command in the root folder of a repo.

File location - .git directory: .git/config

> git config --list --local

You can tell Git to prune remote branches during every fetch to remove stale remote-tracking branches in your local repo that no longer exist on the corresponding remote repo.

> git config --global fetch.prune true

---

## ⚡Tasks:

Task-1:

* Set your user name and email address, which will be associated with your commits.
    

Task-2:

* Create a repository named "Devops" on GitHub
    
* Connect your local repository to the repository on GitHub.
    
* Create a new file in Devops/Git/Day-02.txt & add some content to it
    
* Push your local commits to the repository on GitHub
    

### ⭐**Steps:**

To install git on your machine, follow the [official link](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git). I already have git installed on my machine.

![No alt text provided for this image](https://media.licdn.com/dms/image/D4D12AQF-fJ8qVStTNQ/article-inline_image-shrink_1500_2232/0/1683718823127?e=1691625600&v=beta&t=1XUAEVqEjJtDUikW5CrX3LSPrBHb6DrC2uKerhzC7fU align="left")

1. Created a local directory - DevOpsRepo
    
2. Initialized git in the dir using git bash.
    

> git init

![No alt text provided for this image](https://media.licdn.com/dms/image/D4D12AQFYO6ygtFOujw/article-inline_image-shrink_1500_2232/0/1683718936085?e=1691625600&v=beta&t=j4Mk_BIRqtwqJuafbL5xAPERTeGhp0om4cAsRKgQsuo align="left")

3\. Configured git to use my [username](https://docs.github.com/en/get-started/getting-started-with-git/setting-your-username-in-git) and [email address](https://docs.github.com/en/account-and-profile/setting-up-and-managing-your-personal-account-on-github/managing-email-preferences/setting-your-commit-email-address#setting-your-commit-email-address-in-git).

![No alt text provided for this image](https://media.licdn.com/dms/image/D4D12AQHLt7Fu7AgzMw/article-inline_image-shrink_1500_2232/0/1683719182446?e=1691625600&v=beta&t=Xyrld1k9vV-7GBASJPNHYU8BCMEvlr9A-wWRrqK4mCk align="left")

![No alt text provided for this image](https://media.licdn.com/dms/image/D4D12AQFceuEPBv0h6g/article-inline_image-shrink_1500_2232/0/1683719171048?e=1691625600&v=beta&t=x3muStB0vl_Od2jLoddQ0MRHiD49uC19dqztzdxoxAs align="left")

4\. Create a new GitHub repo -

![No alt text provided for this image](https://media.licdn.com/dms/image/D4D12AQEFURBLJTjaZA/article-inline_image-shrink_1500_2232/0/1683719332535?e=1691625600&v=beta&t=RuzlelEUuHYadiSNL4irAlVtOKtu3vGjPBh4An5OkVc align="left")

5\. Created a new file in the local git workspace -

![No alt text provided for this image](https://media.licdn.com/dms/image/D4D12AQEIVJ6SCtAqhg/article-inline_image-shrink_1500_2232/0/1683720022731?e=1691625600&v=beta&t=zlWnEi5z9p4wJEGOhjVTdUz-8PR6BE8XCNxowdXgarg align="left")

6\. commit this change to the local repo -

It shows if the changes are staged or not.

Red - unstaged (untracked by git)

Green - staged (tracked by git)

> git status

To stage all the changes in the working space.

> Git add .

To commit the changes.

> Git commit -m "commit message"

![No alt text provided for this image](https://media.licdn.com/dms/image/D4D12AQGNFvqvX4IhAg/article-inline_image-shrink_1500_2232/0/1683720047658?e=1691625600&v=beta&t=joEML9ywvX203S5RFf2aNVHPxyTO2DBSrRiUKP88SkA align="left")

7\. Now to push this commit to our existing repo in GitHub, we need to add the GitHub repo as a remote repo in our local. To [connect the local repo to the GitHub repo](https://docs.github.com/en/migrations/importing-source-code/using-the-command-line-to-import-source-code/adding-locally-hosted-code-to-github#adding-a-local-repository-to-github-using-git) -

> git remote add origin &lt;Remote\_URL&gt;

Remote\_URL = clone URL from GitHub

To check the current remote repo -

> git remote -v

![No alt text provided for this image](https://media.licdn.com/dms/image/D4D12AQF0hsRH8Gl7gA/article-inline_image-shrink_1500_2232/0/1683720193119?e=1691625600&v=beta&t=Zb3DQ1lO4ZvorQ-IjAJjEBiovE0uMFi7ELJzHAgcr7k align="left")

8\. Now push the changes to the remote GitHub repo -

> git push origin master

![No alt text provided for this image](https://media.licdn.com/dms/image/D4D12AQGCyjuuuzcjaQ/article-inline_image-shrink_1500_2232/0/1683720511336?e=1691625600&v=beta&t=DDbbRQAwTmOCGeGXWtHksLxwN254w7YUIV5XzSVnTAo align="left")

The changes are pushed successfully. 😁
