Git Cheat Sheet

Posted in /  

Git Cheat Sheet

Vinay Khatri
Last updated on August 1, 2022

    If you have ever worked in web development or software development, then you must be aware of Git. Most developers use Git because it is one of the best version control systems out there. The website GitHub uses git version control as its brain. Git provides the best way to manage your project, and it becomes more helpful when a proper team is working on a project. This article offers a comprehensive Git cheat sheet.

    Git comes with a standard Command Line Interface (CLI). This means that in order to work with Git, we need to write and execute commands. If we are using GitHub for the project then, rather than using Git commands , we can simply use GitHub buttons and other options to manage the project. However, it is not necessary that if we are using Git, we might also use GitHub. Thus, it's important to understand that Git and GitHub are two separate entities. Many developers find using a CLI intriguing, but for beginners, using a command-line tool for running Git commands can be a daunting task.

    Git contains hundreds of commands, and it is impossible to learn each of them. However, the good thing is that we don't need to do so. We should learn and practice only those commands that are common and serve our purpose. For the reason stated above, here, we have provided a Git cheat sheet that discusses the most-used Git commands. You can use this cheat sheet if you get stuck somewhere using Git or forget some commands.

    What is Git?

    Git is an open-source distributed version control system powering the GitHub Websites . It is the most famous and widely used version control system of this decade that has huge community support. It is a distributed VCS because, unlike other version control systems that store the project’s full version history in one place, Git allows each developer to have their own local repository with complete history changes.

    What is Version Control?

    A version control system is a tool that keeps track of your project's history. It helps to roll back to a previous version of the project in case of any error or failure.

    Git Terminologies

    Before proceeding with the Git cheat sheet, let’s take a look at the basic terminologies we use in Git:

    Terminology Description
    Branch A branch represents the various versions and repositories branching out from your main project. It helps us to keep track of the changes happening in the various repositories.
    Commit Commit is used to save some changes.
    Checkout When we switch between branches, we use the checkout command.
    Fetch Fetch commands can copy and download the branch file to your local device.
    Fork A fork is a copy of a repository.
    Head The commit at the tip of a branch is called the head. It represents the most current commit of the repository you’re currently working in.
    Index In order to save any changes made to a project, we need to commit them. Not all those changes will remain in the index for commit.
    Master It is the main branch of the repositories.
    Merge If one branch made any changes that we need to keep, we could use the merge command to make the same changes in all other repositories.
    Origin The default version of a repository.
    Pull It represents the suggestion for changes to the master branch.
    Push Push is used to update the remote branch if we have committed the changes.
    Repository It holds all the project’s files.
    Tag It tracks the important commits.

    Git Cheat Sheet

    Git Configuration

    Commands Description
    git config –-global user.name “User Name” Attaches the name User Name to the commits and tags.
    git config --global user.email “email address” This command attaches the email address to the commits and tags.
    git config --global color.ui  auto Enables some colorization in the Git interface.

    Git Setting Project

    Commands Description
    git init [project name] This command will create a new repository if the project name is provided. Else it will initialize git in the current directory.
    git clone [project url] It will create a clone of the project, which is at the remote repository of the project URL .

    Git Frequently Used Commands

    Commands Description
    git status Shows the status of the repository.
    git diff [File] Displays changes between the working directory and the staging area.
    git diff --stages [file] Shows changes between the staging area and the index.
    git checkout -- [file] Discards change in the working directory.
    git add [file] Adds a new file .
    git reset [file] Gets the file back from the staging area.
    git commit Saves the changes.
    git rm [file] Removes the file .
    git stash Puts the changes into the stash.

    Git Branching

    Commands Description
    git branch [-a] It will show all the branches of the local repository. Here, –a means all.
    git branch [ branch name ] Creates a new branch.
    git checkout [-b] [name] Switches to the name branch. If name is not a branch in the repository, then create a new branch.
    git merge [branch] Merges the branch .
    git branch –d [branch name] Removes the branch.

    Review Work

    Commands Description
    git log [-n count] Lists last n numbers of commit history.
    git log --oneline –graph --decorate Gives an overview along with reference label and history graph.
    git log ref. . Lists current branch commits.
    git reflog This Git command lists operations such as commits and checkout.

    Git Tag

    Commands Description
    git tag Shows all tags.
    git tag [name] [commit sha] Creates a tag reference name for the current commit.
    git tag –d [name] Removes the tag.

    Revert Changes

    Commands Description
    git reset [--hard] [target reference] Switches the current branch to the target reference and leaves a difference as an uncommitted change.
    git revert [commit sha] Creates a new commit, reverting changes from the specified commit.

    Synchronizing Repositories

    Commands Description
    git fetch [remote] Fetches the changes from remote .
    git pull [remote] Fetches the changes and merges the current branch with upstream.
    git push [remote] Pushes all the changes to the remote upstream.
    git push –u [remote] [branch] Pushes the local branch to the remote upstream.

    Conclusion

    Git has hundreds of commands, and learning each of them is impossible and impractical. Therefore, we only use those Git commands that are basic and necessary from our project's POV. All Git commands that we have provided in this article are the most important git commands, and every interviewer expects you to know at least these commands. If you like this article or have any suggestions related to the git cheat sheet, please let us know by adding comments below.

    People are also reading

    Leave a Comment on this Post

    0 Comments