Oscar the owl and Vershd logo

Git cheat sheet

Conquer Git, so you can get back to coding

A Git Cheat Sheet to Master Git

Mastering Git is no easy task, but this comprehensive beginner’s Git cheat sheet will certainly help you along the way. We’ve put together all the most commonly used Git commands (and what they do!), to help you get started right away.

You’ll quickly remember the basic commands like add and commit, but whenever you get stuck, just refer to this helpful cheat.

It’s a beginner’s guide and a quick reference sheet for all the usual git commands – plus a few you’ll be grateful for when you need them.

We’ll walk through the basics step by step, or you can download the full Git cheat sheet below.

Understanding the Rule of Three in Git

Before you jump into using Git, you’ll need to make sure you’re clear on the three major sections of a Git project, and the workflow between them.

  1. Your working directory
  2. Your staging area (aka the index)
  3. The Git repository

With most projects where you’re working with other developers, the third section is likely to have both a local repository and a remote repository.

This diagram highlights the workflow of Git:

Git cheat sheet - workflow

The Git Cheat Sheet

What follows is a list of the commands that you can see after downloading our free Git cheat sheet.

NB: Where you see a parameter within square brackets, it means it is optional. If there are two or more parameters within square brackets, separated by a pipe, it means that they are synonymous. So: [x|y] means that x and y are the same command.

How to Set Up Git

For a quick start beginner’s guide to setting up a new Git project, you’ll need one of two commands. You’ll either want to set the current directory as a Git repository, or you’ll want to clone an existing repository from a local or remote source.
git init
Initialises the current directory as a new Git repository.
git clone <source-path>
Clones a local repository to the current directory.
git clone <source-url>
Clones an existing repository from the hosted location to the current directory.

How to Configure Git

Next, you’ll want to set up and configure a few essential details. These Git configure shortcuts will help make your workflow clear and simple, adding your user details and setting preferences. There’s even a simple command to make reading lines in Git a lot easier.
git config --global user.name "<your-name>"
Sets the name for the user when making commits.
git config --global user.email "<your-email>"
Sets the email address for the user when making commits.
git config --global color.ui auto
Sets automatic colouring on command lines (and makes reviewing easier!)
git config --system core.editor <text-editor-path>
Sets your preferred text editor.
git config (--global | --system | --local) --edit
Opens one of Git’s configuration files: either the global, system, or local one.

How to Add Files to Git

With the essentials configured, you can start adding files. There are several different ways to add to Git, depending on if you want to add single or multiple files and what types of updates you want to include. Our Git cheat sheet explains the different options.
git add (<file-name> | <directory-name>)
Text that describes the sample code.
git add .
Adds all new, modified and deleted files in the current folder.
git add –A
Adds all new, modified and deleted files in all directories in the repository.
git add –u
Updates the staging area to match every directories modified and deleted files, but doesn’t add new files.

How to Commit & Tag in Git

Once you’ve added all the new files you need and made the necessary changes, you need to commit those changes from staging (aka the index) to your local repository.
git commit –m "message"
Commits all staged content.
git commit -a
Stages any modified or delete files, and then commits all files that are staged.
git commit --amend
Replaces the last commit with changes that you later supply.
git tag
Lists all the tags in the repository.
git tag <tag-name>
Tags the head commit with the supplied tag-name.

How to Connect, Push & Pull in Git

After you’ve committed to your local repo, you’ll need to push or pull commits, branches and tags to and from the remote repo. Make sure it’s connected first, and add an alias for it, to make future commands much easier.
git remote add <remote-alias> <url>
Adds an alias to the new remote repository at <url>
git push <remote-alias>
Pushes the current branch to the remote repository with all commits and objects.
git push  --all <remote-alias>
Pushes all local branches to remote repository.
git fetch <remote-alias> <branch-name>
Fetches a specific branch, without changing your local files.
git pull <remote-alias>
Fetches all branches, and merges changes into the local files.

Get the Full Git Cheat Sheet

To get all of these commands and more, just download and print off this cheat sheet, so that whenever you get stuck you can simply refer to the relevant command.

The Quickest & Easiest Solution

Do you need a Git GUI that's totally free for any use? GitBreeze boosts your software development productivity by making Git simple with a unique UI/UX. It's a Git GUI that prevents errors and creates clarity. This Git client soars over your repository and then swoops down to quickly tackle branches, commits and files. It deals with Git, so you can concentrate on coding.

Get screenshots and a 60 second video of what GitBreeze, the effortless Git GUI can do for you.
Oscar the owl and Vershd logo
 © 2024 GitBreeze Ltd.
St. George's flag of England, the Union Jack flag of the United Kingdom, the European Union flag, and the United Nations flag.
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram