CONTENTS

You've successfully subscribed to The DevOps Bootcamp 🚀
Great! Next, complete checkout for full access to The DevOps Bootcamp 🚀
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.

The Beginner's Guide to Learn the Basics of Git / Github

Yiadh TLIJANI
Yiadh TLIJANI

What's Git?

Git is a version-control system for tracking changes in computer files and coordinating work on those files among multiple people. Git is a Distributed Version Control System. So Git does not necessarily rely on a central server to store all the versions of a project’s files. Instead, every user “clones” a copy of a repository (a collection of files) and has the full history of the project on their own hard drive. This clone has all of the metadata of the original while the original itself is stored on a self-hosted server or a third party hosting service like GitHub.

Git helps you keep track of the changes you make to your code. It is basically the history tab for your code editor(With no incognito mode ?). If at any point while coding you hit a fatal error and don’t know what’s causing it you can always revert back to the stable state. So it is very helpful for debugging. Or you can simply see what changes you made to your code over time.

Git also helps you synchronise code between multiple people. So imagine you and your friend are collaborating on a project. You both are working on the same project files. Now Git takes those changes you and your friend made independently and merges them to a single “Master” repository. So by using Git you can ensure you both are working on the most recent version of the repository. So you don’t have to worry about mailing your files to each other and working with a ridiculous number of copies of the original file.

To sum up, a version control system like Git makes our code easy to:

  • Keep the whole track of each code and its committed history of the repository.
  • Work together as a team from the remote place as well, which makes code repository available at the commonplace.
  • Take care as who made which changes and when for which file, each activity is saved.
  • Easy to deploy to staging or production server.

How does Git work?

We can say Git stores all this information in a data structure called a Git repository. The repository is the core of Git. To be very clear, a Git repository is the directory where all of your project files and the related metadata resides.

Take a look at git workflow shows below, how changes are saved from working directory to remote repository

Working directory — The working directory contains the set of working files for the repository. You can change and modify the content whenever you want and commit the changes to the repository. Every new commit is going to save in binary format so that the same name of files committed by others will also be saved with another unique binary file name.

Staging area — The staging area is the place to store changes in the working tree before the commit. It’s the middle stage which contains a snapshot of the changes in the working directory for changed or new changes that are relevant to create the next commit and stores their mode like filetype etc.

Local Repository — A repository contains the history, the different versions over time and all different branches and tags. In Git each copy of the repository is a complete repository.

Remote Repository — It is a shared repository hosted in a server where all the collaborators upload changes made to files.

If you consider a file in your Working Directory, it can be in three possible states.

  1. It can be staged. Which means the files with the updated changes are marked to be committed to the local repository but not yet committed.
  2. It can be modified. Which means the files with the updated changes are not yet stored in the local repository.
  3. It can be committed. Which means that the changes you made to your file are safely stored in the local repository.

Working with Git Basic Commands

The gragh below shows the different stages of hosting your code changes as well as the basic git commands used to exchange data between them:

  • git add is a command used to add a file that is in the working directory to the staging area.
  • git commit is a command used to add all files that are staged to the local repository.
  • git push is a command used to add all committed files in the local repository to the remote repository. So in the remote repository, all files and changes will be visible to anyone with access to the remote repository.
  • git fetch is a command used to get files from the remote repository to the local repository but not into the working directory.
  • git merge is a command used to get the files from the local repository into the working directory.
  • git pull is command used to get files from the remote repository directly into the working directory. It is equivalent to a git fetch and a git merge .

Now that we know what Git is and it’s basic terminologies, let’s see how we can place a file under git. We’re going to do it the right way and the difficult way. Without any GUI applications.

Make a GitHub Account

Install  and Configure Git on the local machine

Configure working directory

Add files to the Staging Area

Check staged files

Unstage file

Commit Changes to local Git Repo

Uncommit Changes from local Git Repo

See the Changes you made to your file

Add a remote origin and Push

Revert back to the last committed version to the Git Repo

View Commit History

Working with Git Advanced Commands

Use of Git Reset, Git Revert, Git Checkout

Use of Git Merge and Git rebase

Squash, rename and reorder commits

Yiadh TLIJANI

Founder of The DevOps Bootcamp | Senior DevSecOps Engineer | Certified Kubernetes Developer, Administrator and Security Specialist