Basic Git & GitHub for DevOps Engineers

Basic Git & GitHub for DevOps Engineers

#90DaysofDevOps Challenge - Day 8

Table of contents

No heading

No headings in the article.

▶What is Git?

It is a distributed, open-source version control system.

It enables developers and data scientists to track code, merge changes and revert to older versions. It allows you to sync changes with a remote server. Due to its flexibility and popularity, Git has become an industry standard as it supports almost all development environments, command-line tools, and operating systems.

▶Git usage

It is used to manage projects with Repositories**, Clone a project to work on a local copy, control and track changes with Staging and Committing,* Branch and Merge to allow for work on different parts and versions of a project, Pull the latest version of the project to a local copy and Push* local updates to the main project.

This makes it what is known as a ‘Distributed Version Control System’, and enables multiple developers to work on the same project.

▶What is GitHub?

It is a platform that can hold repositories of code in cloud-based storage so that multiple developers can work on a single project and see each other's edits in real-time. It makes it easier to collaborate using Git.

It also includes project organization and management features. You can assign tasks to individuals or groups, set permissions, and roles for collaborators, and use comment moderation to keep everyone on task.

Additionally, GitHub repositories are publically available. Developers from across the globe can interact with and contribute to each other’s code in order to modify or improve it, which is known as ‘social coding’.

There are three primary actions you can take when it comes to interacting with other developers’ code on GitHub:

  • Fork: The process of copying another’s code from the repository in order to modify it.

  • Pull: When you’ve finished making changes to someone else’s code, you can share them with the original owner via a ‘pull request’.

  • Merge: Owners can add new changes to their projects via a merge, and give credit to the contributors who suggested them.

For fresher developers, GitHub enables you to share projects on your profile and keeps a timeline of all the ones you have contributed to as well.

▶What is the difference between Git vs GitHub?

Git is a local VCS software that enables developers to save snapshots of their projects over time. It’s generally best for individual use.

GitHub is a web-based platform that incorporates Git’s version control features so they can be used collaboratively. It also includes project and team management features, as well as opportunities for networking and social coding.

▶Install Git on Linux (Rocky distro based on RHEL)

Method 1 (Appstream)

$sudo dnf upgrade --refresh
$sudo dnf install git

One advantage of using Rocky Linux is that Git is included as a default package, which means it is always up-to-date and ready to use. This makes the installation process very simple and can be accomplished by running a single command in the terminal.

Method 2 (Manual)

Using this method to install Git on Rocky Linux is compiling it from source code. This approach is useful if you require a specific version of Git unavailable through the package manager or if you wish to install Git with custom configurations.

Visit the release page on the official Git website to locate the latest stable version or the particular version you need. Once you have identified the version you want, download it using the wget command in the terminal. This command will fetch the Git source code from the official repository and prepare it for compilation.

$wget https://github.com/git/git/archive/refs/tags/vx.x.x.tar.gz

To extract the source code, execute the tar command in the terminal.

$tar -xzf git-x.x.x.tar.gz

Change to the proper directory by navigating to it in the terminal.

$cd git-x.x.x

It is recommended to install the development tools as the first step, as this will ensure that nearly all the required dependencies are installed.

$sudo dnf groupinstall "Development Tools"

Additionally, there are a few dependencies that are not included in the development tools pack. To install these dependencies, execute the following command in the terminal.

$sudo dnf install libcurl-devel expat-devel

To configure the script, run the makecommand in the terminal.

$make prefix=/usr/local all

Next, install Git with the following command.

$sudo make prefix=/usr/local install

Confirm the version of Git that is currently installed on your system by running the command git –versionin the terminal. This will display the version number of the Git installation on your system.

$git --version

▶GitHub create an account and repository

To create an account on GitHub, you will be asked for some personal information like your name, confirm your email, set a username and password, and your account should be set up in minutes.

▶Create a GitHub repository

After you create your GitHub account, click on the + button and select a new repository. After that, type the repository name and add a simple description. It will create an empty public repository.

There are many ways to clone remote repositories to the local directory, and GitHub provides a detailed guide on how to clone, add remote, and initialize a Git project.

▶Common Commands

To clone a repository:

$git clone https://github.com/fjblsouza/Git-Test.git

To create a new directory:

$git remote add origin https://github.com/fjblsouza/Test-Git.git

To a simple Commit:

Before we add files to our repository, make sure you are in the correct local directory.

We will start creating a README file with the heading Test-Git. Then, we will add it to the staging area by using git add.

git status shows that we are on the main branch and the README.md file is staged and ready to be committed.

$echo "# Git-Test">> README.md
$git add README.md
$git status

To create our first commit, we will use git commit with a message.

To add any directory, file, or data after the initial command:

$git add /home/user/Test-Git/RockyLinux.png

Or If you want to add all files to the staging area, then use dot .

$git add .

To Commit and Push:

We will commit the changes with a simple message, and the output shows all the new files in create mode.

$git commit -m "git-test"

Syncing with GitHub remote repository requires a remote name and branch name git push <remote-name> <branch-name>. If you have only one remote and one branch, then using git push will work.

After git push, the pop-up window will ask for the credentials, just add your GitHub username or password. You can also generate Personal access tokens and add them instead of the password.

▶Git Basic Commands

The basic commands include initializing the Git repository, saving changes, checking logs, pushing the changes to the remote server, and merging.

git init create a Git repository in a local directory.

git clone <remote-repo-address>: copy the entire repository from a remote server to a remote directory. You can also use it to copy local repositories.

git add <file.txt>: add a single file or multiple files and folders to the staging area.

git commit –m “Message”: create a snapshot of changes and save it in the repository.

git config use to set user-specific configurations like email, username, and file format.

git status shows the list of changed files or files that have yet to be staged and committed.

git push <remote-name> <branch-name>: send local commits to the remote branch of the repository.

git checkout -b <branch-name>: creates a new branch and switches to a new branch.

git remote –v: view all remote repositories.

git remote add <remote-name> <host-or-remoteURL>: add a remote server to a local repository.

git branch –d <branch-name>: delete the branch.

git pull merge commits to a local directory from a remote repository.

git merge <branch-name>: after resolving merge conflicts the command blends the selected branch into the current branch.

git log show a detailed list of commits for the current branch.