Basic Git & GitHub for DevOps Engineers 🚀 | Day 8 of 90 Days of DevOps

Ajit Fawade
10 min readJul 26, 2023

--

DevOps is a set of practices that aims to improve the collaboration and communication between development and operations teams, as well as automate and streamline the software delivery process. DevOps engineers need to work with various tools and technologies to achieve these goals, and one of the most essential ones is Git.

Git is a distributed version control system that allows you to track and manage changes to your code, collaborate with other developers, and deploy your software efficiently.

GitHub is a cloud-based platform that hosts Git repositories and provides additional features such as code review, project management, and social networking.

In this blog post, you will learn the basics of Git and GitHub, how to install and use them, and some common commands and workflows that you can apply to your DevOps projects. By the end of this post, you will be able to:

  • 😇 Understand what Git is and how it works
  • 🧑‍💻 Install Git on your computer
  • 🐈‍⬛ Create a free account on GitHub
  • ☁️ Create and clone a repository
  • 📝 Make changes to your code and commit them to your repository
  • 📌Push your changes to GitHub
  • 🕐View the history of your commits using git log

🔥What is Git?

What is Git?

Git is a free and open source software that was created by Linus Torvalds in 2005. It is designed to handle everything from small to very large projects with speed and efficiency. Git is a distributed version control system, which means that every developer has a complete copy of the repository on their local machine. This allows them to work offline, branch and merge easily, and avoid conflicts.

Git works by taking snapshots of your files at different points in time, called commits. Each commit has a unique identifier, called a hash, that represents the state of your code at that moment. You can use git commands to navigate through the history of your commits, compare different versions of your code, revert to previous states, or create new branches.

A branch is a parallel line of development that diverges from the main branch, called master. You can create branches to work on different features or bug fixes without affecting the master branch. When you are done with your branch, you can merge it back to the master branch, or push it to a remote repository.

A remote repository is a copy of your repository that is hosted on another server, such as GitHub. You can use git commands to synchronize your local repository with the remote repository, by pushing your changes or pulling updates from other developers. You can also fork a remote repository, which means creating a copy of it under your own account. This allows you to modify the code without affecting the original repository.

🐈‍⬛What is GitHub?

What is Github

GitHub is a website that provides hosting services for Git repositories. It was founded in 2008 by Tom Preston-Werner, Chris Wanstrath, and PJ Hyett. GitHub has over 50 million users and hosts over 100 million repositories, making it the largest host of source code in the world.

GitHub offers many features that enhance the functionality of Git, such as:

  • Code review: You can use pull requests to propose changes to a repository and get feedback from other developers before merging them.
  • Project management: You can use issues to track bugs, enhancements, or tasks related to your project.
  • Social networking: You can follow other users or organizations, star or watch repositories that interest you, or join communities around topics or technologies.
  • Documentation: You can use GitHub Pages to create websites for your projects or GitHub Wikis to write documentation for your code.
  • Integration: You can use GitHub Actions to automate workflows for testing, deployment, or other tasks. You can also use GitHub Apps or Marketplace to connect with other tools or services.

🛞What is version control?

Version control is the process of managing changes to files or documents over time. Version control systems are software tools that help you keep track of these changes, as well as collaborate with other people on the same project.

Version control systems have many benefits for software development, such as:

  • Backup: You can save multiple versions of your files in case you need to restore them later.
  • History: You can view the history of your files and see who made what changes and when.
  • Collaboration: You can work with other developers on the same project without overwriting each other’s work.
  • Quality: You can review and test your code before merging it with the main branch.
  • Deployment: You can deploy your code to different environments with ease.

Types of version control systems

There are two main types of version control systems: centralized and distributed.

⚙️ Centralized version control systems have a single server that stores all the files and their history. Every developer has a working copy of the files on their local machine, but they need to connect to the server to update or commit their changes. Examples of centralized version control systems are Subversion (SVN) and Perforce.

⚙️ Distributed version control systems have no central server. Every developer has a complete copy of the repository on their local machine, including all the files and their history. They can work offline, branch and merge easily, and push or pull changes to other repositories. Examples of distributed version control systems are Git and Mercurial.

🧑‍💻Install Git on your computer

To use Git, you need to install it on your computer. The installation process varies depending on your operating system.

If you are using Windows, you can download the Git for Windows installer from the official website. Run the installer and follow the instructions. You can choose to use Git from the command line, or from a graphical user interface (GUI) such as Git Bash or Git GUI.

If you are using Mac 🍎, you can download the Git for Mac installer from the official website. Run the installer and follow the instructions. You can also use the Homebrew package manager to install Git. To do that, open the Terminal app and type:

brew install git

If you are using Linux 🐧, you can use your distribution’s package manager to install Git. For example, if you are using Ubuntu, you can open the Terminal app and type:

sudo apt-get update && sudo apt-get install git

To verify that Git is installed correctly, open the command line or terminal and type:

git - version

You should see the version of Git that you have installed.

🐈‍⬛Create a free account on GitHub

To use GitHub, you need to create a free account on their website. Go to https://github.com/ and click on Sign up. Enter your username, email address, and password. You can also choose a plan for your account. The free plan allows you to create unlimited public repositories and up to three private repositories.

After you sign up, you will receive a verification email from GitHub. Click on the link in the email to verify your account. You can also set up some additional settings for your profile, such as your name, bio, avatar, and location.

Some basic commands of Git

Git has many commands that you can use to perform various tasks with your repository. Here are some of the most common ones:

  • git init: This command initializes a new repository in the current directory. It creates a hidden folder called .git that contains all the files and information related to your repository.
  • git status: This command shows the status of your repository. It tells you which files are staged, modified, untracked, or ignored by Git.
  • git add: This command adds files to the staging area, which is a temporary area where you prepare your files before committing them to the repository.
  • git commit: This command creates a new commit with the files in the staging area. A commit is a snapshot of your code at a specific point in time. You need to provide a message that describes what changes you made in the commit.
  • git log: This command shows the history of your commits. It displays information such as the commit hash, author, date, and message for each commit.
  • git branch: This command shows the list of branches in your repository. A branch is a parallel line of development that diverges from the main branch. You can also use this command to create, delete, or rename branches.
  • git checkout: This command switches between branches or commits. You can use it to move to a different branch or revert to a previous state of your code.
  • git merge: This command merges two branches together. It applies the changes from one branch to another branch, creating a new commit in the process.
  • git push: This command pushes your local commits to a remote repository. A remote repository is a copy of your repository that is hosted on another server, such as GitHub.
  • git pull: This command pulls updates from a remote repository to your local repository. It fetches and merges the changes from the remote branch to your current branch.

☁️ Create a new repository on GitHub

To create a new repository on GitHub, follow these steps:

  • Sign in to your GitHub account and click on New in the top left corner.
  • Enter a name for your repository and an optional description.
  • Choose whether you want your repository to be public or private. A public repository is visible to anyone on GitHub, while a private repository is only accessible by you and people you invite.
  • Optionally, you can select some options for your repository, such as adding a README file, choosing a license, or adding a .gitignore file.
  • Click on Create repository.

☁️ Clone it in your machine

To clone a repository from GitHub to your machine, follow these steps:

  • Go to the GitHub page of the repository that you want to clone and click on Code.
  • Copy the URL of the repository from the clipboard icon.
  • Open the command line or terminal and navigate to the directory where you want to clone the repository.
  • Type:
git clone [URL]

Replace [URL] with the URL that you copied from GitHub.

For example:

git clone https://github.com/username/repo-name.git

This will create a folder with the same name as the repository and download all its files and history.

📝Make some changes to a file in the repository and commit them using git

To make some changes to a file in the repository and commit them using git, follow these steps:

  • Open the file that you want to edit in your preferred text editor or IDE.
  • Make the changes that you want to the file and save it.
  • Open the command line or terminal and navigate to the directory of your repository.
  • Type:
git status

This will show you which files have been modified, added, deleted, or untracked by Git.

  • To add the file to the staging area, type:
git add [file]

Replace [file] with the name of the file that you edited.

For example:

git add index.html

You can also use git add . to add all the files in the current directory to the staging area.

  • To create a new commit with the file in the staging area, type:
git commit -m "[message]"

Replace [message] with a short and descriptive message that explains what changes you made in the commit.

For example:

git commit -m "Added a title and a header to index.html"

This will create a new commit with a unique hash and your message.

🕐 Git log

To view the history of your commits using git log, follow these steps:

  • Open the command line or terminal and navigate to the directory of your repository.
  • Type:
git log

This will show you a list of all the commits in your repository, starting from the most recent one. You will see information such as the commit hash, author, date, and message for each commit.

You can also use some options to customize the output of git log, such as:

  • –oneline: This will show only the first line of each commit, which includes the hash and the message.
  • –graph: This will show a graphical representation of the branches and merges in your repository.
  • –all: This will show all the commits from all the branches in your repository.
  • –author=[name]: This will show only the commits made by a specific author. Replace [name] with the name of the author that you want to filter by.
  • –since=[date]: This will show only the commits made since a specific date. Replace [date] with a date in any format that Git can understand, such as “2 weeks ago” or “2023–01–01”.
  • –until=[date]: This will show only the commits made until a specific date. Replace [date] with a date in any format that Git can understand, such as “2 weeks ago” or “2023–01–01”.

You can combine these options to get more specific results. For example, if you want to see only the commits made by Alice in the last month on one line, you can type:

git log - oneline - author=Alice - since="1 month ago"

📌Push the changes to GitHub

To push your local changes to GitHub, follow these steps:

  • Open the command line or terminal and navigate to the directory of your repository.
  • Type:
git push origin master

This will push your master branch to the origin remote, which is usually your GitHub repository. You may need to enter your GitHub username and password to authenticate yourself.

If you have created other branches on your local repository, you can also push them to GitHub by specifying their names instead of master. For example, if you have a branch called feature1, you can type:

git push origin feature1

This will create a new branch on GitHub with the same name and history as your local branch.

In this blog post, you have learned how to use Git and GitHub for DevOps projects. You have learned how to install Git on your computer, create a free account on GitHub, create and clone a repository, make changes and commit them to your repository, view the history of your commits using git log, and push your changes to GitHub.

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. If you have any questions or feedback, feel free to leave a comment below. You can also follow me on LinkedIn or GitHub for more updates on my DevOps journey.

Thank you for reading!

--

--

No responses yet