The Wayback Machine - https://web.archive.org/web/20240822232057/https://www.geeksforgeeks.org/git-cheat-sheet/
Open In App

Git Cheat Sheet

Last Updated : 19 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Git Cheat Sheet is a comprehensive quick guide for learning Git concepts, from very basic to advanced levels. By this Git Cheat Sheet, our aim is to provide a handy reference tool for both beginners and experienced developers/DevOps engineers. This Git Cheat Sheet not only makes it easier for newcomers to get started but also serves as a refresher for experienced professionals.

Git Cheat Sheet

Git Cheat Sheet

In this Git Cheat Sheet, we’ve covered all the basics to advanced Git commands that the developers required during the development and deployment process. Moreover, it is well-structured and categorized according to different use cases. It includes Git and GitHub, Git download, Git Configuration & Setup, Git commands, Git bash, Creating and Getting Git Projects, Git Snapshots, Branching and Merging in Git, Sharing and Updating in Git, Git Comparison, Managing History in Git, and more.

What is Git?

Git is the free and open-source distributed version control systems that’s responsible for everything GitHub related that happens locally on your computer.

Understanding Version Control

Version control, also known as source control, is the technique of tracking and managing changes to codes and these are the systems that are software tools that enable software teams to manage modifications to source code as time passes.

What is GitHub?

GitHub is a widely-used Free-to-use cloud Storage platform with version control and many other essential features that specifically helps developers to manage and deploy their projects on GitHub.

Benefits of Using Git

  • History Tracking: Git allows you to track every change made in your project, including: who made the change and when it was made.
  • Collaboration: Multiple developers can be able work on the same project at the same time, and Git efficiently manages the merging of changes in code.
  • Branching and Merging: Git enables developers to create branches to work on new features or bug fixes and later merge them back into the main codebase.
  • Offline Work: Git works offline, which means you can commit changes and work on your project even without an internet connection.
  • Learn Git Cheat Sheet (Basic to Advanced Concepts)

    Learn Git Cheat Sheet (Basic to Advanced Concepts)

    Git Installation Commands

    Here are the Git installation commands for different operating systems:

    Commands

    Description

    Git for Windows stand-alone installer.  For more Details Read Here
    $ brew install git Install Git with Homebrew on Mac OS
    $ sudo port selfupdate Install Git with MacPorts on Mac OS
    $ sudo apt-get install git Install Command for Linux
    $ git –version Shows the current version of your Git

    Git Configuration & Setup

    Here are Git configuration and setup commands:

    Commands 

    Description

    git config –global user.name “Your Name” Set your username globally.
    git config –global user.email “youremail@example.com” Set your email globally.
    git config –global color.ui auto – Set to display colored output in the terminal
    git help Display the main help documentation, showing a list of commonly used Git commands.

    Initializing a Repository  

    Here are the Git initializing a repository commands:

    Commands 

    Description

    git init Initializes a new Git repository in the current directory.
    git init <directory> Creates a new Git repository in the specified directory.
    git clone <repository_url> this Clones a repository from a remote server to your local machine.
    git clone –branch <branch_name> <repository_url> Clones a specific branch from a repository.

    Basic Git Commands

    Here are some basic Git commands:

    Commands 

    Description

    git add <file> Adds a specific file to the staging area.
    git add . or git add –all Adds all modified and new files to the staging area.
    git status Shows the current state of your repository, including tracked and untracked files, modified files, and branch information.
    git status –ignored Displays ignored files in addition to the regular status output.
    git diff Shows the changes between the working directory and the staging area (index).
    git diff <commit1> <commit2> Displays the differences between two commits.
    git diff –staged or git diff –cached Displays the changes between the staging area (index) and the last commit.
    git diff HEAD Display the difference between the current directory and the last commit
    git commit Creates a new commit with the changes in the staging area and opens the default text editor for adding a commit message.
    git commit -m “<message>” or git commit –message “<message>” Creates a new commit with the changes in the staging area and specifies the commit message inline.
    git commit -a or git commit –all Commits all modified and deleted files in the repository without explicitly using git add to stage the changes.
    git notes add Creates a new note and associates it with an object (commit, tag, etc.).
    git restore <file> Restores the file in the working directory to its state in the last commit.
    git reset <commit> Moves the branch pointer to a specified commit, resetting the staging area and the working directory to match the specified commit.
    git reset –soft <commit> Moves the branch pointer to a specified commit, preserving the changes in the staging area and the working directory.
    git reset –hard <commit> Moves the branch pointer to a specified commit, discarding all changes in the staging area and the working directory, effectively resetting the repository to the specified commit.
    git rm <file> Removes a file from both the working directory and the repository, staging the deletion.
    git mv Moves or renames a file or directory in your Git repository.

    Also, check: Basic Git Commands with Examples

    Git Commit (Updated Commands)

    Here are some of the updated commands for Git commit:

    Commands 

    Description

    git commit -m “feat: message” Create a new commit in a Git repository with a specific message to indicate a new feature commit in the repository.
    git commit -m “fix: message” Create a new commit in a Git repository with a specific message to fix the bugs in codebases
    git commit -m “chore: message” Create a new commit in a Git repository with a specific message to show routine tasks or maintenance.
    git commit -m “refactor: message” Create a new commit in a Git repository with a specific message to change the code base and improve the structure.
    git commit -m “docs: message” Create a new commit in a Git repository with a specific message to change the documentation.
    git commit -m “style: message” Create a new commit in a Git repository with a specific message to change the styling and formatting of the codebase.
    git commit -m “test: message” Create a new commit in a Git repository with a specific message to indicate test-related changes.
    git commit -m “perf: message” Create a new commit in a Git repository with a specific message to indicate performance-related changes.
    git commit -m “ci: message” Create a new commit in a Git repository with a specific message to indicate the continuous integration (CI) system-related changes.
    git commit -m “build: message” Create a new commit in a Git repository with a specific message to indicate the changes related to the build process.
    git commit -m “revert: message” Create a new commit in a Git repository with a specific message to indicate the changes related to revert a previous commit.

    Branching and Merging

    Here are some Git branching and merging commands:

    Commands 

    Description

    git branch Lists all branches in the repository.
    git branch <branch-name> Creates a new branch with the specified name.
    git branch -d <branch-name> Deletes the specified branch.
    git branch -a Lists all local and remote branches.
    git branch -r Lists all remote branches.
    git checkout <branch-name> Switches to the specified branch.
    git checkout -b <new-branch-name> Creates a new branch and switches to it.
    git checkout — <file> Discards changes made to the specified file and revert it to the version in the last commit.
    git merge <branch> Merges the specified branch into the current branch.
    git log Displays the commit history of the current branch.
    git log <branch-d Displays the commit history of the specified branch.
    git log –follow <file> Displays the commit history of a file, including its renames.
    git log –all Displays the commit history of all branches.
    git stash Stashes the changes in the working directory, allowing you to switch to a different branch or commit without committing the changes.
    git stash list Lists all stashes in the repository.
    git stash pop Applies and removes the most recent stash from the stash list.
    git stash drop Removes the most recent stash from the stash list.
    git tag Lists all tags in the repository.
    git tag <tag-name> Creates a lightweight tag at the current commit.
    git tag <tag-name> <commit> Creates a lightweight tag at the specified commit.
    git tag -a <tag-name> -m “<message>” Creates an annotated tag at the current commit with a custom message.

    Remote Repositories

    Here are some Git remote repositories commands:

    Commands 

    Description

    git fetch Retrieves change from a remote repository, including new branches and commit.
    git fetch <remote> Retrieves change from the specified remote repository.
    git fetch –prune Removes any remote-tracking branches that no longer exist on the remote repository.
    git pull Fetches changes from the remote repository and merges them into the current branch.
    git pull <remote> Fetches changes from the specified remote repository and merges them into the current branch.
    git pull –rebase Fetches changes from the remote repository and rebases the current branch onto the updated branch.
    git push Pushes local commits to the remote repository.
    git push <remote> Pushes local commits to the specified remote repository.
    git push <remote> <branch> Pushes local commits to the specified branch of the remote repository.
    git push –all Pushes all branches to the remote repository.
    git remote Lists all remote repositories.
    git remote add <name> <url> Adds a new remote repository with the specified name and URL.

    Git Comparison

    Here are some Git comparison commands:

    Commands 

    Description

    git show Shows the details of a specific commit, including its changes.
    git show <commit> Shows the details of the specified commit, including its changes.

    Git Managing History

    Here are some Git managing history commands:

    Commands 

    Description

    git revert <commit> Creates a new commit that undoes the changes introduced by the specified commit.
    git revert –no-commit <commit> Undoes the changes introduced by the specified commit, but does not create a new commit.
    git rebase <branch> Reapplies commits on the current branch onto the tip of the specified branch.

    Why use Git?

    Here are some of the reasons why you might want to use Git:

    • Track changes to your code
    • Collaborate on projects with others
    • Maintain an organized code history
    • Easily revert to previous versions when needed
    • Release your code efficiently and manage versions
    • Enhance productivity and code integrity in software development.

    Conclusion

    In conclusion, This Git Cheat Sheet is thoughtfully organized and categorized, making it easy for developers to quickly find the commands they need for specific use cases. Whether it’s configuring and setting up Git, creating and managing projects, taking snapshots, branching and merging, sharing and updating, comparing changes, or managing version history, the Git Cheat Sheet covers it all.

    By utilizing this resource, developers can enhance their productivity and efficiency in working with Git, ultimately leading to smoother and more successful software development projects.

    Related Resource:

    FAQs on Git Cheat Sheet

    Q.1 What is the difference between Git and GitHub?

    Git is a version control system that allows developers to track changes to their code locally, while GitHub is a web-based platform that provides hosting for Git repositories and facilitates collaboration among developers.

    Q.2 Why is it called Git?

    The name “git” was given by Linus Torvalds when he wrote the very first version.

    Q.3 How to pull all data from git?

    git fetch –all command retrieves metadata on each change made to all the branches in a repository. The git pull –all command downloads all of the changes made across all branches to your local machine

    Q.4 Where is Git data stored?

    Git keeps things organized with two main data structures known as the object store and the index.All of this repository data is stored at the root of your working directory in a hidden subdirectory named . git. It’s like a secret club for your code.



    Previous Article
    Next Article

    Similar Reads

    Difference Between Git remote prune, Git prune and Git fetch --prune
    Git is a version control system that helps developers manage and track changes in their codebase. Among its many commands, git remote prune, git prune, and git fetch --prune are essential for maintaining a clean and organized repository. This article will explain the differences between these commands, their syntax, uses, and provide examples to il
    4 min read
    jQuery Cheat Sheet – A Basic Guide to jQuery
    What is jQuery?jQuery is an open-source, feature-rich JavaScript library, designed to simplify the HTML document traversal and manipulation, event handling, animation, and Ajax with an easy-to-use API that supports the multiple browsers. It makes the easy interaction between the HTML &amp; CSS document, Document Object Model (DOM), and JavaScript.
    15+ min read
    Tkinter Cheat Sheet
    Tkinter, the standard GUI library for Python, empowers developers to effortlessly create visually appealing and interactive desktop applications. This cheat sheet offers a quick reference for the most common Tkinter widgets and commands, along with valuable tips and tricks for crafting well-designed UIs. In this Cheat Sheet, whether you're a beginn
    8 min read
    CSS Cheat Sheet - A Basic Guide to CSS
    What is CSS? CSS i.e. Cascading Style Sheets is a stylesheet language used to describe the presentation of a document written in a markup language such as HTML, XML, etc. CSS enhances the look and feel of the webpage by describing how elements should be rendered on screen or in other media. What is a CSS Cheat Sheet? CSS Cheat Sheet provides you wi
    13 min read
    ggplot2 Cheat Sheet
    Welcome to the ultimate ggplot2 cheat sheet! This is your go-to resource for mastering R's powerful visualization package. With ggplot2, you can create engaging and informative plots effortlessly. Whether you're a beginner or an experienced programmer, ggplot2's popularity and versatility make it an essential skill to have in your R toolkit. If you
    13 min read
    Python OpenCV Cheat Sheet
    The Python OpenCV Cheat Sheet is your complete guide to mastering computer vision and image processing using Python. It's designed to be your trusty companion, helping you quickly understand the important ideas, functions, and techniques in the OpenCV library. Whether you're an experienced developer needing a quick reminder or a newcomer excited to
    15+ min read
    C Cheat Sheet
    This C Cheat Sheet provides an overview of both basic and advanced concepts of the C language. Whether you're a beginner or an experienced programmer, this cheat sheet will help you revise and quickly go through the core principles of the C language. In this Cheat Sheet, we will delve into the basics of the C language, exploring its fundamental con
    15+ min read
    React Cheat Sheet
    React is an open-source JavaScript library used to create user interfaces in a declarative and efficient way. It is a component-based front-end library responsible only for the view layer of a Model View Controller (MVC) architecture. React is used to create modular user interfaces and promotes the development of reusable UI components that display
    9 min read
    Markdown Cheat Sheet
    Markdown is a simple way to write and format text using plain text symbols. It's a lightweight language that lets you create nicely formatted text without the use of complex coding. The idea is to make it easy for people to read and understand the formatting, even if they're looking at the raw text. This cheat sheet will go over some of the key com
    6 min read
    Docker Cheat Sheet : Complete Guide (2024)
    Docker is a very popular tool introduced to make it easier for developers to create, deploy, and run applications using containers. A container is a utility provided by Docker to package and run an application in a loosely isolated environment. Containers are lightweight and contain everything needed to run an application, such as libraries and oth
    11 min read
    NumPy Cheat Sheet: Beginner to Advanced (PDF)
    NumPy stands for Numerical Python. It is one of the most important foundational packages for numerical computing &amp; data analysis in Python. Most computational packages providing scientific functionality use NumPy’s array objects as the lingua franca for data exchange. In this Numpy Cheat sheet for Data Analysis, we've covered the basics to adva
    15+ min read
    Angular Cheat Sheet - A Basic Guide to Angular
    Angular is a client-side TypeScript-based, front-end web framework developed by the Angular Team at Google, that is mainly used to develop scalable single-page web applications(SPAs) for mobile &amp; desktop. Angular is a great, reusable UI (User Interface) library for developers that helps in building attractive, steady, and utilitarian web pages
    15+ min read
    Bootstrap Cheat Sheet - A Basic Guide to Bootstrap
    Bootstrap is a free, open-source, potent CSS framework and toolkit used to create modern and responsive websites and web applications. It is the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites. Nowadays, websites are perfect for all browsers and all sizes of screens. What is Bootstrap Cheat Sheet?A
    15+ min read
    GeeksforGeeks Master Sheet - List of all Cheat Sheets
    In this Master Sheet, we’ll cover all the important cheat sheets like SDE Sheets, DSA Topics Sheets, HTML, CSS, JavaScript, React, Angular, Bootstrap, jQuery, Python, Data Science, C, C++, Java, Computer Networks, Company Wise SDE sheets, etc. What is a Master Sheet ? A selection of comprehensive reference aids in the form of cheat sheets are provi
    10 min read
    Ansible Cheat Sheet
    Ansible is a powerful open-source automation tool that is meant for configuration management and application deployment. It works with agentless architecture and a push-based mechanism. It uses YAML syntax for orchestrating the infrastructure efficiently across different environments. It will be effective and preferable in managing both small-scale
    10 min read
    Python Cheat sheet (2024)
    Python is one of the most widely-used and popular programming languages, was developed by Guido van Rossum and released first in 1991. Python is a free and open-source language with a very simple and clean syntax which makes it easy for developers to learn Python. It supports object-oriented programming and is most commonly used to perform general-
    15+ min read
    Pandas Cheat Sheet for Data Science in Python
    Pandas is a powerful and versatile library that allows you to work with data in Python. It offers a range of features and functions that make data analysis fast, easy, and efficient. Whether you are a data scientist, analyst, or engineer, Pandas can help you handle large datasets, perform complex operations, and visualize your results. This Pandas
    15+ min read
    C++ STL Cheat Sheet
    The C++ STL Cheat Sheet provides short and concise notes on Standard Template Library (STL) in C++. Designed for programmers that want to quickly go through key STL concepts, the STL cheatsheet covers the concepts such as vectors and other containers, iterators, functors, etc., with their syntax and example. What is Standard Template Library(STL)?T
    15+ min read
    HTML Cheat Sheet
    HTML (HyperText Markup Language) serves as the foundational framework for web pages, structuring content like text, images, and videos. HTML forms the backbone of every web page, defining its structure, content, and interactions. Its enduring relevance lies in its universal adoption across web development, ensuring that regardless of the framework
    15 min read
    SQL Cheat Sheet ( Basic to Advanced)
    In this article, we will explore the ultimate SQL cheat sheet with the PDF download, covering a basic to advance of SQL commands, Joins in SQL, CRUD Operations, SQL Trigger, SQL Transactions, and advanced topics to help master SQL effectively. SQL (Structured Query Language) is a Query language used for managing and manipulating relational database
    15 min read
    Java Cheat Sheet
    Java is a programming language and platform that has been widely used since its development by James Gosling in 1982. It follows the Object-oriented Programming concept and can run programs written in any programming language. Java is a high-level, object-oriented, secure, robust, platform-independent, multithreaded, and portable programming langua
    15+ min read
    Subnet Mask Cheat Sheet
    A Subnet Mask is a numerical value that describes a computer or device's how to divide an IP address into two parts: the network portion and the host portion. The network element identifies the network to which the computer belongs and the host part identifies the unique computer on that network. An IP address is made up of four digits separated by
    9 min read
    JavaScript Cheat Sheet - A Basic Guide to JavaScript
    JavaScript is a lightweight, open, and cross-platform programming language. It is omnipresent in modern development and is used by programmers across the world to create dynamic and interactive web content like applications and browsers. It is one of the core technologies of the World Wide Web, alongside HTML and CSS, and the powerhouse behind the
    15+ min read
    Linux Commands Cheat Sheet
    Linux, often associated with being a complex operating system primarily used by developers, may not necessarily fit that description entirely. While it can initially appear challenging for beginners, once you immerse yourself in the Linux world, you may find it difficult to return to your previous Windows systems. The power of Linux commands in con
    13 min read
    Advance Git Sheet For Open Source Contributors
    Contributing to open-source projects is a fantastic way to enhance your coding skills, collaborate with a large community, and give back to the tech world. However, navigating Git can be challenging for many. This advanced Git cheat sheet aims to equip open-source contributors with essential commands and best practices to simplify their workflow, r
    4 min read
    Git - git-show Command Line Utility
    Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git relies on the basis of distributed development of software where more than one developer may have access to the source code of a specific application and can modify changes to it that may be
    3 min read
    Git LFS: Managing Large Files in Git Repositories
    Git, undoubtedly one of the most popular version control systems, is widely used for managing source code in software development projects. However, one of its limitations is its handling of large files. Traditional Git repositories struggle to efficiently manage large files, leading to bloated repositories and slow performance. This is where Git L
    4 min read
    Git Subtree vs. Git Submodule
    Git Subtree and Git Submodule are both mechanisms in Git that allow you to incorporate external repositories into your own repository. They provide a way to manage dependencies and include code from other projects while keeping your repository organized and maintainable. Git Subtree: Git Subtree allows you to insert a separate repository as a subdi
    4 min read
    Difference Between Git Fetch and Git Pull
    Understanding the difference between git fetch and git pull is important for effective version control in Git. These commands are important for managing your repository and collaborating with team members. In this article, Let us look at Git Fetch and Git Pull separately with the help of an example. What is Git Fetch?The Git Fetch command is used t
    4 min read
    Difference Between Git Push Origin and Git Push Origin Master
    Understanding the difference between git push origin and git push origin master is important for efficient version control in Git. These commands are used to upload changes from your local repository to a remote repository, but they function differently. This article will explain these differences, how each command works, and when to use them. What
    3 min read
    three90RightbarBannerImg