Day 9:- Deep Dive in Git & GitHub for DevOps Engineers

What is Git? Why is it important?

Git is a version control system. It is used to track changes to your file. You can revert changes to its previous versions. It is important because:-

  1. Collaboration:- Many developers can work on the same project by cloning their own copy of it.

  2. Version Control:- Git contains a complete history of your project. Suppose you made some changes in a file and save it and this file contains some errors you can revert to its previous version.

  3. Branching and Merging:- Developers can create their own branch so that they can add new features to the code and can merge back to its main branch.

  4. Back and Recovery:- Git provides a reliable and secure backup of your code.

What is the difference between Master and main branch?

Both the master and main branch are the default name of your Git repository. Earlier the default name of the repository used to be master but due to its connotation to different organisations and companies, the name got changed to the main branch.

So the main difference between the main and master branch is their naming convention.

Can you explain the difference between Git and GitHub?

Git is a distributed version control system where you create different versions of your code files. Suppose you make changes to your file and in the future, it contains an error. With the help of Git, you can revert back to your previous versions and undo the changes.

GitHub, on the other hand, is a virtual workspace where you store your project files. It provides hosting for Git repositories.

What is difference between local & remote repository? How to connect local to remote?

The main differences between a local and a remote repository are as follows:

  1. Local Repository: A local repository is located on your local machine (computer). It contains the complete history and version control information of your project. You can make changes, commit them, create branches, and perform various Git operations within your local repository.

  2. Remote Repository: A remote repository is hosted on a remote server, typically on a platform like GitHub, GitLab, or Bitbucket. It serves as a central storage location for your project and allows collaboration with other developers. Remote repositories provide a way to share your code, track changes made by others, and manage multiple contributors working on the same project.

To connect your local repository to a remote repository, you can follow these general steps:

  1. Create a remote repository on a hosting platform like GitHub.

  2. Copy the remote repository's URL (HTTPS or SSH).

  3. In your local repository, use the git remote add command to add the remote repository as a remote named "origin". For example: git remote add origin <remote_repository_url>.

  4. Verify the connection by running git remote -v to see the list of remote repositories associated with your local repository.

  5. Push your local repository's commits to the remote repository using the git push command. For example: git push origin <branch_name>.

Once connected, you can push changes from your local repository to the remote repository and pull changes from the remote repository to your local repository as needed. This allows you to collaborate with others, synchronize your codebase, and maintain a backup of your project on the remote server.

Task 1:-Set your user name and email address, which will be associated with your commits.

To view the username and user email , type the command git config user.name

and git config user.email

Task 2:-

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

In the above image, I have created a repository by the name of day8_git_task on GitHub.

In the above image, I have created a repository by the name of day8_git_task on GitHub.

Click on the <>Code tab for cloning the repository

  1. Connect your local repository to the repository on GitHub.

Now by Git clone CLI command into your Git bash, it will create a copy of the repository in your system.

  1. Create a new file and add some content to it

  2. Push your local commits to the repository on GitHub

Now, I have created files and made some changes to it and after that, I did git add.

Git add will create a temporary staging area where all the changes you have made to the file get stored before saving i.e. commit. The dot. means it will take all the files present in the repository.

Now by doing git commit it will permanently save your files in git history. The command git remote -v will give the URL of the remote repository which is Github over here. Origin is the default name given to your remote repository.

To push the code into your remote repository which is Github you have to configure and authenticate yourself. For authentication, I have used a Personal Access token. It is used for authenticating yourself without using your email and password.

The command git remote set-url origin is used for the configuration of a remote repository where I have also passed generated personal access token.

git push origin main:- this command will push your local copy of your repository in your system to GitHub .main is the branch that you have to specify on which this is created.

You can see the branch displayed in your GitHub.