Welcome back to the #90DaysOfDevOps Challenge! On Day 8, we're diving into the essentials of Git and GitHub. Let's break down these fundamental tools for DevOps Engineers.
Understanding Git:
What is Git?
Git is a powerful version control system that tracks changes to files, allowing seamless collaboration among multiple users. Whether you're a developer or a DevOps enthusiast, Git helps keep track of changes, revert to earlier versions, and merge contributions effortlessly.
Why Use Git?
Git simplifies collaboration and coordination on files, making it a crucial tool for software development and beyond. It enables you to share changes, manage different versions of files, and merge contributions into a cohesive project.
Introduction to GitHub:
What is GitHub?
GitHub is a web-based platform built on Git, providing hosting for version control. It's a hub for collaboration, widely used for sharing, collaborating, and hosting open-source projects. GitHub extends Git's functionality, adding features for an enhanced development experience.
Basics of Version Control:
What is Version Control?
Version control is a system that tracks changes to files over time, allowing you to recall specific versions. It facilitates reverting files or the entire project, comparing changes, identifying contributors, and more. There are two main types: Centralized Version Control Systems (CVCS) and Distributed Version Control Systems (DVCS).
Let's delve into the comparison between Distributed Version Control Systems (DVCS) and Centralized Version Control Systems (CVCS) in simpler terms:
DVCS vs. CVCS: Simplified
Version Control Systems (VCS): Version control is like a superpower for developers, helping them manage changes in their projects over time. There are two main types: Centralized (CVCS) and Distributed (DVCS).
Centralized Version Control Systems (CVCS):
How it Works:
All project versions are stored on a central server.
Developers 'check out' files to work on them and 'check in' changes to the central server.
Examples: Subversion, Perforce.
Advantages:
Simplifies collaboration, as everyone works on the same server.
Centralized control over project versions.
Drawbacks:
Dependency on the central server.
Slower if the server is far or experiences issues.
Distributed Version Control Systems (DVCS):
How it Works:
Every developer has a full copy (clone) of the entire project, including its history.
Changes are made locally and then shared with others.
Examples: Git, Mercurial, Darcs.
Advantages:
Better collaboration - each developer has a full project copy.
Faster - no constant communication with a central server.
Greater flexibility - work offline, share changes selectively.
Drawbacks:
- Slightly steeper learning curve.
Why DVCS over CVCS?
Collaboration: In DVCS, everyone has a complete project copy, making collaboration smoother without constant server communication.
Speed: With DVCS, developers can commit changes faster since they don't have to communicate with a central server.
Flexibility: DVCS allows developers to work offline and choose when to share changes, providing more flexibility.
Security: Data is stored on multiple servers in DVCS, making it more resistant to data loss compared to a single central server.
Conclusion: Choosing between DVCS and CVCS depends on the team's preferences and project requirements. Many opt for DVCS like Git due to its flexibility and improved collaboration, especially in today's fast-paced development environments.
Now, let's proceed to the task and hands-on activities.
Tasks:
Checking version and Installing git:
Here are the steps on how to check if Git is installed on Linux:
Open a terminal window.
Type the following command:
git --version
Press Enter.
If Git is installed, you will see a message that says something like "git version 2.35.1". If Git is not installed, you will see a message that says something like "command not found".
If Git is not installed, you can install it using the following command:
sudo apt install git
This will install the latest version of Git on your system.
Create a GitHub Account:
- Sign up here.
Hands-on Exercises:
- Create a new repository on GitHub and clone it to your local machine:
Creating a new repository on GitHub and cloning it locally is a fundamental task. Here's a simple set of instructions:
Create a New Repository:
On GitHub:
Log in to your GitHub account.
Click on the "+" sign in the top right corner.
Choose "New repository."
Name your repository.
Add a brief description.
Choose public or private (based on your preference).
Initialize with a README if needed.
Click "Create repository."
On Your Local Machine:
Open your terminal or command prompt.
Navigate to the directory where you want to clone the repository.
Use the following command to clone the repository:
git clone https://github.com/your-username/your-repository.git
(Replace
your-username
andyour-repository
with your GitHub username and the repository name)
Now, you have successfully created a new repository on GitHub and cloned it to your local machine. You can start making changes, committing them, and pushing them back to GitHub.
- Make some changes to a file in the repository and commit them to the repository using Git:
Edit a File:
Navigate to the cloned repository on your local machine using the terminal or file explorer.
Open the file you want to modify using your preferred code editor.
Make Your Changes:
- Add or modify content in the file according to your requirements.
Save the Changes:
- Save the changes in your code editor.
Stage the Changes:
In the terminal, use the following command to stage the changes:
git add filename
(Replace
filename
with the actual name of the file you modified)
Commit the Changes:
Now, commit your changes with a descriptive message:
git commit -m "Brief description of the changes"
Example:
git commit -m "Update README with new information"
This creates a snapshot of your changes.
Remember, committing changes is like saving a version of your project at a specific point in time. You can continue making changes, committing, and building a version history.
- Push the changes back to the repository on GitHub:
Navigate to Your Local Repository:
Open the terminal or command prompt.
Change your working directory to the local repository:
cd path/to/your/local/repository
Push Changes to GitHub:
Use the following command to push your committed changes to GitHub:
git push origin main
(Replace
main
with the branch name if you're working on a branch other than the main branch)
Enter GitHub Credentials:
- If prompted, enter your GitHub username and password or personal access token.
Check GitHub Repository:
Visit your GitHub repository in a web browser.
You should see the changes you made reflected in the repository.
By pushing your changes to GitHub, you're making your modifications accessible to collaborators and ensuring your work is safely stored in the remote repository.
As you navigate the vast terrain of DevOps, remember: every line of code is a step forward. Keep coding, stay curious, and embrace the journey.