Deep Dive in Git & GitHub for DevOps Engineers 🚀 | Day 9 of 90 Days of DevOps
Hello everyone, welcome to Day 9 of #90DaysOfDevOps. In this blog post, we will explore some of the most important concepts and features of Git and GitHub, the tools that every DevOps engineer should master. We will also do some hands-on exercises to practice what we learn. Let’s get started! 🙌
Git is a distributed version control system that allows you to track changes in your code and collaborate with other developers.
GitHub is a cloud-based platform that hosts Git repositories and provides various features such as issue tracking, code review, project management, and more.
Git and GitHub are essential for DevOps because they enable you to:
- Manage your codebase efficiently and securely
- Implement continuous integration and continuous delivery (CI/CD) pipelines
- Automate testing and deployment processes
- Collaborate with other developers and stakeholders
- Contribute to open source projects and communities
In this blog post, we will cover the following topics:
- Difference between main branch and master branch?
- How to create a new repository on GitHub?
- What is the difference between local and remote repository? How to connect local repository to remote repository?
Difference between main branch and master branch
In Git, a branch is a pointer to a specific commit in the history of your project. You can create multiple branches to work on different features or bug fixes without affecting the main codebase.
The default branch of a Git repository is the one that is checked out when you clone the repository. It is also the one that receives new commits from other branches when you merge or rebase them.
Traditionally, the default branch of a Git repository was called `master`. However, in October 2020, GitHub announced that it would change the name of the default branch from `master` to `main` for all new repositories created on its platform. This was done to avoid any negative associations with the term `master` and to promote more inclusive language.
If you have an existing repository on GitHub that uses `master` as the default branch, you can rename it to `main` by following these steps:
- Go to your repository on GitHub and click on the Settings tab.
- Under Repository name, click on Rename default branch.
- Enter `main` as the new branch name and click on Rename branch.
- Update your local clone of the repository by running these commands in your terminal:
git fetch origin
git branch -u origin/main main
git remote set-head origin -a
You can also use the following command to rename a branch locally.
git branch -m
How to create a new repository on GitHub
A repository is a collection of files and folders that are tracked by Git. You can create a new repository on GitHub by following these steps:
- Go to https://github.com/ and sign in with your account.
- Click on the New button in the upper-right corner.
- Enter a name for your repository, such as `DevOps`.
- Optionally, you can add a description, choose a license, initialize the repository with a README file, or add a .gitignore file.
- Click on Create repository.
You will see a page with some instructions on how to clone your repository or push an existing one.
What is the difference between local and remote repositories? How to connect the local repository to the remote repository?
A local repository is a copy of your project files and history that resides on your computer. A remote repository is a copy of your project files and history that resides on another server, such as GitHub.
You can connect your local repository to a remote repository by adding it as a remote. A remote is a reference to another repository that you can fetch from or push to.
To add a remote, you need to know its URL, which you can find on GitHub by clicking on the Code button in your repository page. For example, the URL of this repository is https://github.com/ajitfawade/DevOps.git.
To add this remote, run this command in your terminal:
git remote add origin git@github.com:ajitfawade/DevOps.git
This will create a remote named `origin` that points to the URL you specified. You can use any name you want for the remote, but `origin` is a common convention.
To verify that you have added the remote correctly, run this command:
git remote -v
This will show you the list of remotes and their URLs.
To fetch data from the remote, run this command:
git fetch origin
This will download any new commits or branches from the remote to your local repository.
To push data to the remote, run this command:
git push origin main
This will upload any new commits or branches from your local repository to the remote. You need to specify the name of the branch you want to push, such as `main`.
Tasks for Day 9
Now that we have learned some of the basics of Git and GitHub, let’s try to apply them in some tasks. Here are the tasks for Day 9:
- Set your user name and user email which will be associated with the commits
- a. Create a repository named “DevOps” on GitHub
b. Connect your local repository to the repository on GitHub.
c. Create a new file in Devops/Git/Day-02.txt & add some content to it
d. Push your local commits to the repository on GitHub
Task 1: Set your user name and user email which will be associated with the commits
Before you start making any commits, you need to configure your user name and user email in Git. These are the information that will be attached to your commits and will identify you as the author.
To set your user name and user email, run these commands in your terminal:
git config - global user.name "Your Name"
git config - global user.email "your.email@example.com"
Replace `Your Name` and `your.email@example.com` with your own name and email.
You can check your settings by running these commands:
git config --global user.name
git config --global user.email
These will show you the current values of your user name and user email.
Task 2a: Create a repository named “DevOps” on GitHub
We have already seen how to create a new repository on GitHub in the previous section, so we will skip this step here. Just make sure you name your repository `DevOps` and copy its URL.
Task 2b: Connect your local repository to the repository on GitHub
We have also seen how to connect your local repository to a remote repository on GitHub in the previous section, so we will skip this step here. Just make sure you add the remote named `origin` with the URL of your `DevOps` repository.
Task 2c: Create a new file in Devops/Git/Day-02.txt & add some content to it
To create a new file in your local repository, you can use any text editor or IDE of your choice, or you can use the `touch` command in your terminal.
For example, to create a file named `Day-02.txt` in the folder `Devops/Git`, run this command:
touch Devops/Git/Day-02.txt
This will create an empty file in the specified location.
To add some content to the file, you can use any text editor or IDE of your choice, or you can use the `echo` command in your terminal.
For example, to add some text to the file, run this command:
echo "This is Day 2 of #90DaysOfDevOps" > Devops/Git/Day-02.txt
This will overwrite the file with the text you specified.
You can also append text to the file by using `>>` instead of `>`.
For example, to append some text to the file, run this command:
echo "We learned about Git and GitHub today" >> Devops/Git/Day-02.txt
This will add the text to the end of the file.
Task 2d: Push your local commits to the repository on GitHub
To push your local changes to the remote repository on GitHub, you need to first stage and commit them.
Staging is the process of selecting which files or changes you want to include in a commit. A commit is a snapshot of your project at a certain point in time.
To stage all the files or changes in your local repository, run this command:
git add .
This will add all the files or changes to the staging area.
To commit them with a message describing what you did, run this command:
git commit -m "Added Day-02.txt file"
This will create a new commit with the message you specified.
To push them to the remote repository on GitHub, run this command:
git push origin master
This will upload your new commit and branch to the remote repository on GitHub.
You can verify that your changes are reflected on GitHub by visiting your repository page and clicking on Commits or Code.
In this blog post, we have learned some of the basics of Git and GitHub, such as branches, repositories, remotes, fetch, push, and more. We have also done some tasks to practice what we learned.
Git and GitHub are powerful tools that can help you manage your code, collaborate with other developers, and deploy your software efficiently. By mastering these tools, you can improve your DevOps skills and become a better developer.
I hope you enjoyed this blog post and learned something new. You can also follow me on LinkedIn or GitHub for more updates on my DevOps journey.
Thank you for reading!