Gatlin.io Icon
Image of Austin Gatlin's face
March 15, 2022 (last updated March 24, 2022)

Git Cheatsheet

-h shows quick inline help, while --help shows the full man page. It will work with aliases as well.

git branch -h
g br -h

git help branch
git branch --help
g br --help

Branches

  • g bra, git branch --all

    List every branch

  • g br -dr b1 b2, git branch --delete --remote b1 b2

    Deletes multiple branches "safely", i.e., only fully merged branches. Often fails for certain workflows. g br -Dr b1 b2, or g br -drf b1 b2 will force the deletion. If you've already removed the remote-tracking branch, e.g., g remote prune origin, you will need to drop the -r, since git will not run the command if it fails to find the remote-tracking branch. So, g br -df b1 b2.

Remote-tracking branches

From the git docs on remote branches

Remote-tracking branches are references to the state of remote branches. They’re local references that you can’t move; Git moves them for you whenever you do any network communication, to make sure they accurately represent the state of the remote repository. Think of them as bookmarks, to remind you where the branches in your remote repositories were the last time you connected to them.

Keep in mind that remote-tracking branches are local references. This is why the git remote command doesn't actually do anything to the remote repository. What it does is interact with your local reference of the remote repository. For example, git remote prune origin gets rid of local references to stale remote branches.

Misc

  • git cat-file -p [sha]

    -p stands for "pretty print". Will print out the internal "value" associated with the SHA "key", the content of which depends on what the SHA refers to. For a commit it will show things like the tree, parent, author, commit message, etc. Trees contain blobs, and blobs contain file text, all of which can be recursively inspected with the same command. Run git help cat-file to learn more

  • git push [origin] --delete [branch_name]

    Delete the remote version of a branch. (Though I often feel safer doing it via the remote repo GUI.)

  • git remote prune origin or git remote update [origin] --prune

    Prune stale branches from your remote cache

  • git clean -fd

    Removes untracked files and directories from the working tree. I.e., it deletes the red stuff. -f is to force the clean. I think that they make you type it because it's a destructive action. -d is to recursively clean directories. Effectively, that means it deletes directories as well.

  • git rm --cached -r .

    --cached will rm only cached files, i.e., remove paths only from the index (i.e., cache) without messing with the working tree (i.e., your in progress work, aka the red stuff). -r will rm recursively. E.g., ... -r . will rm the current directory and files and all sub-directories and files.

Syntax

  • HEAD is the commit upon which you are basing your current changes in the working tree. (docs)
  • @ is an alias for HEAD
  • <rev> is a generic term that specifies a "revision". This is typically a commit SHA. Another type of revision is a refname, which points to a SHA. (docs)
  • <ref>, or <refname>, is a reference to a revision (or, <rev>). These are the most commonly used revisions, e.g., master, branch-name, HEAD, etc. (docs)
  • <ref>~<n>, e.g., HEAD~3, is syntactic sugar which refers to the 3rd (or nth) ancestor commit to HEAD. So, like, 3 commits back, not including the HEAD commit.