Skip to main content

Command Palette

Search for a command to run...

Deep Dive in Git & GitHub for DevOps Engineers

Today is Day9 of 90DaysOfDevOps

Published
6 min readView as Markdown
Deep Dive in Git & GitHub for DevOps Engineers
S

Hello, I am sumit and currently pursing my final year of graduation with IT stream from JSPM BSIOTR Wagholi Pune. I am currently learning DevOps with TrainWIthShubham. I have prior knowledge of Java, JSP, Servlet, SQL and Data structure also.I am a hard worker, smart and quick learner.

What is Git and why is it important?

  • Git is a version control tool

  • Using git we can manage the versions of files

  • Git is a distributed version control system that is used in software development for tracking changes in source code

Importance of Git:

  • Version control: - Version control means suppose, we make one file in the file system as hello and we add it on git now in the file system suppose we override this file with some more contents and add it on git now our old file changes are overridden in the file system but on git i.e. VCS there our first add file is treated as version1 and recently overridden added file treat as version2. So whenever we want our first created file we can get it from git. Here we manage the versions of files.

  • collaboration: - Git is a distributed version control system. As a name distributed, Git allows multiple developers to work on the same project. It provides a structured way to merge and manage changes made by different team members, making it easier to collaborate on large and complex software projects.

  • branching and merging: - Branching is a very important part of git. By default master branch is present and it is a main branch. We can create a new branch also by using the master branch. when we create a new branch whatever is present in the master branch that all comes in the new branch also. So the developer can make his changes in the new branch if the changes are perfect and ready to go in the main branch then a new branch is merged with the master branch. The master branch is the main branch because it is production-ready code. whatever is present in the master that is published for the end user.

  • Distributed development: - Git is a distributed version control system, which means that each developer has a complete copy of the entire project's history and can work independently, even when not connected to a central server. This decentralization enhances flexibility and resilience.

  • History and Accountability: - Git provides a complete history of changes to a project, which is essential for debugging and understanding how the code has evolved.

Difference between Git and GitHub?

Git:

  1. Git is a distributed version control system (VCS) used for tracking changes in source code during software development.

  2. It is a command-line tool that runs locally on a developer's machine and doesn't require an internet connection to work.

  3. Git allows you to create, manage, and switch between branches, merge code changes, and track the entire history of a project.

  4. It's designed to be lightweight and fast, making it efficient for both small and large software projects.

  5. Git is not tied to any specific hosting service or platform; it can be used independently or integrated with various services.

GitHub:

  1. GitHub is a web-based platform that provides hosting for Git repositories and adds a layer of collaboration and project management features on top of Git.

  2. It allows users to store, share, and collaborate on Git repositories in a centralized way.

  3. GitHub offers a graphical user interface (GUI) for managing Git repositories, in addition to web-based tools for issues tracking, code review, and project management.

  4. GitHub provides features like pull requests, code reviews, and continuous integration (CI) for enhancing collaboration and development workflows.

  5. GitHub is one of several web-based Git repository hosting services, with others like GitLab and Bitbucket offering similar functionality.

Create a new repository on GitHub

  1. Sign In to GitHub: If you have a GitHub account, sign in to your account. If you don't have one, you can create a GitHub account for free.

  2. Navigate to Your Dashboard: Once you're logged in, you'll be taken to your GitHub dashboard.

  3. Create a New Repository: Click on the "+" sign in the top right corner of the GitHub interface, and then select "New repository" from the dropdown menu.

  4. Fill in Repository Details: You will be taken to a new page where you need to provide information about your new repository:

    • Repository name: Choose a name for your repository. This should be unique within your GitHub account.

    • Description (optional): Add a brief description of your repository's purpose.

    • Visibility: Choose whether your repository should be public (visible to anyone) or private (only accessible to you and collaborators).

    • Initialize this repository with a README: You can choose to create an initial README file for your repository. This is often a good idea to provide an overview of your project.

  5. Click "Create repository": Once you've filled in the necessary information, click the "Create repository" button.

What is the difference between local & remote repositories?

Local Repository:

  1. A local repository is a copy of a project's code and its complete history that resides on your local computer or development environment.

  2. It allows you to work on your project, make changes, and track those changes using Git without the need for an internet connection or interaction with a remote server.

  3. You can commit changes, create branches, and manage the entire history of your project locally.

Remote Repository:

  1. A remote repository is a version control repository hosted on a server, typically on the internet or an internal network. Common remote repository hosting services include GitHub, GitLab, Bitbucket, and self-hosted Git servers.

  2. The remote repository serves as a central hub for collaboration and backup, allowing multiple team members to access and contribute to the same codebase.

  3. Remote repositories store the authoritative copy of your project, and changes pushed to the remote repository are visible to others working on the project.

How to connect local to remote?

  1. Create a Remote Repository: First, create a new repository on the remote hosting service. This is usually done through the web interface of the service, and you can follow the steps I provided in a previous response to create a GitHub repository.

  2. Configure Git Remote: In your local repository, configure Git to recognize the remote repository by adding it as a "remote." You typically do this with the git remote add command, like this:

     csharpCopy codegit remote add origin <remote_repository_url>
    

    The "origin" is a common name for the remote repository, but you can choose a different name if you prefer. <remote_repository_url> is the URL of the remote repository you created in step 1.

  3. Push to the Remote Repository: To send your code to the remote repository, use the git push command. The most common way to do this is by pushing the code in your current branch to the remote's branch. For example, if you're working on the "main" branch locally and want to push it to the remote's "main" branch, use:

     cssCopy codegit push origin main
    

    This will upload your code to the remote repository.

Advance Commands

  • git branch: - use to see how many branches are present.

  • git checkout -b [branch-name]; - use to create a new branch. -b is used to make a new branch.

  • git checkout [branch-name]; - used to switch from one branch to another branch

  • git branch -d [branch-name]; - use to delete the local branch

  • git branch origin --delete [branch-name]; - use to delete the remote branch

  • git remote -v; - use to see how many remote GitHub users are present

  • git remote add [name] [remote-url]; - use to add new GitHub user

  • git fetch; - used to copy the remote repository with all branches on the local

  • git push origin [branch-name]; - used to push the changes from the local branch to the remote branch

  • git pull origin [branch-name]; - used to pull the changes from the remote branch to the local branch.