Git & GitHub

Vanshika SharmaVanshika Sharma
8 min read

Basic Git & GitHub for DevOps Engineers.

What is Git?

Git is a version control system that monitors code changes, enabling collaboration among multiple users. It allows you to save various versions of your project, work offline, and easily merge changes from different contributors.

What is GitHub?

GitHub is a web-based platform for hosting and managing Git repositories, providing a collaborative environment for developers to store, share, and work on code together. With GitHub, you can:

  1. Host Repositories: Store your Git projects online, making them accessible from anywhere.

  2. Collaborate: Work with others through features like pull requests, code reviews, and comments.

  3. Track Issues: Manage tasks and bugs with GitHub’s issue-tracking system.

  4. Use CI/CD: Automate testing and deployment of code with GitHub Actions.

What is Version Control?

Version control is a system that tracks changes to files over time, enabling multiple people to collaborate effectively. It allows users to roll back to previous versions and manage different versions of a project with ease. While it is commonly used for managing code, version control can also track changes in other file types, including documents and configuration files.

How many types of version controls do we have?

Types of Version Control Systems

  1. Local Version Control Systems:

    • These store versions on a local computer.

    • An example is RCS (Revision Control System), which keeps track of changes in a single file, but this type doesn't work well for team collaboration.

  2. Centralized Version Control Systems (CVCS):

    • In these systems, all file versions are stored on a single central server.

    • Users can check out files, make changes, and commit them back to the server. However, if the server goes down, team members can’t access or collaborate on the project.

    • Examples: CVS (Concurrent Versions System), SVN (Apache Subversion), Perforce.

  3. Distributed Version Control Systems (DVCS):

    • In a DVCS, each user has a complete copy of the repository with all history and versions on their local machine.

    • This enables offline work, and users can commit changes locally before sharing them with others, making collaboration more flexible and resilient.

    • Examples: Git, Mercurial, Bazaar.

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

Distributed Version Control Systems (DVCS), like Git, are preferred over Centralized Version Control Systems (CVCS), such as SVN, for their flexibility and efficiency. Here are the main advantages of DVCS:

  1. Offline Work: Users have a complete copy of the repository, enabling them to commit changes and create branches without an internet connection.
  1. Reduced Server Dependency: Each user’s local copy means that the system remains resilient, as the repository can be restored from any local copy if the central server fails.

  2. Efficient Collaboration: DVCS allows for faster branching and merging, enabling developers to work independently and share updates when ready, reducing conflicts.

  3. Better Performance: Local operations like commits and logs are significantly quicker since they don’t require server access.

  4. Enhanced Security: Each repository copy acts as a backup, minimizing data loss risk, while allowing for controlled access to sensitive data.

Deep Dive in Git & GitHub for DevOps Engineers

Why Git is Important?

Git is important for modern software development because it enables efficient collaboration, project organization, and version management. Here’s why Git is essential:

  1. Collaboration: Git allows multiple developers to work on the same project simultaneously without overwriting each other’s changes, thanks to features like branching and merging.

  2. Version History: Every change is saved as a "commit," creating a timeline of all changes made to a project. This makes it easy to review history, revert to previous versions, or identify when and by whom changes were made.

  3. Branching: Git’s branching feature allows developers to work on features or fixes in isolation, making it easier to experiment, develop, and test without affecting the main codebase. Branches can be merged back into the main project once tested.

  4. Backup and Recovery: Because Git is distributed, every team member has a complete copy of the project, ensuring project redundancy. This makes it easier to recover if there’s a failure on a central server.

  5. Widely Supported: Git is compatible with various tools and platforms (like GitHub, GitLab, and Bitbucket), and it's widely used in the software industry, making it a key skill for developers.

Difference Between the Main Branch and Master Branch

The terms "main" and "master" in Git refer to branch names in a repository, typically representing the primary branch where the latest stable version of the code is stored. However, there are subtle differences and reasons behind these two terms:

  1. Historical Default:

    • Originally, "master" was the default name for the primary branch in Git.

    • Recently, many Git platforms (like GitHub, GitLab, etc.) have adopted "main" as the default branch name for new repositories, partly to promote inclusive terminology and move away from language with historical connotations.

  2. Usage:

    • "Master" and "main" are functionally the same; they represent the main branch in the project that usually holds the latest, stable version of the code.

    • Teams can choose which branch name to use, or even rename "master" to "main" in existing projects if they prefer.

  3. Compatibility:

    • Newer repositories on platforms like GitHub default to "main" for consistency. However, both names are supported in Git, and you can configure either as the primary branch in your project.

Difference between Git and GitHub

FeatureGitGitHub

Purpose

A version control system to track code changes locally

A cloud-based platform for hosting Git repositories online

Functionality

Manages versions, commits, and branches

Adds collaboration tools like pull requests, issue tracking, and code reviews

Usage

Command-line tool installed locally on your computer

Web-based platform accessible from anywhere

Scope

Distributed, standalone version control

Centralized hosting and collaboration service for Git repositories

Connectivity

Works offline for commits and version control

Requires internet connection for accessing repositories

User Interface

Command-line interface (CLI), some GUIs available

Web interface with user-friendly tools

Ownership

Open-source software

Owned by Microsoft, offers both free and paid plans

Create a new repository on GitHub

  1. Sign in to GitHub:

  2. Navigate to the Repositories Page:

  3. Fill Out Repository Details:

    • Repository Name: Enter a unique name for your repository.

    • Description (optional): Provide a short description of your project.

    • Public/Private: Choose whether you want the repository to be public (visible to everyone) or private (only you and selected collaborators can see it).

  4. Initialize the Repository:

    • You can choose to initialize this repository with a README file. This is recommended as it provides a basic overview of your project.

    • Optionally, you can add a .gitignore file to specify which files or directories to ignore in the repository.

    • You can also choose a license for your project from the available options.

  5. Create the Repository:

  6. Clone the Repository (optional):

    • After the repository is created, you’ll see options to clone it to your local machine. You can copy the URL and use Git commands to clone it, or you can use GitHub Desktop or another Git client.

Example Git Command to Clone

If you choose to clone the repository, use the following command in your terminal:

git clone <repository-url>

Replace <repository-url> with the URL of your newly created repository.

What is the difference between local & remote repositories? How do you connect local to remote?

Difference Between Local and Remote Repositories

FeatureLocal RepositoryRemote Repository
DefinitionA repository stored on your local machine.A repository hosted on a remote server, typically on a platform like GitHub, GitLab, or Bitbucket.
AccessibilityAccessible only from the machine where it is stored.Accessible from anywhere with internet access.
Version ControlMaintains the entire history and versions locally.Contains a centralized version of the repository for collaboration.
BackupIt's not automatically backed up unless you manually copy it.Usually backed up on the server, providing redundancy.
UsageUsed for local development and testing.Used for collaboration and sharing code with others.

How to Connect a Local Repository to a Remote Repository

  1. Create a Remote Repository:

    • First, create a new repository on a platform like GitHub, GitLab, or Bitbucket, as described in the previous response.
  2. Open Your Local Repository:

    • Open your terminal (or command prompt) and navigate to your local repository directory using the cd command:

        cd path/to/your/local/repository
      
  3. Add Remote Repository:

    • Use the git remote add command to connect your local repository to the remote one. Replace <remote-name> (commonly origin) with the desired name for the remote, and <repository-url> with the URL of your remote repository:

        git remote add <remote-name> <repository-url>
      
    • Example:

        git remote add origin https://github.com/username/repository-name.git
      
  4. Verify Remote Connection:

    • You can check if the remote has been added correctly by running:

        git remote -v
      
  5. Push Changes:

    • After making changes in your local repository, you can push them to the remote repository using:

        git push <remote-name> <branch-name>
      
    • Example:

        git push origin main
      
0
Subscribe to my newsletter

Read articles from Vanshika Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vanshika Sharma
Vanshika Sharma

I am currently a B.Tech student pursuing Computer Science with a specialization in Data Science at I.T.S Engineering College. I am always excited to learn and explore new things to increase my knowledge. I have good knowledge of programming languages such as C, Python, Java, and web development.