Day8&9 #90DaysOfDevOps Challenge

Version control system - Git & GitHub 😺

Git And GitHub

⚡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

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

⚡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

⚡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 <remote repo URL>

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. I already have git installed on my machine.

No alt text provided for this image

  1. Created a local directory - DevOpsRepo

  2. Initialized git in the dir using git bash.

git init

No alt text provided for this image

3. Configured git to use my username and email address.

No alt text provided for this image

No alt text provided for this image

4. Create a new GitHub repo -

No alt text provided for this image

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

No alt text provided for this image

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

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 -

git remote add origin <Remote_URL>

Remote_URL = clone URL from GitHub

To check the current remote repo -

git remote -v

No alt text provided for this image

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

git push origin master

No alt text provided for this image

The changes are pushed successfully. 😁

No alt text provided for this image


Thank you for reading! 📘

Â