Git Cheat Sheet (Free PDF)

May 03, 2023

Git Cheat Sheet (Free PDF)

Git is the free and open source distributed version control system that's responsible for everything GitHub related that happens locally on your computer. This cheat sheet features the most important and commonly used Git commands for easy reference.

You can get the PDF version of this cheat sheet for free here:

Download cheat sheet PDF

Account Setup

Commands for configuring user information used across all local repositories.


Set a name that is identifiable for credit when review version history

git config --global user.name "[firstname lastname]"

Set an email address that will be associated with each history marker

git config --global user.email "[valid-email]"

Repository Setup

Commands for initializing and cloning repositories


Add a git repository to an existing local folder

cd path-to-folder
git init

Retrieve an entire repository from a hosted location via URL

git clone [url]

Changes

Commands for working with local changes


Show modified files in working directory, staged for your next commit

git status

Select (stage) a file as for your next commit

git add [file]

Unstage a file from your next commit (while keeping the changes)

git reset [file]

Show the difference between staged and unstaged files

git diff

Show the difference between what is staged and what is not

git diff --staged

Commit your staged changes

git commit -m "[descriptive message]"

Branches

Commands for creating, changing and managing branches. Isolating work in branches, changing context, and integrating changes

List your branches. a * will appear next to the currently active branch

git branch

Create a new branch

git branch [branch-name]

Create and switch to a new branch

git checkout -b [branch-name]

Switch to another branch

git checkout [branch-name]

Merge the specified branch’s history into the current one

git merge [branch]

Show all commits in the current branch’s history

git log

Remote repositories

Commands for retrieving updates from another repository and updating local repos.

Add a remote

git remote add [alias] [url]

Fetch all the branches from that Git remote

git fetch [alias]

Update the current branch from its remote counterpart

git pull [alias]/[branch]

Transmit local branch commits to the remote branch

git push [alias] [branch]

Stashing

Commands for temporarily saving and applying code changes.


Save modified and staged changes

git stash

List the saved stash entries

git stash list

Apply the latest stash entry.

git stash pop

Discard the latest stash entry

git stash drop

Rewrite History

Commands for changing the history.


Rewriting branches, updating commits and clearing history

git rebase [branch]

Apply any commits of current branch ahead of specified one

git reset --hard [commit]

Clear staging area, rewrite working tree from specified commit

History

Commands for viewing the history.


Show the commit history for the currently active branch

git log

Show the commits on branchA that are not on branchB

git log branchB..branchA

Show the commits that changed file, even across renames

git log --follow [file]

Show the diff of what is in branchA that is not in branchB

git diff branchB...branchA

Show any object (branch, hash...) in Git in a human-readable format

git show [SHA]

Ignore patterns

Commands and patterns for ignoring files.


Prevent unintentionally staging or commiting files globally

git config --global core.excludesfile [file]

Prevent unintentionally staging or commiting files in a project by adding it to .gitignore

echo [file] >> .gitignore

Pattern examples

# ignore all files inside the folder node_modules
node_modules/
# ignore all files ending with txt
*.txt
# ignore all folders starting with pattern
pattern*/

PDF version

You can get the PDF version of this cheat sheet for free here:

Download cheat sheet PDF

Resources