Basic Git & GitHub for DevOps Engineers

Basic Git & GitHub for DevOps Engineers

ยท

6 min read

๐Ÿ“œWhat is Git?

Git is a version control system ๐Ÿ“โœจ that allows you to track changes to files and coordinate work on those files among multiple people. It is commonly used for software development, but it can be used to track changes to any set of files.

With Git, you can keep a record of who๐Ÿ•ต๏ธโ€โ™‚๏ธ made changes ๐Ÿ“ to what part ๐Ÿ” of a file, and you can revert back to earlier versions of the file if needed. Git also makes it easy to collaborate ๐Ÿซฑ๐Ÿปโ€๐Ÿซฒ๐Ÿป with others ๐Ÿง๐Ÿป๐Ÿง๐Ÿป, as you can share changes and merge the changes made by different people into a single version of a file.

๐Ÿ”—๐ŸŒWhat is GitHub?๐Ÿง‘โ€๐Ÿ’ป๐Ÿค๐Ÿ“š

GitHub is a web-based platform ๐ŸŒ๐Ÿ‘ฉโ€๐Ÿ’ป that provides hosting for version control ๐Ÿ•ฐ๏ธ๐Ÿ”€ using Git. It is a subsidiary of Microsoft, and it offers all of the distributed version control and source code management (SCM) functionality of Git as well as adding its own features. GitHub is a very popular platform for developers to share and collaborate on projects, and it is also used for hosting ๐Ÿ”—๐ŸŒopen-source projects.

๐Ÿ•ฐ๏ธ๐Ÿ”€What is Version Control?

How many types of version controls do we have? Version control is a system that tracks changes to a file or set of files over time so that you can recall specific versions later. It allows you to revert files back to a previous state, revert the entire project back to a previous state, compare changes over time, see who last modified something that might be causing a problem, who introduced an issue and when, and more.

There are two main types of version control systems:

  1. ๐Ÿ“๐Ÿ”—๐Ÿ–ฅ๏ธCentralized Version Control Systems

  2. ๐Ÿ–ฅ๏ธ๐Ÿ”€๐Ÿ“Distributed Version Control Systems.

A centralized version control system (CVCS) ๐Ÿ“๐Ÿ”—๐Ÿ–ฅ๏ธuses a central server to store all the versions of a project's files. Developers "check out" files from the central server, make changes, and then "check in" the updated files. Examples of CVCS include Apache Subversion and Perforce.

A distributed version control system (DVCS) ๐Ÿ–ฅ๏ธ๐Ÿ”€๐Ÿ“allows developers to "clone" an entire repository, including the entire version history of the project. This means that they have a complete local copy of the repository, including all branches and past versions. Developers can work independently and then later merge their changes back into the main repository. Examples of DVCS include Git, Mercurial, and Darcs.

๐Ÿค”๐Ÿ”€Why do we use distributed version control over centralized version control?

  • ๐Ÿค๐Ÿ”€Better collaboration: In a DVCS, every developer has a full copy of the repository, including the entire history of all changes. This makes it easier for developers to work together, as they don't have to constantly communicate with a central server to commit their changes or to see the changes made by others.

  • ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿƒโ€โ™‚๏ธImproved speed: Because developers have a local copy of the repository, they can commit their changes and perform other version control actions faster, as they don't have to communicate with a central server.

  • ๐Ÿ› ๏ธ๐Ÿ”„Greater flexibility: With a DVCS, developers can work offline and commit their changes later when they do have an internet connection. They can also choose to share their changes with only a subset of the team, rather than pushing all of their changes to a central server.

  • ๐Ÿ”’๐Ÿ“‚Enhanced security: In a DVCS, the repository history is stored on multiple servers and computers, which makes it more resistant to data loss. If the central server in a CVCS goes down or the repository becomes corrupted, it can be difficult to recover the lost data.

Overall, the decentralized nature of a DVCS allows for greater collaboration, flexibility, and security, making it a popular choice for many teams.

๐Ÿš€Task:

  • Install Git on your computer (if it is not already installed). You can download it from the official website at https://git-scm.com/downloads

  • Create a free account on GitHub (if you don't already have one). You can sign up at https://github.com/

    • Just Sign up using your google sign and follow the instruction as displayed on your screen.

๐ŸŒŸExercises:

  1. Create a new repository on GitHub and clone it to your local machine

    โœจ Creating a New Repository on GitHub:

    • Open your web browser and go to GitHub (github.com).

    • Make sure you're logged in to your GitHub account. If not, log in or create an account.

    • Click on the "+" icon in the top right corner and select "New repository."

    • Fill in the repository name, description, choose visibility (public or private), and other settings.

    • Click "Create repository."

โœจ Cloning the Repository to Your Local Machine:

  • After creating the repository, you'll be redirected to the repository's main page.

  • Click the green "Code" button. You can choose to clone the repository using HTTPS, SSH, or GitHub CLI. Click on the clipboard icon to copy the repository URL.

โœจ Cloning the Repository

  • Open your terminal or command prompt on your local machine.

  • Navigate to the directory where you want to clone the repository using the cd command.

  • Use the following command to clone the repository (replace <repository-url> with the URL you copied):

      git clone <repository-url>
    
  • Press Enter to initiate the cloning process. You might be prompted to enter your GitHub credentials if you're using HTTPS.

  1. Make some changes to a file in the repository and commit them to the repository using Git

  2. Generate a token to access the Github for your local

    To generate a personal access token to access GitHub from your local machine. Follow these steps to create a personal access token:

    1. Log in to GitHub:

      Make sure you're logged in to your GitHub account in your web browser.

    2. Access Personal Access Tokens:

      • Click on your profile picture in the top right corner and select "Settings."

      • In the left sidebar, click on "Developer settings," and then click on "Personal access tokens."

    3. Generate a New Token:

      • Click the "Generate token" button.

      • Provide a token description (e.g., "Local Machine Access").

      • Select the desired scopes (permissions) for the token. For accessing repositories, you'll need at least the "repo" scope. You can choose additional scopes depending on your needs.

      • Click the "Generate token" button at the bottom of the page.

    4. Copy the Token:

      • After generating the token, you'll see it displayed on the screen. Make sure to copy it immediately because you won't be able to see it again.
    5. Use the Token:

      • In your local machine's terminal or command prompt, you can use the token to authenticate with GitHub. For example, if you're using Git on the command line, you can replace <token> with your actual token:
  3. Push the changes back to the repository on GitHub

ย