Deep Dive in Git & GitHub for DevOps Engineers | #Day09 #90daysof devops

Deep Dive in Git & GitHub for DevOps Engineers | #Day09 #90daysof devops

Introduction:

In the world of software development, version control systems play a crucial role in managing code and collaboration. Among the most widely used systems, Git and GitHub have gained immense popularity due to their efficiency and ease of use. In this blog, we will deal with the significance of Git, the difference between the main and master branches, the distinctions between Git and GitHub, the process of creating a new repository on GitHub, and finally, how to connect a local repository to a remote one.

What is Git and why is it important?

Git is the most popular distributed version control system(DVCS) that used for source code management it is an open-source version control system, designed to track changes in source code during the software development process. Created by Linus Torvalds in 2005, Git allows developers to collaborate seamlessly on projects, maintain a history of code changes, and revert to previous versions if needed.

It is important because it gives the facility to collaborate efficiently, making it easier for the team to work together and reduce conflicts, and errors and also reduce the production time.

The main features of Git are:

  • Tracks history

  • Free and open source

  • Supports non-linear development

  • Creates backups

  • Scalable

  • Supports collaboration

  • Branching is easier

  • Distributed development

What is the difference Between Main Branch and Master Branch?

There's no practical difference between "main" and "master". They're just branch names.

Git:

It has a local repository that's always defaulted the primary branch to be called "master".

GitHub:

GitHub has switched its default branch name to "main" branch because "master" indicates the things like master-slave relationship in the USA because the base of GitHub is situated in the USA, there it is recognized as having bad connotations, that's why they have changed the default branch as the "main" branch.

Difference Between Git and GitHub?

Git and GitHub are often used interchangeably, but they are distinct entities serving different purposes:

Git:

  • Git is a version control system that runs locally on a developer's computer.

  • It allows users to track changes, create branches, and manage code history.

  • Git facilitates collaboration between team members and provides mechanisms to merge changes from different branches.

GitHub:

  • GitHub, on the other hand, is a web-based hosting service for Git repositories.

  • It provides a remote platform where developers can store, share, and collaborate on their Git repositories.

  • GitHub offers additional features like issue tracking, project management, pull requests, and code reviews, making it a powerful platform for open-source projects and team collaborations.

In summary, Git is the underlying technology for version control, while GitHub is a web-based platform that utilizes Git to provide a collaborative environment for developers.

How do you create a new repository on GitHub?

Step 1: Sign in to your GitHub account

If you already have a GitHub account, go to https://github.com and sign in. If you don't have an account, you can sign up for free by clicking on the "Sign up" button.

Step 2: Go to your GitHub dashboard

After signing in, you will be taken to your GitHub dashboard. On the dashboard, you will see a green "New" button on the left side of the page. Click on this button to create a new repository.

Step 3: Set up a new repository

After clicking on the "New" button, you will be taken to the "Create a new repository" page. Here, you'll need to provide some information for your new repository:

Repository name: Choose a name for your repository. This should be a unique and descriptive name for your project.

README: Check this option to create an initial README file for your repository. It's a good practice to include a README to provide an overview of your project.

Step 6: Click "Create repository"

After providing the necessary information for your new repository, click on the green "Create repository" button at the bottom of the page.

The difference between local and remote repositories lies in their location and purpose within the version control system:

Local Repository:

  1. A local repository is stored on your computer's hard drive, and it contains all the files, code history, branches, and version information related to your project.

  2. When you work on a project, you interact with the local repository directly. All code changes and version history are tracked locally before being shared with others.

  3. The local repository allows you to work offline and independently, making it ideal for individual development and experimentation.

Remote Repository:

  1. A remote repository is stored on a server, typically hosted by a service like GitHub.

  2. Its primary purpose is to act as a centralized location where multiple developers can collaborate and share their work.

  3. Remote repositories provide a way for team members to synchronize their code changes, share updates, and collaborate effectively.

  4. When you push your code to a remote repository, it becomes visible and accessible to others.

Connecting Local to Remote Repository: To connect your local repository to a remote one, follow these steps:

  1. Create a Remote Repository:

    • Sign in to your GitHub, GitLab, or Bitbucket account.

    • Create a new repository on the platform by clicking the "New" or "Create" button.

    • Provide a name, description, and other necessary details for the repository.

  2. Initialize the Local Repository:

    • On your local machine, navigate to the root directory of your project using the terminal or command prom

    • If your project is not already a Git repository, run the following command to initialize it:

  3. Add and Commit Your Files:

    • Add the files you want to track to the staging area using the following command:
  4.  git add <file_name>
     or 
     git add .
    

    The period (.) adds all files in the current directory. You can also specify individual files.

  5. Commit the changes to the local repository with a commit message:

     git commit -m "Initial commit"
    
    • Replace "Initial commit" with a meaningful message describing the changes in this commit.
  6. Link the Local Repository to the Remote Repository:

    • In the remote repository on the platform (GitHub, GitLab, etc.), locate the repository's URL.

    • example:

    • In the terminal or command prompt, set the remote URL of your local repository using the following command:

    •    git remote add origin <remote_repository_url>
      

      Here, "origin" is the name of the remote (you can use any name you prefer), and <remote_repository_url> is the URL of your remote repository.

      • To verify that you have added the remote correctly, run this command:

        git remote -v

        This will show you the list of remotes and their URLs.

  7. Push the Local Repository to Remote:

    • Finally, push your local repository to the remote repository using the following command:
git push -u origin master

When you navigate on github and refresh the page you will see the first commit and the same file on github which is in your local repository.

Example:

Tasks:

Till now we have learned the basics and some commands of Git now, lets try to implement these in some 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

Task 1: Set your user name and user email which will be associated with the commits

First of all we have to assign our commits with the user name and user eamil-id in Git, which will be appear on your commit and shows you as the author of the commit.

To set your user name and user email, run these commands in your terminal:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
git config --global user.name "Avanish Singh"
git config --global user.email "rsinghav08@example.com"

Task-2:

a) Create a repository named "Devops" on GitHub:

Go to github.com and make a repository named Devops.

b) Connect your local repository to the repository on GitHub.

As we had already take a look on this topic, how to connect our local repository to the remote repository so, i think we can do the same.

c) Create a new file in Devops/Git/Day-02.txt & add some content to it.

now we create a new file name Day-02.txt in Devops_Git folder.

Now add some content in the file Day-02.txt, to do that we have to use vim Day-02.txt to open a vim editor:

To override this sentence in the file we can write this command in terminal:

You can also append text to the file by using >> instead of >.

If we want to append some text in the file then we have to write this command:

Now for pushing local repository to the remote repository we have use three commands:

  1. git init- to make the git repository on local machine

  2. git add <file_name> : This will add the file or changes to the staging area.

    or

    git add . : This will add all the files or changes to the staging area.

  3. git commit -m "initial commit" : This will create a new commit with the message you specified.

  4. git push -u origin master : This will push the file or files to the remote repository. But before pushing we have to first link the remote repo with local repo using URL of remote repository follow by the command:

    git remote add origin <remote repo URL>

Now repository is pusshed to remote repository.

Conclusion:

We've learnt about branches, repositories, remotes, fetch, push, and other Git and GitHub fundamentals in this blog post. In order to put our knowledge to use, we also completed several projects.

Git and GitHub are strong tools that can assist you in managing your code, working effectively with other engineers, and efficiently deploying your program. You may advance your developer abilities and DevOps knowledge by understanding these tools.

I hope this blog post helped to add some knowledge and if you like this post please share and comment.

Thank You!

#trainwithshubham #Devops #Devopsjourney #Git #Github #push&pull #90daysofdevops.