Day 8 Task: Basic Git & GitHub for DevOps Engineers.

Day 8 Task: Basic Git & GitHub for DevOps Engineers.

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.

Task:

I have already installed it!

  • Create a free account on GitHub (if you don't already have one). You can sign up at https://github.com/

  • Learn the basics of Git by reading through the video This will give you an understanding of what Git is, how it works, and how to use it to track changes to files.

Exercises:

  1. Create a new repository on GitHub and clone it to your local machine

Here are the steps on how to create a new repository on GitHub and clone it to your local machine:

  1. Go to the GitHub website: https://github.com/.

  2. Sign in to your GitHub account or create a new account if you don't have one.

  3. Click the "New Repository" button

  4. In the "Repository name" field, enter the name of your new repository

  5. (Optional) In the "Description" field, enter a description of your repository.

  6. (Optional) Select the visibility of your repository.

  7. Click the "Create repository" button

Once your repository is created, you can clone it to your local machine by following these steps:

  1. Open a terminal window.

  2. Navigate to the directory where you want to clone the repository.

  3. Run the following command:

git clone https://github.com/<your_username>/<repository_name>.git

Replace <your_username> with your GitHub username and <repository_name> with the name of your repository.

like this :

This will clone the repository to your local machine. You can then start working on the files in the repository.

  1. Make some changes to a file in the repository and commit them to the repository using Git

Here are the steps on how to make some changes to a file in a repository and commit them to the repository using Git:

  1. Open the file in a text editor and make the changes you want.

  2. Save the file.

  3. In the terminal, navigate to the directory where the file is located.

  4. Run the following command to add the file to the staging area:

git add <file_name>

Replace <file_name> with the name of the file you changed. 5. Run the following command to commit the changes to the repository:

git commit -m "<commit_message>"

Replace <commit_message> with a brief message describing the changes you made.

This will commit the changes to the local repository. You can then push the changes to the remote repository using the git push command.

To make things more concrete, let's say you have a file called README.md in your repository. You want to add a new paragraph to the file. You would follow these steps:

  1. Open README.md in a text editor.

  2. Add a new paragraph to the file.

  3. Save the file.

  4. In the terminal, navigate to the directory where README.md is located.

  5. Run the following command:

git add README.md
  1. Run the following command:
git commit -m "Add new paragraph to README.md"
  1. Run the following command to push the changes to the remote repository:
git push

This will push the changes to the remote repository and make them available to everyone else who has access to the repository.

  1. Push the changes back to the repository on GitHub

To push the changes back to the repository on GitHub, you can use the git push command.

The syntax for the git push command is:

git push <remote_name> <branch_name>

Replace <remote_name> with the name of the remote repository and <branch_name> with the name of the branch you want to push.

For example, if you want to push the changes to the master branch of the remote repository called origin, you would run the following command:

git push origin master

If you are not sure what the name of the remote repository or the branch is, you can run the git remote command to list all of the remote repositories and the git branch command to list all of the branches.

Let me know if you have any other queries.

Thank You

If you liked this blog then click on ❤

and do follow for more interesting and helpful blogs.

Did you find this article valuable?

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