Technology Solutions for Everyday Folks

My Personal Git Cheat Sheet

I've used a number of various Git cheatsheets over the years, usually duing a moment of "how do I do __ again?" and sometimes during a moment of panic like "Shit! Undo that commit!"

Recently, I (finally) "removed my training wheels" and uninstalled the UI client I had for Git, for two reasons:

  1. I was not actually using its features anymore*; and
  2. It was constantly getting in the way of Git updates.

* Save for the occasional git clone action

The combination of features now more or less baked into VSCode (plus a few useful extensions) and my consistent terminal (WSL or Git bash) use marked the death knell for the UI tool. The only thing I miss from the tool is a visual identifier in my Repos folder. As I have a very small number of "parent" folders containing repo clones, it's helpful to be able to see if any of them contain uncommitted changes or the like. However, in the scheme of things, this is a pretty minor "nice to have" deal.

For Future Me's sake, below are the Git commands I most frequently use or for which I forget their syntax. I have not included links to documentation or explanations of the commands.

Cloning and Branching

  • git clone https://github.com/username/reponame.git

Create empty branch (e.g. gh-pages)

  1. git checkout --orphan empty-branch
  2. git rm -rf .
  3. git commit --allow-empty -m "root commit"

Normal Branching

  • git branch branchname
  • git checkout [-b] branchname
  • git branch -al

Modifying or Renaming Branches

Add remote (e.g. point local repo to freshly-minted GitHub repo)

  1. git remote add origin https://github.com/username/reponame.git
  2. git remote -v

Rename branch from master to main (starting locally)

  1. git branch -m master main
  2. Push change to GitHub
  3. git push -u origin main
  4. Delete branch in GitHub, then
  5. git fetch --prune

Switching Between SSH and HTTPS

Sometimes, depending on environments, I might have cloned something with HTTPS and need to change to SSH (for using key authentication, for example) or vice-versa:

  • git remote -v
  • git remote set-url origin git@github.com:username/reponame.git (change to SSH)
  • git remote set-url origin https://github.com/username/reponame.git (change to HTTPS)

Committing and Undoing

Undo last commit (ideally before push):

  • git reset --soft HEAD~1

Normal commit & push:

  • git commit
  • git push

Synchronizing and Updating

  • git status
  • git push
  • git pull [--prune]
  • git fetch [--prune]

Updating Git for Windows

  • git update-git-for-windows

Far From Complete

This is my own personal short list and is far from complete. Most of these commands are now muscle-memory based, but having a reference to my commonly-used commands will hopefully make Future Me's life better.