Git cheatsheet

Various things I need to remember for Git, but that I don’t remember. I know I can read the help to get this, but I still have to wade through a bunch of verbiage.

Undo the last commit (--soft keeps changes):

$ git reset --soft HEAD~1
$ git reset --hard HEAD~1

Delete remote branches (Stackoverflow):

$ git push REMOTENAME --delete <branch-name>
$ git push REMOTENAME :<branch-name>
$ git remote prune origin

Show filenames in log messages (Stackoverflow)

$ git log --name-only

Give someone else the author credit:

$ git commit --author="Joe Cool <cool@example.com>" -m "Fix the thing"

Add a trailer:

$ git commit --trailer "Co-authored-by: Joe Cool <cool@example.com>" -m "Fix the thing"

Show all merged branches

$ git branch --merged master

See which .gitignore rules exclude a file (Stackoverflow)

$ git check-ignore -v **/*

Find the commit that deleted a file (git docs) (Stackoverflow)

$ git rev-list -n 1 HEAD -- <file_path>

Diff the same file across two branches:

$ git diff mybranch..master -- filename