Day #9 : Deep Dive in Git & GitHub for DevOps Engineers.
Table of contents
- What is Git and Why is it Important?
- What is the difference Between the Main Branch and the Master Branch??
- Can you explain the difference between Git and GitHub?
- How do you create a new repository on GitHub?
- What is the difference between local & remote repositories? How to connect local to remote?
- Task 1: Set your user name and email address, which will be associated with your commits.
- Task 2 : Repository Operations
- Conclusion
Welcome back to our DevOps journey! Today, we're going to take a deep dive into Git and GitHub, two of the most essential tools for modern software development and collaboration. We'll answer some key questions and provide step-by-step guidance on using these tools effectively.
What is Git and Why is it Important?
Git is a distributed version control system that allows developers to track changes to their codebase. It's vital for a few reasons:
Version Control: Git keeps track of every change made to a project, creating a history that enables you to revisit past states of your code.
Collaboration: Git facilitates team collaboration by allowing multiple developers to work on a project simultaneously.
Backup and Recovery: It provides a safety net. If something goes wrong or you lose your code, Git allows you to recover previous versions.
Branching and Merging: Git enables the creation of branches for developing features, and these branches can later be merged back into the main project.
What is the difference Between the Main Branch and the Master Branch??
In Git, the main branch and the master branch essentially refer to the same thing: the default branch of your repository. Historically, "master" was used, but many projects are now switching to "main" due to potential connotations of the term "master." The choice between the two largely depends on your project's naming convention, and both are widely accepted.
Can you explain the difference between Git and GitHub?
Git is the version control system itself, while GitHub is a web-based platform for hosting Git repositories. Think of Git as the engine, and GitHub as the car that houses and drives it. You use Git to manage your source code, and you can use GitHub to store, collaborate on, and share your Git repositories.
How do you create a new repository on GitHub?
Log In to Your GitHub Account: Open your web browser and go to GitHub. Log in to your GitHub account if you haven't already.
Access Your Dashboard: Once you're logged in, you'll land on your GitHub dashboard. You can access it by clicking on the GitHub logo in the top left corner.
Create a New Repository: On your dashboard, you'll find a green button labeled "New" in the top right corner. Click on it to start creating a new repository.
Set Up Your New Repository: You'll be taken to a new page where you can fill in the details for your repository:
Repository Name: Choose a unique and descriptive name for your repository.
Description (Optional): Add a brief description of your repository.
Visibility: You can choose between Public and Private. Public repositories are visible to everyone, while Private repositories are only visible to you and collaborators.
Initialize this repository with: You can choose to add a README file, a .gitignore file, or a license. These are optional but can be helpful.
What is the difference between local & remote repositories? How to connect local to remote?
The difference between a local repository and a remote repository lies in their location and purpose:
Local Repository:
Location: Your local repository is stored on your computer, specifically in a directory where you're working on a project.
Purpose: It serves as your working area, allowing you to make changes to files, track those changes, create branches, and test new features. A local repository is where you perform most of your development tasks.
Remote Repository:
Location: A remote repository is hosted on a remote server, typically on a platform like GitHub, GitLab, or Bitbucket. It's not on your computer but on a server accessible via the internet.
Purpose: Remote repositories are used for collaboration and backup. They enable multiple developers to work together on a project and provide a secure and centralized location to store your project's code. They also serve as a backup in case your local repository is lost or compromised.
Connecting Local to Remote Repository:
To connect your local repository to a remote repository, typically hosted on a platform like GitHub, follow these steps:
Create a Remote Repository:
Log in to your GitHub account (or the platform of your choice).
Create a new repository on the platform. This will be your remote repository.
Initialize Your Local Repository:
- If you haven't already, initialize a Git repository in your local project folder. You can do this using the
git init
command.
- If you haven't already, initialize a Git repository in your local project folder. You can do this using the
Link the Local Repository to the Remote Repository:
Use the
git remote add
command to link your local repository to the remote repository. Replace<repository_url>
with the URL of your remote repository on GitHub.For example:
git remote add origin <repository_url>
Set the Default Branch:
The default branch name has changed from "master" to "main" on many platforms to promote inclusive language. Use the following commands to update your local repository:
git branch -M main
Push Local Commits to Remote Repository:
After linking, you can push your local commits to the remote repository using the
git push
command:git push -u origin main
The -u
option is used to set up a tracking relationship. After this, you can simply use git push
and git pull
without specifying the remote and branch names.
Your local repository is now connected to the remote repository, and you can easily collaborate with others and keep your code safe and accessible in the remote repository.
Task 1: Set your user name and email address, which will be associated with your commits.
Open a Terminal (Linux/macOS) or Command Prompt (Windows):
- On Windows, you can use the Git Bash terminal if you've installed Git. You can also use the Windows Command Prompt if Git is included in your PATH.
Set Your Username and Email:
Use the following command to set your Git username and Git email:
ubuntu@ip-172-31-8-95:~$ git config --global user.name "Shubham Gour" ubuntu@ip-172-31-8-95:~$ git config --global user.email "iamtheshubhamgour@gmail.com"
Verify Your Settings:
To verify that your settings have been applied, you can use the following command to display your Git configuration:
ubuntu@ip-172-31-8-95:~$ git config --list
Task 2 : Repository Operations
Create a repository named "Devops" on GitHub
Connect your local repository to the repository on GitHub.
Create a new file in Devops/Git/Day-02.txt & add some content to it
Push your local commits to the repository on GitHub
Write the Repository name as "DevOps" and Click on Create Repository
Now go to your local terminal create a Directory named "DevOps" which we will connect with Github later.
In our local system we have created a Directory named Devops within it we have Git and a file named Day-02.txt as per the task.
Now we initialised the local repository and marked the changes to the staging area.
It time to connect the git repo with the local for that we need to fire the below command
shubhamgour@Shubhams-MacBook-Air DevOps % git remote add DevOps https://github.com/theshubhamgour/DevOps.git
Now let's push the changes to the Central repo, we will execute the below command
shubhamgour@Shubhams-MacBook-Air DevOps % git push --all
Now when you check your Github account you will get the files present, live on the central
Conclusion
In summary, Task-1 ensured your identity is associated with your Git commits, a fundamental step in version control. Task-2, creating and connecting a "Devops" repository on GitHub, adding a new file, and pushing local commits, demonstrates the practical application of Git and GitHub for efficient code management and collaboration in your DevOps journey. Keep up the great work, and you're well on your way to becoming a proficient DevOps engineer! ๐๐จโ๐ป #DevOps #Git #GitHub #EfficientCoding