Git

Git Commands Cheat Sheet

Introduction Git commands are an essential lesson that every developer needs to master at some point.

Introduction

Git commands are an essential lesson that every developer needs to master at some point. To use the full potential of Git, the popular version control system, you need to know how to use Git commands.

In this tutorial, you will find all the commonly used Git commands as well as a downloadable cheat sheet.

A List of Git Commands

Git Setup

Create a new Git repository from an existing directory:

git init [directory]
Code

Clone a repository (local or remote via HTTP/SSH):

git clone [repo / URL]
Code

Clone a repository into a specified folder on your local machine:

git clone [repo / URL] [folder]
Code
Git Configuration

Attach an author name to all commits that will appear in the version history:

git config --global user.name "[your_name]"
Code

Attach an email address to all commits by the current user:

git config --global user.email "[email_address]"
Code

Apply Git’s automatic command line coloring which helps you keep track and revise repository changes:

git config --global color.ui auto
Code

Create a shortcut (alias) for a Git command:

git config --global alias.[alias_name] [git_command]
Code

Note: Git requires you to type out the entire command to perform actions. Setting shortcuts for commonly used commands can speed up and simplify development. For example, you can use the alias st for the status command by typing the command: git config --global alias.st status

Set a default text editor:

git config --system core.editor [text_editor]
Code

Open Git’s global configuration file:

git config --global --edit
Code
Managing Files

Show the state of the current directory (list staged, unstaged, and untracked files):

git status
Code

List the commit history of the current branch:

git log
Code

List all commits from all branches:

git log --all
Code

Compare two branches by showing which commits from the first branch are missing from the second branch:

git log [branch1]..[branch2]
Code

Examine the difference between the working directory and the index:

git diff
Code

Explore the difference between the last commit and the index:

get diff --cached
Code

See the difference between the last commit and the working directory:

get diff HEAD
Code

Display the content and metadata of an object (blob, tree, tag or commit):

git show [object]
Code
Git Branches

List all branches in the repository:

git branch
Code

List all remote branches:

git branch -aa
Code

Create a new branch under a specified name:

git branch [branch]
Code

Switch to a branch under a specified name (if it doesn’t exist, a new one will be created):

git checkout [branch]
Code

Note: For a more detailed tutorial on working with Git branches, you can refer to our articles.Git Documentation

Delete a local branch

git branch -d [branch]
Code

Rename a branch you are currently working in:

git branch -m [new_branch_name]
Code

Merge the specified branch with the current branch:

git merge [branch]
Code
Making Changes

Stage changes for the next commit:

git add [file/directory]
Code

Stage everything in the directory for an initial commit:

git add .
Code

Commit staged snapshots in the version history with a descriptive message included in the command:

git commit -m "[descriptive_message]"
Code
Undoing Changes

Undo changes in a file or directory and create a new commit with the git revert command:

git revert [file/directory]
Code

Unstage a file without overwriting changes:

git reset [file]
Code

Undo any changes introduced after the specified commit:

git reset [commit]
Code

Show untracked files which will be removed when you run **git clean** (do a dry run):

git clean -n
Code

Remove untracked files:

git clean -f
Code
Rewriting History

Replace the last commit with a combination of the staged changes and the last commit combined:

git commit --amend
Code

Rebase the current branch with the specified base (it can be a branch name, tag, reference to a HEAD, or a commit ID):

git rebase [base]
Code

List changes made to the HEAD of the local repository:

git reflog
Code
Remote Repositories

Show remote repository

git remote show [remote_repo]
Code

Example:

git remote show origin
Code

Remove connection to a remote repository

git remote rm [remote_repo]
Code

Example:

git remote rm origin
Code

Create a new connection to a remote repository (give it a name to serve as a shortcut to the URL):

git remote add [remote_repo] [URL]
Code

Example:

git remote add origin [URL]
Code

Fetch a branch from a remote repository:

git fetch [remote_repo] [branch]
Code

Fetch a repository and merge it with the local copy:

git pull [remote_repo]
Code

Push a branch to a remote repository with all its commits and objects:

git push [remote_repo] [branch]
Code