Git Cheat Sheet 📄 (50 commands + PDF and poster)
Tags: git, github, programming, productivity
Published: 2022-01-20 18:49:47

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](https://doabledanny.gumroad.com/l/git-commands-cheat-sheet-pdf)

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

![Git cheat sheet poster in light mode](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n082uxea33j6zq3mca7u.png)

![Git cheat sheet poster in dark mode](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8cl1lotlaikqp8vhhr6i.png)

## 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](https://doabledanny.gumroad.com/l/git-cheat-sheet-poster)

Here's mine on my office wall:

![The Git cheat sheet poster on my office wall](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/60mjbucqvp3ojsq0fowy.jpg)

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

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

$ git config --global user.email "myemail@gmail.com"
```

## Starting a Project with Git
Create a local repo (omit `<directory>` to initialise the current directory as a git repo)

```bash
$ git init <directory>
```

Download a remote repo

```bash
$ git clone <url>
```

## Make a Change

Add a file to staging

```bash
$ git add <file>
```

Stage all files

```bash
$ git add .
```

Commit all staged files to git

```bash
$ git commit -m "commit message"
```

Add all changes made to tracked files & commit

```bash
$ git commit -am "commit message"
```

## 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.

```bash
$ git branch
```

Create a new branch

```bash
$ git branch <new-branch>
```

Switch to a branch & update the working directory

```bash
$ git checkout <branch>
```

Create a new branch and switch to it

```bash
$ git checkout -b <newbranch>
```

Delete a merged branch

```bash
$ git branch -d <branch>
```

Delete a branch, whether merged or
not

```bash
$ git branch -D <branch>
```

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

```bash
$ git tag <tag-name>
```

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2zpi27oxqws7j39iaxs2.PNG)

```bash
$ git checkout b

$ git merge a
```

Merge & squash all commits into one new commit

```bash
$ git merge --squash a
```

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/99lln9qgsiiwz8w18vdp.PNG)

```bash
$ git checkout feature

$ git rebase main
```

Interactively clean up a branches commits before rebasing onto main

```bash
$ git rebase -i main
```

Interactively rebase the last 3 commits on current branch

```bash
$ git rebase -i Head~3
```

## Undoing Things

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

```bash
$ git mv <existing_path> <new_path>
```

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

```bash
$ git rm <file>
```

Remove from staging area only

```bash
$ git rm --cached <file>
```

View a previous commit (READ only)

```bash
$ git checkout <commit_ID>
```

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

```bash
$ git revert <commit_ID>
```

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)

```bash
$ git reset <commit_ID>
```

## Review your Repo

List new or modified files not yet committed

```bash
$ git status
```

List commit history, with respective IDs

```bash
$ git log --oneline
```

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

```bash
$ git diff
```

Show changes between two commits

```bash
$ git diff commit1_ID commit2_ID
```

## Stashing

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

```bash
$ git stash
```

As above, but add a comment.

```bash
$ git stash save "comment"
```

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

```bash
$ git stash -p
```

List all stashes

```bash
$ git stash list
```

Re-apply the stash without deleting it

```bash
$ git stash apply
```

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

```bash
$ git stash pop stash@{2}
```

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

```bash
$ git stash show stash@{1}
```

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

```bash
$ git stash drop stash@{1}
```

Delete all stashes

```bash
$ git stash clear
```

## Synchronizing

Add a remote repo

```bash
$ git remote add <alias> <url>
```

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

```bash
$ git remote
```

Remove a connection

```bash
$ git remote remove <alias>
```

Rename a connection

```bash
$ git remote rename <old> <new>
```

Fetch all branches from remote repo (no merge)

```bash
$ git fetch <alias>
```

Fetch a specific branch

```bash
$ git fetch <alias> <branch>
```

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

```bash
$ git pull
```

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

```bash
$ git pull --rebase <alias>
```

Upload local content to remote repo

```bash
$ git push <alias>
```

Upload to a branch (can then pull request)

```bash
$ git push <alias> <branch>
```

## 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](https://doabledanny.gumroad.com/l/git-commands-cheat-sheet-pdf)
[Order a physical poster](https://doabledanny.gumroad.com/l/git-cheat-sheet-poster)

For more from me, you can [follow me on Twitter](https://twitter.com/DoableDanny).

Cheers!