Git Cheat Sheet for Beginners

Git Cheat Sheet for Beginners

We can't remember every single Git command, no matter how hard we try. Remembering these commands is also unnecessary as we can easily search them on Google.

In this blog, we will briefly cover some of the most important Git commands. So, let us jump right into it.

Installing and Configuring Git

Before we begin, you must install Git on your system. You can follow the blogs below to learn more about installing Git on different operating systems:

After you have installed Git, you need to configure it. Here's how you do it:



git --version

Check Git Installation

git config --list --show-origin

View all your Git settings

git config --global "Your Name"

Set your Username 

git config --global

Set your Email

git config --global init.defaultBranch main

Change Default Branch Name

git config --global core.editor "'path to the code editor app'"

Change your Code Editor

Git Basics

After configuring Git, you can use different commands to perform many operations. Here are a few of them:

Initialize a repo

The first thing to do while using Git is initializing a repository with the git init command.

You can also initialize a directory to a certain folder on your computer using the following command:

git init <directory>

Example: git init C:\Program Files\Project

The git init command creates a hidden folder named .git/ in your project folder, which tracks changes to the files.

Add to the Staging Area

Git has a staging area, which is a temporary space where you add files you have made changes to before you commit those changes.

To add a file to the staging area, you can type the git add command. There are two versions of this command:



git add <file_names>

Adds certain files to the staging area. 

git add <directory>

Adds the entire directory to the staging area. 

Commit Changes

You can commit changes to your local repository once you have entered the files into the staging area.

Here's the Git commit command:

git commit -m <message>

where <message> can be any information about the commit.

Example: git commit -m "New feature added"

Status Commands

These commands are used to track changes, view folder history, and see the files in different stages. Here's how you use them:



git log

Display the entire commit history. 

git status

Show the status of files in the repository, i.e., if the files are staged or not. 

git diff

Show the difference between your staging area and the working directory.

Git Branches

Creating a new branch in Git is basically creating a new version of your file. As a developer, you need to be able to work with branches. Here are some commands for it:



git branch

List the branches in your repository.

git branch -r

Check remote branches that Git is tracking.

git branch branch_name 

Create a new branch.

git checkout branch_name

Switch to a new branch.

git checkout -b branch_name

Create a new branch and switch to it at once.

git branch -d branch_name

Delete a branch.

Git Merges

Git also allows you to merge different file versions, i.e., branches. Here are some Git merge commands:



git merge branch_name

Merge the branch you are currently working in. 

git merge origin/main

Merge remote repo with a local repo. 

git merge --abort

Abort a merge and start over. 

Connecting With Remote Repositories

The best part about Git is that we can connect it with a remote repository and share our files with others. Here are a few commands for this purpose:



git remote add <name> <url>

Create a new connection to a remote repo using its name and URL. 

git push -u origin branch_name

Push a new branch to the remote repo. 

git push --all  <remote> 

Push all of the changes in your local branches to the specified remote repository.

git push <remote> <branch> 

Push the changes and commits in the local repository to the main branch of the remote repository. 

git remote -v

See all the remote repositories for your local repository.

git push -f

Force a Push request. 

git fetch 

Fetch a specific branch from the repository.

git pull <remote> 

Get the remote copy of the current branch and merge it to the local repo. 

Reverting Changes

When you mistakenly perform a commit, you need to be able to revert the changes back. You might also need to remove files from the staging area.

Git allows you to do this with these commands:



git revert <commit>

Create a commit that undoes all the changes made. 

git reset <file> 

Remove files from the staging area.

git clean -n

Show the files that will be removed from the working directory.

git clean -f 

Delete files from the working directory. 

Miscellaneous Commands

Here are a few miscellaneous Git commands that you might need to use in some scenarios:



git tag [name] [commit sha]

Add a reference tag name to a commit id. 

git tag

List all tags.

git clone <repo>

Clone a repo located at <repo> onto local machine.

git rebase

Transfer completed work between branches. 

Frequently Asked Questions

1. What is a Git cheat sheet?

A Git cheat sheet is basically a list of important Git commands with short descriptions that you can use when you can't remember a command.

2. What are the basic Git commands?

Some of the most basic Git commands include:

  • git init
  • git add
  • git commit
  • git push
  • git pull
  • git merge.

3. How many Git commands are there?

It depends on the Git version you are using. But generally, there are 135 to 164 commands. But don't worry, you probably won't be using all of them. Also, most of these commands are sub-commands.  

4. How do I run a Git command?

After you have installed Git, you can run it with the Command Prompt in Windows and the terminal in Linux and Mac. Alternatively, you can use Git Bash with Windows.