Basic Git & GitHub for DevOps Engineers.

Basic Git & GitHub for DevOps Engineers.

ยท

7 min read

Managing files and documents ๐Ÿ“‚๐Ÿ“ can be a challenging task, especially in the software industry. Keeping track of where everything is stored can be difficult without proper record-keeping ๐Ÿค”. Sharing files with others can also be cumbersome with limited options like email ๐Ÿ“ง and drives ๐Ÿš— available. Sharing new versions of projects every time a file is changed can hamper scalability and reduce productivity ๐Ÿ“‰.

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: centralized version control systems and distributed version control systems.

  1. 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 Subversion and Perforce.

  2. 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?

  1. 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.

  2. 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.

  3. 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.

  4. 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.

Some Basic Commands of Git.

Command          Description

git init         Initializes a new Git repository in the current directory.

git clone        Creates a local copy of a remote repository.

git add          Adds changes or new files to the staging area for the next commit.

git commit       Records changes to the repository with a commit message.

git status       Shows the status of the working directory and staging area.

git push         Uploads local commits to a remote repository.

git pull         Fetches and merges changes from a remote repository.

git branch       Lists, creates, or deletes branches in the repository.

git checkout     Switches to a different branch or restores files from a commit.

git merge        Combines changes from one branch into another branch.

git log          Displays a history of commits in the repository.

git remote       Manages remote repositories linked to the local repository.

Install Git on your computer:

Create a New Repo on GitHub & Clone it to a Local Machine

Download the Git installer for Windows from the official Git website: gitscm.com/download/win

Run the installer and follow the on-screen instructions. You can generally use the default settings unless you have specific preferences.

After installation, you can verify that Git is properly installed by opening a terminal or command prompt and running:

git --version

Task 2: Create a free account on GitHub

Visit github.com and click "Sign Up."

Choose the "Free" plan and create an account with email or Google.

Verify your email to activate your account.

Complete your profile settings and personalize your GitHub experience.

Exercise 1: Create a new repository on GitHub.

Once successfully logged into the GitHub account. Click on the โ€œnewโ€ button to create your repo. Check the โ€œAdd a README fileโ€ to create a readme file in your remote repo and click on the โ€œCreate Repositoryโ€ button. See below:

Once created, copy the repo URL to be used on your local machine. Once completed, move over to your Terminal in the directory where you want to clone your remote repo. Use the clone command

Excercise 2: Clone the Repository to Your Local Machine

  • In the terminal, use the git clone command followed by the repository URL you copied earlier. For example:

        git clone https://github.com/your-username/your-repository.git
    
    • Excercise 3: Make some changes to a file in the repository and commit them to the repository using Git

  1. Open a File:

    • Use a text editor or code editor to open a file from your cloned repository that you want to modify. You can use any editor you're comfortable with, such as Notepad (Windows), Visual Studio Code, Sublime Text, etc.
  2. Make Changes:

    • Edit the content of the file to make the desired changes. For example, add some text or modify existing lines.
  3. Save the File:

    • Save the changes you've made to the file.
  4. Check Status:

    • Open your terminal or command prompt.

    • Navigate to the repository's directory using the cd command.

    • Run the following command to see the status of your repository:

          git status
      
    • This will show you which files have been modified.

  5. Stage Changes:

    • Use the git add command to stage the changes you made. Replace filename with the actual name of the file you edited.

          git add filename
      
    • If you want to stage all changes in the repository, you can use:

          git add .
      
  6. Commit Changes:

    • Now, commit the staged changes using the git commit command. This command also opens a text editor for you to enter a commit message.

          git commit
      
    • Alternatively, you can use the -m flag to provide a commit message directly in the command:

          git commit -m "Add a description of your changes"
      
  7. Push Changes:

    • If you cloned a remote repository, you can use the git push command to push your committed changes back to the remote repository.

          git push origin master
      

      ๐Ÿ“Conclusion:

      ๐Ÿ” The information above is crucial for a DevOps engineer ๐Ÿ› ๏ธ, as mastering Git and GitHub ๐Ÿ™ puts you in the league of legendary DevOps engineers! ๐ŸŒŸ Embrace version control and collaboration ๐Ÿ‘ฅ๐Ÿ‘ฅ, and your coding adventures will be limitless! ๐Ÿš€

      So go ahead and give it a shot on your own, and share your learning experience in the comments section below. ๐Ÿ’ฌ

      Thanks for reading! ๐Ÿ™Œ

Did you find this article valuable?

Support DevOps_journey by becoming a sponsor. Any amount is appreciated!

ย