[2020-04-03] Git Cheatsheet

This cheatsheet is based on the excellent video here https://www.youtube.com/watch?v=jtEthlTz1Q0

Terminology

  • Working Tree (aka Working Directory) — files that you are currently working on
  • Index (aka Staging Area) — staged files to be committed later
  • HEAD — current branch or last committed state on current branch

Basics

git init
git status
git add file1 file 2 # adds or updates files from the working tree into your index
git commit -m "Add new files"
git commit -a # stages changes and commits the current contents of you working tree in one step

Reset

  • Soft: move HEAD
  • Mixed (default): move HEAD and update the index
  • Hard (can cause data loss!): move HEAD, update the index and Working Tree
git reset --soft HEAD~1
git reset HEAD~1
git reset --hard HEAD~1

Stash

git stash -u
git stash save -u 'Description'
git stash list
git stash show
git stash show stash@{0}
git stash pop
git stash apply
git stash branch new-branch-name stash@{0}

Reflog

git log
git reflog
git reset --hard commit_hash
get checkout -b new-branch-name commit_hash
git reflog show feature-ranch
git reflog show master@{1.week.ago}

Interactive rebase

git rebase -i HEAD~8

Blame

git blame lib/index.js
git show commit_hash

Aliases

# Soft reset: git undo
git config --global alias.undo 'reset --soft HEAD~1'

# Mixed reset: git unstage [file1]
git config --global alias.unstage 'reset HEAD --'
Edit on GithubPublished on 2020-04-03

Copyright © 2019...2020 Nick S. Plekhanov