DEV Community

Cover image for Git Cheat Sheet 馃搫 (50 commands + PDF and poster)
Danny Adams
Danny Adams

Posted on • Updated on

Git Cheat Sheet 馃搫 (50 commands + PDF and poster)

I was tired of looking up the same common Git commands - so I made a cheat sheet that I could print and put on my office wall.

This cheat sheet contains 50 commonly used Git commands on the following topics:

  • Setting up Git
  • Starting a project
  • Making a change
  • Basic concepts
  • Branching
  • Merging
  • Rebasing
  • Undoing things
  • Reviewing your repo
  • Stashing
  • Synchronising local and remote repositories

Git Commands Cheat Sheet PDF

One page PDF to make it easy to copy and paste in commands.

Download the Git Commands Cheat Sheet PDF here

Both PDF and poster are available in Light Mode and Dark Mode:

Git cheat sheet poster in light mode

Git cheat sheet poster in dark mode

Git Cheat Sheet Poster

Order a physical A3 poster for your office wall - so you can quickly look up commands, and keep them at the top of your head.

It comes in thick durable paper, and a matte, light-absorbing finish.

Order a Git Cheat Sheet Poster here

Here's mine on my office wall:

The Git cheat sheet poster on my office wall

Here are all of the commands from the cheat sheet:

Setup

Set the name and email that will be attached to your commits and tags

$ git config --global user.name "Danny Adams"

$ git config --global user.email "myemail@gmail.com"
Enter fullscreen mode Exit fullscreen mode

Starting a Project with Git

Create a local repo (omit <directory> to initialise the current directory as a git repo)

$ git init <directory>
Enter fullscreen mode Exit fullscreen mode

Download a remote repo

$ git clone <url>
Enter fullscreen mode Exit fullscreen mode

Make a Change

Add a file to staging

$ git add <file>
Enter fullscreen mode Exit fullscreen mode

Stage all files

$ git add .
Enter fullscreen mode Exit fullscreen mode

Commit all staged files to git

$ git commit -m "commit message"
Enter fullscreen mode Exit fullscreen mode

Add all changes made to tracked files & commit

$ git commit -am "commit message"
Enter fullscreen mode Exit fullscreen mode

Basic Git Concepts

main: default development branch
origin: default upstream repo
HEAD: current branch
HEAD^: parent of HEAD
HEAD~4: great-great grandparent of HEAD

Branches

List all local branches. Add -r flag to show all remote branches. -a flag for all branches.

$ git branch
Enter fullscreen mode Exit fullscreen mode

Create a new branch

$ git branch <new-branch>
Enter fullscreen mode Exit fullscreen mode

Switch to a branch & update the working directory

$ git checkout <branch>
Enter fullscreen mode Exit fullscreen mode

Create a new branch and switch to it

$ git checkout -b <newbranch>
Enter fullscreen mode Exit fullscreen mode

Delete a merged branch

$ git branch -d <branch>
Enter fullscreen mode Exit fullscreen mode

Delete a branch, whether merged or
not

$ git branch -D <branch>
Enter fullscreen mode Exit fullscreen mode

Add a tag to current commit (often used for new version releases)

$ git tag <tag-name>
Enter fullscreen mode Exit fullscreen mode

Merging

Merge branch a into branch b. Add --no-ff option for no-fast-forward merge

fast-forward vs no-fast-forward merge in Git

$ git checkout b

$ git merge a
Enter fullscreen mode Exit fullscreen mode

Merge & squash all commits into one new commit

$ git merge --squash a
Enter fullscreen mode Exit fullscreen mode

Rebasing

Rebase feature branch onto main (to incorporate new changes made to main). Prevents unnecessary merge commits into feature, keeping history clean

Rebasing feature onto main in Git

$ git checkout feature

$ git rebase main
Enter fullscreen mode Exit fullscreen mode

Interactively clean up a branches commits before rebasing onto main

$ git rebase -i main
Enter fullscreen mode Exit fullscreen mode

Interactively rebase the last 3 commits on current branch

$ git rebase -i Head~3
Enter fullscreen mode Exit fullscreen mode

Undoing Things

Move (&/or rename) a file & stage move

$ git mv <existing_path> <new_path>
Enter fullscreen mode Exit fullscreen mode

Remove a file from working directory & staging area, then stage the removal

$ git rm <file>
Enter fullscreen mode Exit fullscreen mode

Remove from staging area only

$ git rm --cached <file>
Enter fullscreen mode Exit fullscreen mode

View a previous commit (READ only)

$ git checkout <commit_ID>
Enter fullscreen mode Exit fullscreen mode

Create a new commit, reverting the changes from a specified commit

$ git revert <commit_ID>
Enter fullscreen mode Exit fullscreen mode

Go back to a previous commit & delete all commits ahead of it (revert is safer). Add --hard flag to also delete workspace changes (BE VERY CAREFUL)

$ git reset <commit_ID>
Enter fullscreen mode Exit fullscreen mode

Review your Repo

List new or modified files not yet committed

$ git status
Enter fullscreen mode Exit fullscreen mode

List commit history, with respective IDs

$ git log --oneline
Enter fullscreen mode Exit fullscreen mode

Show changes to unstaged files. For changes to staged files, add --cached option

$ git diff
Enter fullscreen mode Exit fullscreen mode

Show changes between two commits

$ git diff commit1_ID commit2_ID
Enter fullscreen mode Exit fullscreen mode

Stashing

Store modified & staged changes. To include untracked files, add -u flag. For untracked & ignored files, add -a flag.

$ git stash
Enter fullscreen mode Exit fullscreen mode

As above, but add a comment.

$ git stash save "comment"
Enter fullscreen mode Exit fullscreen mode

Partial stash. Stash just a single file, a collection of files, or individual changes from within files

$ git stash -p
Enter fullscreen mode Exit fullscreen mode

List all stashes

$ git stash list
Enter fullscreen mode Exit fullscreen mode

Re-apply the stash without deleting it

$ git stash apply
Enter fullscreen mode Exit fullscreen mode

Re-apply the stash at index 2, then delete it from the stash list. Omit stash@{n} to pop the most recent stash.

$ git stash pop stash@{2}
Enter fullscreen mode Exit fullscreen mode

Show the diff summary of stash 1. Pass the -p flag to see the full diff.

$ git stash show stash@{1}
Enter fullscreen mode Exit fullscreen mode

Delete stash at index 1. Omit stash@{n} to delete last stash made

$ git stash drop stash@{1}
Enter fullscreen mode Exit fullscreen mode

Delete all stashes

$ git stash clear
Enter fullscreen mode Exit fullscreen mode

Synchronizing

Add a remote repo

$ git remote add <alias> <url>
Enter fullscreen mode Exit fullscreen mode

View all remote connections. Add -v flag to view urls.

$ git remote
Enter fullscreen mode Exit fullscreen mode

Remove a connection

$ git remote remove <alias>
Enter fullscreen mode Exit fullscreen mode

Rename a connection

$ git remote rename <old> <new>
Enter fullscreen mode Exit fullscreen mode

Fetch all branches from remote repo (no merge)

$ git fetch <alias>
Enter fullscreen mode Exit fullscreen mode

Fetch a specific branch

$ git fetch <alias> <branch>
Enter fullscreen mode Exit fullscreen mode

Fetch the remote repo's copy of the current branch, then merge

$ git pull
Enter fullscreen mode Exit fullscreen mode

Move (rebase) your local changes onto the top of new changes made to the remote repo (for clean, linear history)

$ git pull --rebase <alias>
Enter fullscreen mode Exit fullscreen mode

Upload local content to remote repo

$ git push <alias>
Enter fullscreen mode Exit fullscreen mode

Upload to a branch (can then pull request)

$ git push <alias> <branch>
Enter fullscreen mode Exit fullscreen mode

Thanks for reading

Hope this cheat sheet is useful!

Again, feel free to download the one-page PDF or order a poster:
One-page Git commands cheat sheet PDF
Order a physical poster

For more from me, you can follow me on Twitter.

Cheers!

Discussion (26)

Collapse
friendbear profile image
T Kumagai

Beautiful.

Collapse
doabledanny profile image
Danny Adams Author

Thank you!

Collapse
kumar_sons_off profile image
Priyanshu

This is very handy cheatsheet, thank for sharing Danny 馃檶

Collapse
doabledanny profile image
Danny Adams Author

You're welcome!

Collapse
fourwhitesocks profile image
fourwhitesocks

Luv it I just bought the white one :) !!

Collapse
doabledanny profile image
Danny Adams Author

Awesome, thank you!!

Collapse
cess11 profile image
PNS11

Looks good but I think I'll stick to the man pages, they're quite good.

Besides 'man git' giving a high level overview suitable for topic search it also lists these learning resources:
gittutorial(7), gittutorial-2(7), giteveryday(7), gitcvs-migration(7), gitglossary(7), gitcore-tutorial(7), gitcli(7), The Git User鈥檚 Manual[1], gitworkflows(7)

Collapse
doabledanny profile image
Danny Adams Author

Thanks! Yeah the man pages are great, but my aim was to keep this cheat sheet as the one-page with commands that I most commonly use so it wasn't too overwhelming.

Collapse
robinglory profile image
Robin Glory

Damm!!
Life will be so much easier with this cheat sheet!1

Collapse
doabledanny profile image
Danny Adams Author

Cheers Robin!

Collapse
cyberjack profile image
CyberJack

And yet another git article where the "switch" and "restore" commands are missing.

The checkout command has multiple responsibilities, which isn't a good thing. So 2 new commands where created. The "switch" command can be used to create and/or switch to a branch, while the "restore" command can be used to restore working tree files.

Collapse
doabledanny profile image
Danny Adams Author

Thanks for the suggestions - I probably should've included "switch", but just stuck with "checkout" as it's what I always use.

Collapse
kaumnen profile image
kaumnen

this is handy, good job!

Collapse
doabledanny profile image
Danny Adams Author

Glad it's useful, thank you!

Collapse
sairamnagothu profile image
SairamNagothu

Awesome work 馃憦

Collapse
doabledanny profile image
Danny Adams Author

Thank you!!

Collapse
kg07lema profile image
kennylema

good job bro

Collapse
doabledanny profile image
Danny Adams Author

Thank you!

Collapse
smeetsmeister profile image
Jelle Smeets

Great informational post. Thanks for sharing Danny!

Collapse
doabledanny profile image
Danny Adams Author

Glad to hear it's helpful, thanks Jelle!

Collapse
chandrasekhar7 profile image
chandrasekhar7

Thank you 鉂わ笍

Collapse
joset98 profile image
joset98

thank you dude, you are so crack

Collapse
doabledanny profile image
Danny Adams Author

Thank you!

Collapse
oumaymasghayer profile image
Oumayma JavaScript Developer

This is helpful, thank you 馃槉

Collapse
doabledanny profile image
Danny Adams Author

That's great to hear, thank you!