Skip to content

Instantly share code, notes, and snippets.

@willis7
Last active May 30, 2017 07:38
Show Gist options
  • Save willis7/57ba16374f0c3c3d57a8dd22e66993a5 to your computer and use it in GitHub Desktop.
Save willis7/57ba16374f0c3c3d57a8dd22e66993a5 to your computer and use it in GitHub Desktop.
Git cheat sheet

Common Git Commands

Command Description
git remote -v list the remotes
git pull --rebase rebases your local changes on top of your teammates' without creating a merge commit
git rebase --interactive rewrites commits but gives you a chance to modify them as they are reapplied onto the new base

History

Command Description
git log --all --full-history -- **/thefile.* find a file that once existed
git show <SHA> -- <path-to-file> find the version of the file you want, and display
git checkout <SHA>^ -- <path-to-file> restore it into your working copy with
git log --oneline --no-merges HEAD..<remote>/<branch> check which changes you're about to pull
git log --oneline --no-merges <remote>/<branch>..HEAD check which changes you're about to push

Tips

Command Description
git pull --rebase origin master rebase local branch with remote master
git rebase master −−interactive −−exec=”npm test” would generate an interactive rebase plan that invokes npm test after rewriting each commit
git rebase master -x “npm test” non-interactive exec
git checkout feature/whatever && git difftool -y master... show the changes since the branch diverged from master but won't show the changes to master since the branch (ie it only shows the feature branch not changes made to master in the meantime)
git ls-files --stage show staged contents' object name, mode bits and stage number in the output
git update-index --chmod=+x foo.sh changes the index to reflect an executable flag on file foo.sh

Housekeeping

Command Description
git clean -fxd clean the repo back to the point you cloned

Aliases

  • st = status -s
  • lp = log --graph --pretty=format:'%Cred%h%Creset - %s %Cgreen(%cr) %C(yellow)%d%Creset %C(bold blue)<%an>%Creset' --abbrev-commit
  • ll = log -n 20 --pretty=format:'%Cred%h%Creset %<(55,trunc)%s %Cgreen(%<(7,trunc)%cr) %C(bold blue)<%<(5,trunc)%an>%Creset' --abbrev-commit
  • ahead = log --pretty=format:'%Cred%h%Creset %<(55,trunc)%s %Cgreen(%<(7,trunc)%cr) %C(bold blue)<%<(5,trunc)%an>%Creset' --abbrev-commit @{u}..
  • behind = log --pretty=format:'%Cred%h%Creset %<(55,trunc)%s %Cgreen(%<(7,trunc)%cr) %C(bold blue)<%<(5,trunc)%an>%Creset' --abbrev-commit ..@{u}
  • unstage = reset --
  • ff = pull --ff-only
  • review = rebase -i @{u}
  • co = checkout
  • b = "!git for-each-ref --sort='-authordate' --format='%(authordate)%09%(objectname:short)%09%(refname)' refs/heads | sed -e 's-refs/heads/--'" -- list branches sorted by last modified

Notes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment