Basic Git & GitHub for DevOps Engineers.
#90 Days of DevOps Challenge - Day 8
Imagine a scenario where you're diligently working on a document, making various edits and improvements. To ensure a safety net, you save the document with a modified name, such as adding "_v2" to indicate a new version. This practice allows you to revert to the original document if necessary. However, what if you later realize that the previous version was actually better, after making further modifications? Manually comparing the two versions and transferring the changes would be a time-consuming task. This is precisely where a Version Control System (VCS) proves invaluable.
What is Git?
Git is a distributed version control system (VCS) widely used for tracking changes in files and coordinating collaborative work on software development projects. It was created by Linus Torvalds in 2005 with a focus on speed, data integrity, and distributed development.
Git is known for its flexibility, efficiency, and robustness, making it one of the most popular VCS choices among developers. It offers a wide range of features and capabilities that enable efficient version control and collaboration.
Uses of git.
Version control for tracking changes in files over time.
Collaboration among developers, enabling efficient teamwork and code sharing.
Branching and parallel development, allow developers to work on different features simultaneously.
Integration with CI/CD pipelines for automated builds, tests, and deployments.
Configuration management for maintaining different versions of configuration files.
Documentation management, enabling versioning and collaboration on documentation.
Personal project management, tracking changes in various file types beyond code.
Git's versatility and flexibility make it a widely adopted tool in software development and other domains. It provides a reliable and efficient way to manage code, collaborate with others, and streamline development processes.
What is GitHub?
GitHub is a web-based platform built on top of Git that serves as a hosting service for Git repositories. It provides a centralized location for developers to store, collaborate on, and share their Git repositories with others. GitHub offers a wide range of features and tools that enhance collaboration, code review, project management, and software development workflows.
Uses of Github.
GitHub, as a popular code hosting platform built on top of Git, offers a wide range of uses and benefits for developers and teams. Here are some key uses of GitHub:
Code Collaboration:
GitHub enables seamless collaboration among developers. Multiple individuals can work on the same project, contribute changes, and review each other's code through features like pull requests and code reviews.Version Control:
GitHub serves as a reliable version control system. It allows developers to track changes made to their codebase, maintain a complete history of revisions, and easily revert to previous versions if needed.Code Hosting and Sharing:
GitHub provides a centralized location to host and share Git repositories. Developers can publish their projects on GitHub, making them accessible to others for exploration, contributions, and reuse.Open Source Contribution:
GitHub is widely used for open-source development. It offers a platform for developers to contribute to existing projects, submit bug fixes, suggest improvements, and collaborate with the project maintainers and the broader community.Project Management:
GitHub includes features to aid project management. Developers can create and track issues, assign tasks, set milestones, and visualize project progress using built-in tools like project boards and issue trackers.Continuous Integration/Continuous Deployment (CI/CD):
GitHub integrates with various CI/CD tools and services. Developers can set up automated workflows triggered by code changes, enabling automated builds, tests, and deployments to streamline the development process.
What is Version Control? How many types of version controls do we have?
Version control is a system that records changes to a file or set of files over time so that you can revert back to a previous version if necessary.
There are two main types of version control: centralized and distributed.
Centralized version control systems(CVCS) store all of the versions of a project's files on a central server. This makes it easy to collaborate with others, but it also means that you're reliant on that server being available.
Distributed version control systems(DVCS), on the other hand, store all of the versions of a project's files on each developer's computer. This gives you more control over your data, but it can also make it more difficult to collaborate.
Distributed Version Control System (DVCS)
A Distributed Version Control System (DVCS) is a type of version control system that allows each user to have a complete copy of the repository, including the entire history and files. DVCS enables users to work independently, offline, and without relying on a central server. It offers benefits such as decentralized collaboration, fast operations, efficient branching and merging, repository mirroring, scalability, and flexibility. Git, Mercurial, and Bazaar are examples of popular DVCS tools. DVCS is widely used in software development for its ability to support distributed teams, handle large codebases, and provide flexibility in workflows.
Centralized Version Control System (CVCS)
A Centralized Version Control System (CVCS) is a type of version control system where a central server stores the entire version history and files of a project. Users interact with the server to check out, modify, and commit changes to the repository. CVCS allows for collaboration but requires constant network access to the central server. It provides file-locking mechanisms and maintains a linear version history. Examples of CVCS include Apache Subversion (SVN) and Concurrent Versions System (CVS). CVCS is suitable for centralized workflows but may have limitations in offline work and distributed collaboration compared to Distributed Version Control Systems (DVCS) like Git.
Why do we use distributed version control over centralized version control?
There are several reasons why distributed version control systems (DVCS) are preferred over centralized version control systems (CVCS) in many software development workflows. Here are some key advantages of DVCS:
Offline Work: DVCS allows users to work offline since each user has a complete copy of the repository. This is particularly useful in situations where network connectivity is limited or unreliable. Users can commit changes, create branches, and perform various version control operations without requiring constant access to a central server.
Concurrent Development: DVCS facilitates concurrent development by enabling developers to work independently on different branches or features. Each developer has their own local copy of the repository and can commit changes without affecting others. This promotes parallel development and reduces conflicts that arise from simultaneous modifications.
Faster Operations: With DVCS, most operations are performed locally without the need for network communication with a central server. This results in faster operations, such as committing changes, switching between branches, and viewing history. Developers can iterate more quickly and maintain a smoother workflow.
Branching and Merging: DVCS systems excel in branching and merging capabilities. Creating branches for experimentation, feature development, or bug fixing is easy and efficient. Merging changes from different branches is also typically straightforward, allowing for smoother collaboration and easier code integration.
Flexibility and Freedom: DVCS offers more flexibility and freedom in how developers work. They can commit changes locally, experiment with new ideas, and switch between branches or versions easily. Developers have more control over their workflow and can make changes without immediately impacting others.
Scalability: DVCS systems are designed to handle large codebases and repositories with efficiency. They distribute the repository's history across multiple copies, enabling optimal performance even with complex projects and large teams. DVCS scales well as the codebase and the number of contributors grow.
Redundancy and Backup: DVCS inherently provides redundancy as each user has a complete copy of the repository. This redundancy acts as a backup mechanism, reducing the risk of data loss. If one user's copy is lost or corrupted, the repository can be restored from other copies.
Git, the most popular DVCS, is widely adopted in the software development community due to its powerful features, distributed nature, and extensive tooling support.
While CVCS can still be suitable for specific use cases and workflows, the advantages of DVCS, such as offline work, concurrent development, faster operations, branching and merging capabilities, flexibility, scalability, and redundancy, make it a preferred choice for many development teams and projects.
Tasks
Task 1: Install Git on your computer
To install Git on your computer, please follow this manual by Atlassian Install Git.
Task 2: Create a free account on GitHub
To create your account on GitHub, you can sign up on GitHub SignUp. To know more about the creation of an account on GitHub, you can follow this guide: Signing up for a new GitHub account.
Exercises
Exercise 1: Create a new repository on GitHub and clone it to your local machine
so first signup into your Github account then go to the main page after that you will see a `+` icon on the Right side of the main page near profile picture. then click on that plus icon where u will see the " New repository " option click on that and u will be redirected to create repository page.
After clicking :
Enter the repo name whatever you want, then select public or private as per your preference and create a repo.
after creating the repo this will look like this,
Hurray! Our first repository is created ! ...
Let's clone this to local,
To clone this repository to my local machine, Select the "Code" option, which is in the green colour dialogue box and copy the HTTPS link from there.
Then, open Git in your system and go to your preferred directory. The command used for cloning a git repository is
git clone https://github.com/D-singh121/myNewRepo.git
Oh yes !!!! we did it, our repository is cloned in our local system.
Exercise 2: Make some changes to a file in the repository and commit them to the repository using Git.
Let's make a file name testfile.txt
Now before the commiting into GitHub first bring it to the staging.
These are the command to bring the unstaged file into the staging area
git add 'Filename'
git commit -m 'Commit_Message' # -m for message
git status # for checking current git status
The file has been committed successfully ......! Done
Exercise 3: Push the changes back to the repository on GitHub.
Before pushing the changes to the repo we need to signup on to GitHub with a username and password
This is the command for pushing the changes
git push -u -f origin
"push": This is the command to push your local commits to a remote repository.
"-u": This option sets the upstream branch for the current branch. It links your local branch to the corresponding branch on the remote repository, so that you can use "git pull" and "git push" without specifying the remote and branch names each time.
"-f": This option stands for "force" and allows you to overwrite the remote branch with your local branch, even if the remote branch has new commits that you don't have locally. Be cautious when using this option, as it can potentially overwrite or discard other people's work on the remote branch.
"origin": This specifies the remote repository where you want to push your changes. "origin" is a common name used for the default remote repository in Git, but it can be different depending on your specific repository setup.
we can see our commit here on GitHub and pushed file.
In the upcoming blog, I will take you on a deep dive into the fascinating world of Git and GitHub. We'll explore the ins and outs of version control, branching strategies, collaboration workflows, and the powerful features that Git and GitHub offers.
Your feedback and suggestions are crucial in making this blog better and ensuring its accuracy. Feel free to connect with me on LinkedIn as Devesh Choudhary welcome any suggestions, corrections, or additional insights you may have.
Stay tuned for an exciting journey through Git and GitHub as we continue our #90DaysofDevops challenge on Day 8!