Git Command Cheat Sheet: A Handy Guide

Git Command Cheat Sheet: A Handy Guide

This handy cheat sheet provides a quick reference to the most commonly used Git commands, from setting up your environment to managing branches and rewriting history.

SETUP

Configuring user information used across all local repositories.

DESCRIPTIONDESCRIPTION
Set a name that is easy to recognize for credit when reviewing version history.git config --global user.name “[firstname lastname]”
set an email address that will be linked to each history entrygit config --global user.email “[valid-email]”
set automatic command line coloring for Git for easy reviewinggit config --global color.ui auto

SETUP & INIT

Configuring user information, initializing and cloning repositories

DESCRIPTIONCOMMAND
Initialize an existing directory as a Git repositorygit init
Retrieve an entire repository from a hosted location via URLgit clone [url]

STAGE & SNAPSHOT

Working with snapshots and the Git staging area.

DESCRIPTIONCOMMAND
Show modified files in working directory, staged for your next commitgit status
Add a file as it looks now to your next commit (stage)git add [file]
Unstage a file while retaining the changes in working directorygit reset [file]
Diff of what is changed but not stagedgit diff
Diff of what is staged but not yet committedgit diff --staged
Commit your staged content as a new commit snapshotgit commit -m “[descriptive message]”

BRANCH & MERGE

Isolating work in branches, changing context, and integrating changes.

DESCRIPTIONCOMMAND
List your branches. A * will appear next to the currently active branch.git branch
Create a new branch at the current commit.git branch [branch-name]
Switch to another branch and check it out into your working directory.git checkout
Merge the specified branch’s history into the current one.git merge [branch]
Show all commits in the current branch’s history.git log

INSPECT & COMPARE

Examining logs, diffs and object information.

DESCRIPTIONCOMMAND
Show the commit history for the currently active branchgit log
Show the commits on branchA that are not on branchBgit log branchB..branchA
Show the commits that changed file, even across renamesgit log --follow [file]
Show the diff of what is in branchA that is not in branchBgit diff branchB...branchA
Show any object in Git in human-readable formatgit show [SHA]

SHARE & UPDATE

Retrieving updates from another repository and updating local repos.

DESCRIPTIONCOMMAND
Add a git URL as an aliasgit remote add [alias] [url]
Fetch down all the branches from that Git remotegit fetch [alias]
Merge a remote branch into your current branch to bring it up to dategit merge [alias]/[branch]
Transmit local branch commits to the remote repository branchgit push [alias] [branch]
Fetch and merge any commits from the tracking remote branchgit pull

TRACKING PATH CHANGES

Versioning file removes and path changes.

DESCRIPTIONCOMMAND
Delete the file from project and stage the removal for commit.git rm [file]
Change an existing file path and stage the move.git mv [existing-path] [new-path]
Show all commit logs with indication of any paths that moved.git log --stat -M

REWRITE HISTORY

Rewriting branches, updating commits and clearing history.

DESCRIPTIONCOMMAND
Apply any commits of current branch ahead of specified onegit rebase [branch]
Clear staging area, rewrite working tree from specified commitgit reset --hard [commit]

IGNORING PATTERNS

Preventing unintentional staging or committing of files.

DESCRIPTIONCOMMAND
Save a file with desired patterns as .gitignore with either direct string matches or wildcard globs.logs/ .notes pattern/
System wide ignore pattern for all local repositories.git config --global core.excludesfile [file]

TEMPORARY COMMITS

Temporarily store modified, tracked files in order to change branches.

DESCRIPTIONCOMMAND
Save modified and staged changes.git stash
List stack-order of stashed file changes.git stash list
Write working from top of stash stackgit stash pop
Discard the changes from top of stash stackgit stash drop