Day 8 - Basic Git & GitHub for DevOps Engineers
Introduction
Are you new to the world of version control and collaboration in software development? Are you looking to streamline your workflow and enhance your team's productivity? If so, then you've come to the right place! In this blog post, we'll delve into the basics of Git and GitHub, two essential tools for modern software development and DevOps practices.
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 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.
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.
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 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 1: Install Git
To kickstart our journey with Git, let's install it on your computer. Git is compatible with Windows, macOS, and Linux. Follow these straightforward steps:
Navigate to git-scm.com/downloads, the official Git website.
Download the installer tailored for your operating system.
Run the downloaded installer and adhere to the prompts displayed on-screen to complete the installation process.
After installation, launch a terminal (on Linux or macOS) or command prompt (on Windows).
Type the command
git --version
and hit Enter to ensure Git is installed correctly. You should witness the version number of Git displayed on your screen.
To install Git on a Debian-based system such as Ubuntu, you can run the following commands:
sudo apt-get update
sudo apt-get install git
After running these commands, Git should be successfully installed on your system. To verify the installation and check the version of Git installed, you can run:
git --version
This command will display the installed version of Git on your machine. If Git was installed correctly, you'll see the version number printed in the terminal.
Task 2: Create a GitHub Account
GitHub serves as a vibrant platform for hosting Git repositories and fostering collaboration among developers. If you're yet to join the GitHub community, here's a simple guide to create your account:
Open your preferred web browser and navigate to github.com.
On the GitHub homepage, locate and click on the prominent "Sign up" button.
Fill in the requisite details, including your desired username, email address, and password.
Choose a plan that aligns with your requirements, whether it's the Free plan or one of the paid options, based on your needs and preferences.
Proceed with the verification process, which may involve completing a CAPTCHA challenge or confirming your email address.
Upon successfully completing these steps, congratulations are in order! You've officially created your GitHub account, unlocking a plethora of possibilities for sharing and collaborating on your projects.
With your GitHub account ready, you're primed to dive into the world of collaborative coding, sharing your creations, and contributing to open-source initiatives. ๐๐ค
Exercise 1: Create a New Repository on GitHub
Let's kickstart our coding journey by setting up a new repository on GitHub to store and organize our code. Follow these straightforward steps to get started:
Open your preferred web browser and navigate to github.com.
Log in to your GitHub account using your credentials.
Once logged in, you'll land on the GitHub homepage. Look for the "+" button located at the top-right corner of the page and click on it. From the dropdown menu that appears, select "New repository."
Provide a meaningful name for your repository, reflecting the project or codebase it will contain.
Optionally, you can add a description to provide additional context and details about your repository.
Choose the visibility of your repository by selecting either "Public" or "Private," depending on your requirements and preferences.
Finally, click on the "Create repository" button to instantiate your new repository. Congratulations! Your central hub for code storage and collaboration is now ready for action. ๐
With your repository created, you're all set to start populating it with code, collaborating with others, and taking your projects to new heights.
Exercise 2: Clone the Repository to Your Local Machine
Now that our repository is up and running on GitHub, it's time to bring it down to your local machine so you can start working on it. Follow these simple steps to clone the repository:
Access Your Repository on GitHub:
- Open your web browser and navigate to the repository page on GitHub.
Clone the Repository:
On the repository page, locate the green "Code" button and click on it.
From the dropdown menu that appears, copy the URL provided. This is the address of your repository.
Open Terminal or Command Prompt:
- Open a terminal window or command prompt on your computer.
Navigate to Desired Directory:
- Use the
cd
command to navigate to the directory where you want to save the repository on your local machine.
- Use the
Clone the Repository:
Once you're in the desired directory, 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
Press Enter to execute the command.
Confirmation:
After executing the command, Git will clone the repository onto your local machine.
You'll see a progress indicator as Git fetches all the files and commits from the remote repository.
Congratulations! ๐ You have successfully cloned the repository from GitHub to your local machine. You're now ready to make changes, add new features, and collaborate with others on your project. Let's move on to the next exercise and continue our DevOps journey. ๐
Exercise 3: Make Changes, Commit, and Push
Now that you've cloned the repository to your local machine, let's dive into making changes, committing them, and pushing them back to GitHub. Follow these steps to manage your changes effectively:
Navigate to Repository Directory:
- Open your terminal or command prompt and move to the directory where you cloned the repository using the
cd
command.
- Open your terminal or command prompt and move to the directory where you cloned the repository using the
Create Files:
Use the
touch
command to create two new files within the repository directory. For example:touch file1.txt touch file2.txt
Check Status:
- Use the
git status
command to view the status of your changes. Git will display the modified files.
- Use the
Stage Changes:
Use the
git add
command to stage the changes for commit. You can add individual files or all changes using the.
notation. For example:git add file1.txt file2.txt
or
git add .
Commit Changes:
Commit your changes with a descriptive message using the
git commit
command. For example:git commit -m "Add two new files"
Push Changes:
Finally, push your committed changes back to the remote repository on GitHub using the
git push
command. Specify the remote repository name (usually 'origin') and the branch name (e.g., 'main' or 'master'). For example:git push origin main
or
git push origin master
By following these steps, you'll effectively manage changes to your GitHub repository, ensuring that your code is version-controlled and accessible to collaborators. This streamlined process enhances collaboration and ensures the integrity of your project on GitHub. ๐
Conclusion:
In summary, Git and GitHub revolutionize software development by providing powerful tools for version control and collaboration. Git's decentralized system enables efficient tracking of changes, while GitHub's platform enhances collaboration with features like pull requests and issue tracking. Together, they streamline workflows, empower teams, and drive innovation in the development community. Embrace Git and GitHub for enhanced productivity and seamless collaboration in your projects.
Thank you for reading our DevOps blog post. We hope you found it informative and helpful. If you have any questions or feedback, please don't hesitate to contact us.
I hope this helps!
Happy Learningโจ
Subscribe to my newsletter
Read articles from Rahul Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Rahul Gupta
Rahul Gupta
Hey there! ๐ I'm Rahul Gupta, a DevOps Engineer passionate about all things AWS DevOps Technology. Currently, on a learning adventure, I'm here to share my journey and Blogs in the world of cloud and DevOps. ๐ ๏ธ My focus? Making sense of AWS services, improving CI/CD, and diving into infrastructure as code. Whether you're fellow interns or curious enthusiasts, let's grow together in the vibrant DevOps space. ๐ Connect with me for friendly chats, shared experiences, and learning moments. Here's to embracing the learning curve and thriving in the exciting world of AWS DevOps Technology!