Deep Dive in Git & GitHub for DevOps Engineers
Day 9 : #90DaysOfDevOps Challange

What is Git and why is it important?
Git is a version control system that allows developers to track changes to their code over time. It was created by Linus Torvalds in 2005 and has since become one of the most widely used tools in the software development industry.
Git is important for several reasons:
Collaboration: Git enables multiple developers to work on the same codebase simultaneously without interfering with each other's work. This is achieved through branching and merging, which allow developers to create separate copies of the code and then integrate their changes seamlessly.
Version control: Git provides a complete history of changes made to the codebase, which helps developers keep track of what changes were made, who made them, and when. This is especially useful when debugging issues or rolling back to previous versions of the code.
Backup: Git also serves as a backup mechanism for code. Since every change is tracked and stored, developers can easily recover lost or deleted code from the Git repository.
Open source: Git is an open-source tool, which means it's free to use and anyone can contribute to its development. This has led to a large and active community of developers who contribute to the tool's development and help improve its functionality and features.
Overall, Git is an essential tool for modern software development and has revolutionized the way developers collaborate and manage code.
What is the difference Between Main Branch and Master Branch?
In Git, "main" and "master" branches are both used as the default branch name in different contexts. The difference between them is mostly historical and political.
Traditionally, the default branch in Git was named "master". This name was used in the original Git documentation and in many popular Git workflows. However, some people have argued that the term "master" has negative connotations due to its association with slavery, and have therefore advocated for the use of "main" instead.
In response to these concerns, many Git hosting platforms (such as GitHub and GitLab) have started using "main" as the default branch name. This has led to a shift in the industry towards using "main" instead of "master".
However, it's important to note that the difference between "main" and "master" is mostly cosmetic. They both serve the same purpose as the default branch in Git, which is to serve as the starting point for new development and to be the branch that contains the most up-to-date version of the code.
Ultimately, whether you use "main" or "master" as the default branch in your Git repository is a matter of personal preference and your organization's policies.
Difference between Git and GitHub?
Git is a Distributed VCS that allows developers to manage their codes and maintain the version. Whereas GitHub is a web-based service for Git repositories. There are many other platforms for hosting Git Repositories like GitLab, BitBucket, etc.
Git | GitHub |
Git is a distributed version control system (VCS) | GitHub is a web-based service for Git repositories |
Git is a command-line tool | GitHub provides a user interface for working with Git repositories |
Git allows developers to track changes to their code over time | GitHub provides a platform for storing, sharing, and collaborating on Git repositories |
Git can be used independently or with other hosting services | GitHub is just one of several hosting services for Git repositories |
Git is a free and open-source software | GitHub offers both free and enterprise plans for hosting repositories and accessing additional features |
Git does not provide collaboration features such as pull requests or issue tracking | GitHub provides a range of collaboration tools such as pull requests, issue tracking, code reviews, and more |
Git does not require an internet connection | GitHub requires an internet connection for hosting and accessing repositories |
How do you create a new repository on GitHub?
To create a new repository on GitHub, follow these steps:
Log in to your GitHub account.
Click the “+” icon in the upper right-hand corner of the page and select “New repository” from the dropdown menu.
In the “Create a new repository” page, enter a name for your repository in the “Repository name” field. Choose a descriptive name that reflects the purpose of your project.
(Optional) Add a description for your repository in the “Description” field. This can help others understand what your project is about.
Choose whether you want your repository to be public or private. Public repositories can be viewed by anyone, while private repositories are only accessible to collaborators you invite.
(Optional) Choose to initialize your repository with a README, which is a file that provides information about your project. You can also choose to add a .gitignore file, which tells Git which files to ignore when committing changes.
Choose a license for your repository. This specifies the terms under which others can use your code.
Click the “Create repository” button.
Your new repository is now created on GitHub, and you can start adding files and making changes to your project. You can also invite collaborators to work with you on your repository.
What is the difference between local & remote repositories? How to connect local to remote?
A local repository is a copy of a Git repository that is stored on a developer’s local computer, while a remote repository is a copy of the same repository that is stored on a server or a cloud-based hosting service, such as GitHub.
The primary difference between a local and remote repository is that the local repository is located on the developer’s computer and can only be accessed by that developer, while the remote repository can be accessed by multiple developers and changes made to it can be easily shared and synced across all copies of the repository.
To connect a local repository to a remote repository, you need to perform the following steps:
Create a remote repository on a hosting service such as GitHub or Bitbucket.
Copy the remote repository’s URL, which you can find on the hosting service’s website.
On your local computer, navigate to the directory containing the Git repository.
In the terminal or command prompt, enter the following command to add the remote repository URL to your local Git repository:
git remote add origin <remote-repository-URL>
5. Verify that the remote repository has been added to your local Git repository by running the following command:
git remote -v
This command will show you a list of all the remote repositories that your local repository is connected to.
6. To push your local repository changes to the remote repository, run the following command:
git push origin <branch-name>
This will download the latest changes from the remote repository to your local repository.
By connecting your local repository to a remote repository, you can collaborate with other developers and keep your codebase up-to-date with the latest changes.
Tasks:-
01. Set your user name and email address, which will be associated with your commits
To set your user name and email address in Git, you can use the following commands:
Open a terminal or command prompt.
Navigate to the directory containing the Git repository.
Run the following command, replacing “your name” with your own name:
git config --global user.name "your name"
This sets your name as the author of all commits you to make in the future.
4. Run the following command, replacing “your email” with your own email address:
git config --global user.email "your email"
This sets your email address as the contact information associated with your commits.
By setting your user name and email address in Git, you can ensure that your contributions to a project are properly attributed to you. It also helps other developers and contributors to contact you if they have questions or comments about your code.
02. Create a repository named “DevOps” on GitHub
To connect your local Git repository to the repository on GitHub, you can follow these steps:
Open a terminal or command prompt.
Navigate to the directory of your local Git repository.
Use the following command to add the remote repository’s URL to your local Git repository:
git remote add origin <remote-repository-URL>
Replace <remote-repository-URL> with the URL of your GitHub repository. For example:
git remote add origin https://github.com/yourusername/your-repository.git
4. Verify that the remote repository has been added to your local Git repository by running the following command:
git remote -v
5. This command will show you a list of all the remote repositories that your local repository is connected to. You should see the URL of your GitHub repository listed here.
6. Push your local Git repository changes to the remote repository on GitHub by running the following command:
git push -u origin master
This command will push your local master branch to the remote master branch on GitHub. If you have a different branch name, replace master with your branch name.
The -u option tells Git to remember the remote repository and branch so that you can use the shorter git push command in the future.
You may be prompted to enter your GitHub username and password to authenticate your push.
Now your local Git repository is connected to the repository on GitHub, and you can push and pull changes between the two repositories as needed.
03. Create a new file in Devops/Git/Day-02.txt & add some content to it
To create a new file named Day-02.txt in the Devops/Git directory on GitHub and add some content to it, you can follow these steps:
Open your web browser and navigate to your repository on GitHub.
Click on the “Create new file” button.
In the “Name your file…” field, type
Devops/Git/Day-02.txt.In the large text box, add the content you want to include in the file.
At the bottom of the page, you will see a section labeled “Commit new file”. Here you can add a commit message to describe the changes you’ve made. Type a message such as “Add Day-02.txt” in the “Commit new file” field.
Make sure the “Commit directly to the master branch” option is selected.
Click the “Commit new file” button to create the new file and commit your changes.
Your new file, Day-02.txt is now added to the Devops/Git directory on GitHub and contains the content you added. You can now access this file from your local Git repository by pulling the changes from the remote repository on GitHub.
04. Push your local commits to the repository on GitHub
To push your local commits to the repository on GitHub, you can follow these steps:
Open a terminal or command prompt.
Navigate to the directory of your local Git repository.
Pull any changes from the remote repository on GitHub to make sure you have the latest version of the repository:
git pull origin master
This command will fetch and merge any changes from the remote master branch on GitHub to your local master branch.
4. Add and commit any changes you’ve made to your local Git repository:
git add .
git commit -m "Your commit message"
Replace “Your commit message” with a brief description of the changes you’ve made.
05. Push your local Git repository changes to the remote repository on GitHub:
git push origin master
This command will push your local
masterbranch to the remotemasterbranch on GitHub. If you have a different branch name, replacemasterwith your branch name.You may be prompted to enter your GitHub username and password to authenticate your push.
Now your local Git repository changes are pushed to the repository on GitHub, and others who have access to the repository can see and pull your changes.
Thank you for reading!!!
~Bhaarat




