Git & GitHub Mastery in DevOps: Streamlining Collaboration and Version Control (Day-7){PART-2}
Welcome back to the seventh day of our DevOps journey! Today, we dive into the heart of modern software development – Git and GitHub. As DevOps engineers, version control and collaboration are at the core of our daily workflows. Let's explore how these tools can streamline our processes and faster a collaborative and efficient development environment.
Understanding Git:
Let's connect to EC2 instances. I will create a folder there named 'gitex.com.' Let's navigate to this 'gitex.com' folder and create a file using Vim. Type 'vim calculator.sh' to begin.
And what I have done initially is I wrote a code for the addition of two numbers. This is a very simple example.
What happened here is that this is just a file that I am writing. To keep versioning or to maintain the concept of sharing the code, what I will do is, I will first create a Git repository. To create a Git repository, the command you need to use is to first install Git. Git is the command-line tool that you have to install. To install the Git command-line tool, you can simply go to your browser and visit this page: https://git-scm.com/downloads. Here, you will find the option to install Git on any operating system.
Since I am on the Linux OS, I will click on the 'Linux/Unix' button. In Linux, I will choose Ubuntu and then select the command to install Git.
Once you are done with the installation, the very basic command to create a Git repository is git init
. This command initializes a Git repository.
Now, what it said is to initialize the empty Git repository. How do you confirm that the Git repository is created? Just type 'ls -la
'; it will show you a hidden folder, and this folder is '.git.' Essentially, Git tracks everything using this '.git.
' If you delete this folder, it means your entire repository is not being tracked, and Git is not monitoring this entire system or folder.
lets see what is in this .git folder.
So, if you look at this '.git,' it contains crucial components that Git uses for tracking the entire repository. The first includes 'refs' and 'objects.' Every file you create inside Git, such as 'calculator.sh' or text files, is tracked as objects. That's where your Git objects come into play. Then, there is a folder called 'hooks.' Using hooks, you can prevent unintentional commits of sensitive information like passwords or API tokens. The 'config' folder is used to configure Git credentials, and other settings can be defined in the configuration. Regarding 'head,' I will explain it in the future as it is not required for now.
Now, I am going to explain. The first step is 'git add.' Before that, type 'git status' to see what Git is indicating about your current repository status.
So, when you use 'git status
,' you'll find that there is one untracked file, meaning you created something called 'calculator.sh.' However, Git is indicating uncertainty about whether to track this file or not. As I mentioned, Git serves two crucial purposes: versioning and determining which files to track. To manage versions, Git needs to know which files to track and which ones to exclude. Initially, Git will prompt you to decide whether to track the newly created file in this folder. Since you've initialized this repository with 'git init
,' Git seeks clarification on whether the creation of this file was intentional or unintentional.
Now, enter git add
followed by the file name, and Git will keep track of this file.
If you type git status now,
It says there are no untracked files
Now, if someone modifies the 'calculator.sh' file.
If you type git status
, it will clearly indicate that someone has made changes to a file.
Now using git diff
, git diff
is a command which will tell you what are the exact changes that are made.
To keep track of versioning, you need to specify that this is version-1, this is version-2, and so on. In the Git scenario, these are referred to as commits.
Now, I will use the command 'git commit' followed by the message that I want to convey to Git, indicating that this is version-1 of the addition.
Now, if you run git status
Git states that you are on the 'master' branch, and there is nothing to commit; the working tree is clean. This indicates that Git has comprehended all the changes you have made.
Now, what you've decided is to modify this file and introduce a new functionality for subtraction.
Let's see what happens when you enter the 'git status' command.
Yes, it suggests using the git add
command to stage the modified file.
Here, you also committed the file as version-2 for subtraction.
Now, we will explore the significant benefit of Git, which is versioning. Just type 'git log,' and you will understand the details of your commits, such as commit number one and commit number two.
Command git log
use case:git log
: Command used to display the commit history in a Git repository.
It shows a chronological list of commits, including commit messages, authors, dates, and commit hash.
Each entry in the log represents a unique commit with a corresponding commit hash.
The most recent commits appear at the top, providing a clear timeline of changes.
Useful for tracking project history, understanding changes, and collaborating with a team.
Now, if you want to revert to a previous version, you can simply copy the commit ID or commit hash. To go to the previous version, use the command 'git reset --hard <commit hash>' and navigate to that particular version.
This is how you can revert to the previous version, demonstrating the role of versioning.
To distribute this code on GitHub:
First, you need to create a repository on GitHub. Go to https://github.com and log in or sign up for an account.
Once logged in, click on the '+' sign in the top right corner and select 'New repository.'
Provide a name for your repository, add a description if you like, and choose public or private visibility. Initialize this repository with a README if you want to.
Click on the 'Create repository' button.
Now, follow the instructions under the section '…or push an existing repository from the command line.' It will look something like this:
git remote add origin https://github.com/your-username/your-repository.git
git branch -M main
git push -u origin main
Replace 'your-username' with your GitHub username and 'your-repository' with the name of the repository you created.
Once you've done this, your local Git repository is linked to your GitHub repository. You can now push changes to GitHub using 'git push origin main' or any branch you are working on.
To collaborate with others, share the repository link with them. They can clone the repository using 'git clone https://github.com/your-username/your-repository.git' and contribute by pushing their changes.
By following these steps, you've successfully distributed your code on GitHub, enabling collaboration and version control with other developers."
In Closing:
Mastering Git and GitHub as DevOps engineers empowers us with efficient version control and seamless collaboration, key elements in modern software development. We've delved into the basics of Git, understanding how to initialize repositories, track changes, and leverage versioning. Furthermore, we've explored the pivotal role of GitHub in distributing and collaborating on code.
As we continue our DevOps journey, these tools will remain integral to our workflows, enabling us to build and deploy software with agility and precision. The combination of Git and GitHub forms a powerful duo, providing the foundation for collaborative and organized development.
Keep Exploring...