Deep Dive in Git & GitHub for DevOps Engineers
Table of contents
- 🔹What is Git and why is it important?
- 🔹What is difference Between Main Branch and Master Branch??
- 🔹difference between Git and GitHub?
- 🔹How do you create a new repository on GitHub?
- 🔹What is difference between local & remote repository? How to connect local to remote?
- 🔹Set your user name and email address, which will be associated with your commits.
- 🔹create an SSH key and add it to your GitHub account for pulling and pushing projects.
🔹What is Git and why is it important?
Git is a distributed version control system (VCS) widely used for tracking changes in source code during software development. It allows multiple developers to collaborate on a project by managing revisions, branches, and merges effectively.
Imagine Git as a tool that keeps a detailed history of every change made to a file. It records who made the changes, what changes were made, and when they were made. This history allows you to revert back to earlier versions of a file if needed, which is extremely helpful in case of mistakes or if you want to see how your work evolved over time.
The importance of Git lies in several key aspects:
Collaboration: Git enables multiple people to work on the same project simultaneously without conflicts. It allows developers to work on different parts of the code and merge their changes together seamlessly.
Versioning: With Git, you can keep track of every version of your files. This ensures that you can always go back to a working version or compare changes made over time.
Branching: Git allows you to create branches, which are independent lines of development. This feature allows you to work on new features or bug fixes without affecting the main project until you are ready to merge your changes.
Remote Repositories: Git facilitates the use of remote repositories, such as GitHub or GitLab. These platforms provide a centralized place to store and share your code with others, making collaboration and code reviews easier.
Flexibility: Git is not limited to just code; it can manage any type of files. This makes it useful for projects beyond software development, such as writing documents or managing configuration files.
🔹What is difference Between Main Branch and Master Branch??
In the context of version control systems like Git, the terms "Main branch" and "Master branch" refer to the default branch in a repository where the main development work takes place. The difference between these terms is primarily a matter of terminology and historical context.
- Master Branch:
"Master" was traditionally used as the default branch name in Git repositories for many years.
It represents the primary branch of development, where the latest stable and production-ready code is expected to reside.
The name "Master" does not have any technical significance; it was just a commonly used convention.
However, in recent times, there has been a movement to change the default branch name to be more inclusive and less tied to historical associations.
- Main Branch:
The term "Main" is an alternative name for the default branch in a Git repository.
It has gained popularity as a more inclusive and neutral term, replacing "Master" to avoid any potential references to slavery and to promote inclusivity in software development communities.
Many organizations and open-source projects have started adopting "Main" as the default branch name in their repositories.
In summary, the main difference between "Main branch" and "Master branch" is the name itself and the connotations associated with it. Functionally, they serve the same purpose as the default branch for development in a Git repository. The shift from "Master" to "Main" is part of an ongoing effort to make the tech industry more diverse and inclusive by eliminating any potentially offensive language. Regardless of the term used, the default branch remains the central place for ongoing development and collaboration in a version control system.
🔹difference between Git and GitHub?
Git:
Git is a version control system.
Imagine it as a tool that helps you track changes to files over time.
It allows you to collaborate with others on a project efficiently.
With Git, you can keep a record of who made changes, what changes were made, and when they were made.
Git is mainly used by developers to manage and track the development of software code, but it can be used for any type of file.
GitHub:
GitHub is a web-based platform that uses Git for version control.
Think of it as a place where you can store your Git repositories online.
GitHub provides hosting for your Git repositories, making it easy for you to share your code with others.
It also offers additional features like issue tracking, pull requests, and code reviews, making collaboration on projects smoother.
GitHub is widely used by the developer community to share and collaborate on open-source projects.
🔹How do you create a new repository on GitHub?
To create a new repository on GitHub, follow these steps:
Sign in to your GitHub account: If you don't have an account, you can sign up for free at github.com/join.
Once you are signed in, click on the "+" sign icon in the top-right corner of the GitHub website.
From the drop-down menu, select "New repository."
You will be directed to the "Create a new repository" page.
On this page, you need to provide the following information for your new repository:
Repository name: Choose a unique name for your repository. This will be the name of your project.
Description: Optionally, you can provide a brief description of your project to help others understand its purpose.
Public or Private: Choose whether you want your repository to be public (visible to everyone) or private (visible only to you and collaborators).
Initialize this repository with a README: If you select this option, GitHub will create an initial README file for your repository. It's a good practice to include a README to provide some information about your project.
Once you have filled in the required information, click on the "Create repository" button at the bottom of the page.
Congratulations! You have successfully created a new repository on GitHub. Now, you can start adding files, code, and other content to your repository and collaborate with others on your project.
🔹What is difference between local & remote repository? How to connect local to remote?
A local repository and a remote repository are two types of repositories used in version control systems like Git. The main difference between them lies in their locations and accessibility:
Local Repository:
A local repository is stored on your local machine, typically on your computer's hard drive.
It contains all the version history and files associated with a particular project.
You can work with the local repository even without an internet connection since it resides on your computer.
Changes to files are tracked locally, and you can commit and make changes without affecting others until you push your changes to the remote repository.
A local repository is usually the first place where you start working on a project before sharing it with others.
Remote Repository:
A remote repository is hosted on a server or a cloud-based platform (like GitHub, GitLab, or Bitbucket) and is accessible over the internet.
It serves as a centralized or distributed location where multiple developers can collaborate and share their work.
When you push your changes from your local repository to the remote repository, others in the team can access and sync those changes.
Remote repositories are essential for team collaboration and version control management in a multi-developer environment.
To connect your local repository to a remote repository, follow these general steps:
Create a Remote Repository: First, you need to create a remote repository on a hosting service like GitHub, GitLab, or Bitbucket. This step typically involves signing up for an account and creating a new repository through their web interface.
Add Remote URL: In your local repository, you need to specify the URL of the remote repository. Use the following Git command to add the remote URL:
git remote add origin <remote_repository_url>
The
origin
here is just an alias for the remote repository; you can use any name you prefer.Push Local Changes: Once the remote is added, you can push your local changes to the remote repository using the
git push
command:git push origin <branch_name>
This will send your local commits to the remote repository and make them accessible to others.
Pull Remote Changes: If other team members have made changes in the remote repository, you can pull those changes to your local repository using the
git pull
command:git pull origin <branch_name>
This will sync your local repository with the latest changes from the remote.
With these steps, your local and remote repositories are now connected, and you can collaborate with your team members by pushing and pulling changes as needed.
🔹Set your user name and email address, which will be associated with your commits.
In the context of version control systems like Git, setting your user name and email address is important for identifying the author of the commits you make to a repository. When you commit changes to a project, Git records the author's information along with the changes made, including the user name and email address.
Here's why setting your user name and email address is beneficial:
Author Attribution: When multiple developers are collaborating on a project, it's crucial to know who made each commit. By setting a user name and email address, Git can accurately attribute each commit to the appropriate author.
Collaboration and Communication: Clear identification of authors allows for effective collaboration and communication among team members. It helps others understand who made specific changes and facilitates discussions about the code.
Contributor History: Over time, a project may receive contributions from various developers. By having consistent and accurate author information, the project maintains a complete and reliable history of contributions.
Code Review and Accountability: Code review processes benefit from knowing the author of each commit. It helps reviewers direct feedback to the appropriate person and ensures accountability for the changes made.
Open Source and Community Projects: In open-source projects or projects with a larger community, setting a user name and email address helps build trust and recognition among contributors.
To set your user name and email address in Git, use the following commands:
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
Replace "Your Name" with your actual name and "youremail@example.com" with your valid email address. The --global
flag makes these configurations apply to all repositories on your local machine.
By setting your user name and email address, you ensure that your commits are correctly attributed, making the version control history more informative and useful for you and your collaborators.
🔹create an SSH key and add it to your GitHub account for pulling and pushing projects.
Here are the steps to set up SSH authentication on GitHub:
Generate SSH Key Pair:
Open your terminal or command prompt.
Run the following command to generate an SSH key pair:
ssh-keygen -t ed25519 -C "your_email@example.com"
Add Public Key to GitHub:
Copy the content of the public key file to your clipboard. The public key file has a ".pub" extension and is located in the same directory where you generated the keys.
Go to your GitHub account settings (github.com/settings/keys).
Click on "SSH and GPG keys."
Click on "New SSH key" or "Add SSH key."
Give your key a title (e.g., "My SSH Key") and paste the public key into the "Key" field.
Click "Add SSH key" to save the key to your GitHub account.
Test SSH Connection:
To ensure your SSH key is set up correctly, run the following command:
ssh -T git@github.com
clone the project first you have to click the code icon then copy the SSH url.