Git Cheatsheet

This is a git cheatsheet based on my personal usage, so your mileage may vary. I hope this is useful to others as well.

Git logo

I have struggled with many many clients over the years, and I somehow always return to the command line. I think the reason is that it’s much easier for me to thing at the basic level of git, instead of some abstraction offered by the various GUI clients out there.

That’s not to say I never use a GUI client. I do, especially when I want to stage hunks (i.e. parts of changes I made to files) instead of whole files. That’s something one can do from the command line, but it’s a bit cumbersome.

Table of contents


Here are my aliases:

  branch-name = "!git rev-parse --abbrev-ref HEAD"
  publish = "!git push -u origin $(git branch-name)"
  unpublish = "!git push origin :$(git branch-name)"
  ro = !git fetch origin && git reset --hard origin/master
  dc = diff --cached
  edit = !vim $(git ls-files -o -m --exclude-standard)
  commits = "!sh -c \"git log --left-right --graph --cherry-pick --oneline ${1:-master}...${2:-$(git branch-name)}\" -"
  files = "!sh -c \"git diff --name-only ${1:-master}...${2:-$(git branch-name)}\" -"
  cas = commit -aS
  cs = commit -S
  aliases = !git config --get-regexp 'alias.*' | colrm 1 6 | sed 's/[ ]/ = /'

Change older commit

You have created a topic branch, and added two commits:

$ git commits # not a standard git command (check the alias)
> 1480424 (HEAD -> petros/wizard-wars) Create a new wizard for additional organizations
> 3eec67f Add a path to the add payment method wizard step

If you wanted to change something you forgot in a file that was touched by 3eec67f, here’s how to do it:

$ git rebase -i '3eec67f'^

The caret ^ at the end of the command, means we need to rebase back to the commit before the one we wish to modify.

Make your changes (edit your files, or add new files), and then issue the following commands to wrap things up:

$ git commit --all --amend --no-edit
$ git rebase --continue

Notice this is rewriting history which means all the commit hashes will change. If you need to push to the remote branch (you previously pushed) you will need to git push -f (i.e. force push) which is a very bad idea, unless you know what you are doing. This tip here is perfect for when you haven’t pushed your changed to a remote branch yet.

What are you up to Petros?

Staying calm. Indie 2D retro style game dev wannabe. Check what I am doing now.