DEV Community

Cover image for Git Cheatsheet that will make you a master in Git
hichamelhirch
hichamelhirch

Posted on • Updated on

Git Cheatsheet that will make you a master in Git

Introduction to Git

Git is an essential version control system that enables developers to track changes and collaborate effectively on projects. It has become a crucial tool for managing code, whether you're working alone or with a team. However, learning Git can be daunting, especially for beginners unfamiliar with its commands and functionality. In this Git cheatsheet, we’ll explore key commands every developer should know, from setting up a repository to advanced tasks like branching and merging. This guide is designed to help both newcomers and seasoned developers enhance their Git skills and streamline their workflow, making Git easier and more efficient to use.

Basic Git commands

Initialization

To initialize a new Git repository in the current directory, run the following command:

Image description

This creates a hidden .git directory in the current directory that tracks changes to your code.


Cloning

To clone an existing Git repository to your local machine, run the following command:

Image description

This creates a new directory on your computer with a copy of the repository.


Staging changes

Before you commit changes to your code, you need to stage them using the git add command. This tells Git which changes you want to include in your commit.

To stage a file or directory, run the following command:

Image description

You can also stage all changes in the current directory by running:

Image description


Committing changes

To commit the changes in the staging area with a commit message, run the following command:

Image description

The commit message should briefly describe the changes you made in this commit.


Checking status

To check the current status of your repository, run the following command:

Image description

This will show you which files have been modified, which files are staged for commit, and which files are untracked.


Viewing changes

To view the changes between the working directory and the staging area, run the following command:

Image description

To view the changes between the staging area and the last commit, run the following command:

Image description


Branching

Git allows you to create multiple branches of your code, enabling you to work on different features or fixes without affecting the main codebase. The default branch in Git is called master.

To create a new branch with a specified name, run the following command:

Image description

To switch to the specified branch, run the following command:

Image description

You can also create and switch to a new branch in one command by running:

Image description

To merge the specified branch into the current branch, run the following command:

Image description


Pushing changes

To push changes to a remote repository, run the following command:

Image description

Here, <remote> is the name of the remote repository (commonly origin), and <branch> is the name of the branch you want to push.


Pulling changes

To pull changes from a remote repository, run the following command:

Image description

In this command, <remote> refers to the name of the remote repository (often origin), and <branch> is the name of the branch you want to pull.


Viewing history

To view the commit history, run the following command:

Image description

This will show you a list of all the commits in the repository, along with the commit message, author, and date.


Connect with Me


Happy coding! 💻👨‍💻👩‍💻

Top comments (0)