Introduction

Git is one of the most used tools in the IT industry, whether it be in the field of software development or Data Science. Every project needs to be version-controlled, and Git is the tool that helps us in version control for various projects.  

This Git cheat sheet is designed for those who have an interest in learning file management using Git as a tool. This cheat sheet will be a handy reference not only when you learn Git but also when you start working with it. Don’t worry if you are a beginner! Intellipaat’s Git cheat sheet will help you with the basics.

Git Cheat Sheet
To ace the concepts of Git, here are the topics we will cover:

Watch this Git Tutorial for Beginners video:

Advanced Git Cheat Sheet

Advanced Git Commands Cheat Sheet

What is Version Control?

In layman’s terms, version control, also known as source control, is the practice of tracking and managing changes to software code.

Technically, version control is the management of changes made to the code, documents, programs, and other information. The changes are referred to as versions.

A version control system (VCS) performs the following:

  1. It allows developers to work simultaneously.
  2. It does not allow overwriting each other’s changes.
  3. It maintains a history of every version.

There are two types of version control systems:

  • Centralized Version Control System (CVCS)
  • Distributed Version Control System (DVCS)
Centralized Version Control System Distributed Version Control System
In the Centralized Version Control System there is a single remote server repository. In Distributed Version Control System, other than the remote server repository, each developer has their own local repository server that contains the entire version history
CVCS is slower compared to DVCS as for every operation you need to connect to the remote repository DVCS is faster than CVCS as you perform every operation locally.
If the remote server crashes then you lose all your data If the remote server crashes, still you will have a copy of the repository as a local repository. 

Version Control System (VCS)

Our DevOps tool, Git, falls under the category of distributed Version Control System.

What is Git?

Git is a free and open-source DevOps tool that is used as a version control system. It was created by Linus Torvalds, the creator of the Linux Operating System Kernel in 2005.

It is a repository that is used to manage the version of a project or set of files as they change over time. Here, multiple developers can contribute to the code simultaneously. It also supports a non-linear way of development where you can make multiple branches for each new feature.

According to a survey by Stack Overflow, 93% of the developers are using Git as their Version Control Tool.

Install Git and GUIs

Git can be installed on operating systems like Windows, Mac, and Linux. By default, Git is pre-installed on Mac and Linux machines!

Download Git for Mac

Follow the steps to install Git on your Mac Machine, if not installed yet: 

1. Download and Install homebrew

# /bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)”

2. Go to your terminal and execute the following command: 

# brew install git

For more information: https://git-scm.com/download/mac

Download Git for Windows

Follow the steps to install Git on your Windows Machine, if not installed yet: 

1. Download the installer from the link given below: 

https://git-scm.com/download/win

2. Run the .exe file you downloaded and install it on your system.

Download Git for Linux

Follow the steps to install Git on your Linux  Machine, if not installed yet: 

1. For Ubuntu, Run the following command: 

# apt-get install git

2. For other Linux distributions, follow the steps in the link:

https://git-scm.com/download/linux

Git in DevOps

Git in DevOps

  • The DevOps tool, Git, is an integral part of the DevOps lifecycle. 
  • The whole DevOps LifeCycle has multiple stages like Plan, Code, Build, Release, Deploy, Operate, and Monitor. We follow the principles of Continuous Integration and Continuous Delivery/Deployment (CI/CD) in it.
  • Git is mainly used in the Plan and Code part of the DevOps Lifecycle, wherein we plan the problem statement, define an approach, and write the code. 
  • As a DevOps tool, Git is used to improve the efficiency of the code by empowering teamwork and effectively reducing errors to improve the overall quality of the code.

Be ready for the industry by going through these Top DevOps Interview Questions and Answers!

Key Terminologies in Git

Before we deep dive into Git, it is very important to understand the terminologies used in the technology. Here is a list of all the common terms that you will find in the upcoming sections:

  • Local repository: It is a local directory that contains the code files for the project.
  • Remote repository: It is an online version of the local repository that is hosted on services like GitHub, GitLab, and BitBucket.
  • Cloning: The act of making a clone or copy of a repository in a new directory.
  • Commit:  It is a snapshot of the project’s staged changes.
  • Branch: A branch is a copy of the project used for working in an isolated environment without affecting the main project.
  • Git merge: The process of combining two branches in Git.
  • .gitignore file: It is a file that specifies intentionally untracked files that Git should ignore.
  • Staging area: A cache that holds changes that will go into your next commit.
  • Git stash: Another type of cache that holds unwanted changes you may want to return to later.
  • Commit ID or hash: A unique identifier for each commit, used for switching to different save points.
  • HEAD (always capitalized letters): A reference name for the latest commit, to save you from having to type commit IDs.

Branches in Git

Branches are special copies of the code base that allow you to work on new features in an isolated environment. The changes made in a branch won’t affect the main branch, which is also the main project development environment.

Branches in GIT

Taking reference from the above image, 

  • The Black line is the main branch or the main codebase. Now when multiple people are working on the same codebase, we must review any code that has to be added to reduce errors and improve the overall quality of the code. 
  • The violet color line indicates a new feature being developed. The new feature is being developed in an isolated environment with a copy of the main codebase. 
  • When all the development and testing have been done, we review the code and merge it to the original main branch that is denoted with the help of the orange color line.

Git Life Cycle

Let us have a look at the Life Cycle that Git has and understand more about its life cycle:

Step 01 – Cloning Repository: We are cloning the code that resides in the remote repository to make a local repository on our system.

Step 02 – Adding Changes: We perform multiple operations or changes, like editing a file, adding new code, deleting previous code, moving files, etc.

Step 03 – Pulling Changes from the Remote Repository: Then we pull the files again from the remote repository to check if someone has made some recent changes or not.

Step 04 – Reviewing the Code: We Review the changes, move the code to the staging area, and remove any conflict present in the code.

Step 05 – Committing Changes: We finally commit the changes with a proper commit message.

Git Life Cycle

 

Become a master of DevOps by enrolling in this online DevOps Training in London!

Features of Git

Git is a Version Control System that supports distributed nonlinear workflows by providing data assurance for developing quality software.

Its features are as follows:

  • Multiple developers can contribute to the code at the same time
  • Compatible with systems and protocol
  • Allows the non-linear development of code
  • Lightweight
  • Faster than the remote repository
  • Reliable open-source tool

For more information, visit the Git vs GitHub blog by Intellipaat.

Git Commands

1. Git configuration

These commands are used for setting up configuration values like first name, last name, and email ID across all local repositories. Using these commands you are introducing yourself to Git.

Commands Description
git config –global user.name “[firstname lastname]” Set the global configuration for the Git user’s name.
git config –global user.email “[valid-email]” Set the global configuration for the Git email.
git config –global color.ui auto Enable automatic coloring for Git output, i.e., Git will automatically colorize its output when it’s going to a terminal.
git config –list Displays the current configuration settings for Git on your system.

2. Setup and Initialization

These commands are used to initialize a repository which means setting up a new project or cloning an existing repository.

Commands Description
git init Initialize an existing directory.
git clone [url] Retrieve the repository from the hosted location through the URL.

3. Staging and Snapshots

These commands will help you with all the operations done in the Git Staging Area, i.e., before you commit the changes to the branch.

Commands Description
git status To know the status
git diff Displays what has been changed but not staged yet.
git add [file] To add a file
git add [filename] [2nd filename] [3rd filename] To add multiple files
git reset [file] Used to unstage a file while keeping the changes in the working directory.
git commit -m “[descriptive message]”  Commit your staged content
git diff –staged Displays what has been staged but not committed yet.
git commit –amend -m “new_message” To amend the last commit or the last message

4. Branch and Merge

These commands will help you while developing a new feature in an isolated environment and later on, after proper review and testing merging it with the original codebase.

Commands Description
git branch To list branches
git branch -a To list all the branches
git branch [branch name] To create a new branch
git branch -d [branch name] To delete a branch
git push origin –delete [branchName] To delete a remote branch
git checkout -b [branch name] To create and switch to a new branch
git checkout -b [branch name] origin/[branch name] To clone and switch to a remote branch
git checkout [branch name] To switch to a branch
git checkout – To switch to the branch last checked out
git checkout — [file-name.txt] Used to discard any changes made to a file
git merge [branch name] To merge a branch into an active branch

Learn more about the basic commands in this Git Tutorial by Intellipaat.

5. Inspect and Compare

These commands are used to inspect the logs that contain all the commit history. This can also help you with the differences between the branches.

Commands Description
git log Show the commit history
git diff branchB…branchA Show the difference between branchA and branchB 
git log branchB..branchA Show the commits on branchA that are not there on branchB 
git log –follow [file] Show the commits that changed the file
git show [SHA] Show any object in a human-readable format 

6. Share and Update

These commands are used for fetching fresh updates from the remote repository and accordingly updating the local repository. With the help of these commands, you will also be able to push new code to the remote repository from the local repository. Fetching updates from another repository and updating the local repositories.

Commands Description
git push origin [branch name] To push a branch to a remote repository
git push -u origin [branch name] To push the changes made to a remote repository (-u remember the branch for the next use)
git push origin –delete [branch name] To delete a remote branch
git pull To update a local repository to the newest commit
git pull origin [branch name] To pull the changes from a remote repository
git remote add origin ssh://[email protected]/[username]/[repository-name].git To add a remote repository
git remote set-url origin ssh://[email protected]/[username]/[repository-name].git To set a repository’s origin branch to SSH
git fetch [alias] Fetch all the branches from that hosted remote repository.

Interested in grabbing an industry-recognized DevOps certification? Intellipaat’s DevOps Course in Bangalore is just designed for you!

7. Rewrite History

These commands are used to rewrite branches, update  commits, and clear history.

Commands Description
git rebase [branch] Used to integrate changes from one branch into another branch.
git reset –hard [commit]  Used to reset the current branch to a specific commit

8. Temporary Commit

These commands are used to create a temporary snapshot of your work using a commit. This could be useful in various scenarios, such as experimenting with changes or working on a feature that is not ready to be permanently committed to the version history.

Commands Description
git stash To stash the changes in a dirty working directory
git stash pop  Write working from the top of the stash stack.
git stash list  List the stack-order of stashed file changes. 
git stash drop  Discard the changes from the top of the stash stack.
git stash clear To remove all the stashed entries

Ace Git with Intellipaat’s curated Git Interview Questions and Answer 2024

How Do Git Commands Work?

How Do Git Commands Work

Intellipaat’s comprehensive DevOps Training would help you be a certified DevOps Engineer!

Conclusion

I hope this Git cheat sheet was helpful. These commands will improve your productivity in the field of Git. We have listed all the major commands in this sheet, so there is no need to remember them; just bookmark this page. Best of luck!

Are you looking to start your career or even elevate your skills in the field of DevOps? You can enroll in our comprehensive Git and Github Course or enroll in the Advanced Certification in DevOps & Cloud Computing in collaboration with IIT Madras with Intellipaat and get certified today.

If you want to deep dive into Git with some questions to prepare, feel free to join Intellipaat’s vibrant Git Community and get answers to your queries from like-minded enthusiasts.

Course Schedule

Name Date Details
Big Data Course 30 Mar 2024(Sat-Sun) Weekend Batch
View Details
Big Data Course 06 Apr 2024(Sat-Sun) Weekend Batch
View Details
Big Data Course 13 Apr 2024(Sat-Sun) Weekend Batch
View Details