Git Cheat Sheet

#tech - 2 Minute Read

lines of code on a computer screen

Workflow

See the repository status

git status

Add all files to staging

git add .

Commit with a message

git commit -m "<message>"

Edit the last commit message

git commit --amend -m "<message>"

Push the current state of the branch

git push origin master

Branches

Create a new branch

git checkout -b <branch>

Create a new branch from a previous commit

git checkout -b <branch> <hash>

Check out the previous branch

git checkout -

Check out a remote branch

git checkout -b <branch> origin/<remote_branch>

Delete a branch

git branch -D <branch>

Diffs

List the latest changes

git diff

List the latest staged changes (ignoring whitespace)

git diff --staged --ignore-space-at-eol

List the different commits between branches

git log --oneline <from_branch>..<to_branch>

List the different files between branches

git diff --name-status <from_branch>..<to_branch>

Stashing

Create a new stash

git stash

List stashes

git stash list

Apply the last stash

git stash apply

Undo the last stash apply

git stash show -p | git apply -R

Undoing

Revert everything

git checkout .

Revert a file

git checkout <filename>

Revert a file (if the filename is the same as a branch name)

git checkout -- <filename>

Digging

Read all about a commit

git show <hash>

Grab a file from another branch

git checkout <branch> -- <filename>

Find the common ancestor of two branches

git merge-base <branch_1> <branch_2>

List the branches that contain a commit

git branch -a --contains <hash>

List users and their commit counts, since a given date

git shortlog -cens --since "Jan 1 2019"

Show the entire history graph

git log --graph --oneline --all

Rebasing

Reorganize commits ahead of the specified branch

git rebase -i <base_branch>

Reorganize the last 5 commits

git rebase -i HEAD~5

Tagging

Create a new tag

git tag -a v#.#.# -m "<note>"

List tags with annotations

git tag -n

Push tags

git push --tags

Cleanup

List removable untracked files

git clean -dn

Remove untracked files

git clean -df

Configuration

Initialize the current directory as a git repository

git init

List all the current configuration settings

git config --list

Updated May 22, 2020.