Git notes

Here are some funky Git commands I use all the time yet I never remember what they look like.

If you’re trouble shooting Git LFS related issues my Git LFS notes might be helpful.

Config

List Git configuration

git config -l --show-origin

Change the author of a Git commit

git commit --amend --author="Your Name <your.name@email.com>" --no-edit

Set Global Git user details

git config --global user.name "Your Name"
git config --global user.email your.name@email.com

Rebasing

Interactively rebase the first commit

git rebase -i --root

Undo a rebase

git reset --hard ORIG_HEAD

Tags

List tags

git tag -l

Create new tag

git tag v1.0

Create & check out a new branch off a tag

git checkout -b v1.0 tags/v1.0

Delete tag

git tag -d v1.0
git push origin :refs/tags/v1.0

Tidy up

Stop tracking and remove a file

git rm --cached -r package-lock.json; git add .; git status; git commit -m "Ignore unwanted files"

Prune remote branches on local

git remote prune origin

Garbage collection

Sometimes Git gets confused and shows messages, such as “unable to update local ref”

git gc --prune=now

Shortcuts

Switch back to previous branch

git checkout -

Create & check out a new branch off a specific commit

git checkout -b my-new-branch a7df14c7e1ebabe05015d7e462b9230e859cdbac

Summarise changes between feature branch (latest commit) and main (HEAD)

git diff --name-status origin/main...HEAD | cut -f1 | sort | uniq -c

Life saver

Raise a branch from the dead

How to restore a deleted branch: https://confluence.atlassian.com/bbkb/how-to-restore-a-deleted-branch-765757540.html

Singing commits

Signing commits with Git is a good thing as it shows other devs that your commits come from a trusted source.

A screenshot of a list of commits on Github showing the "verified" label

Install GPG CLI

brew install gpg2

Generate new GPG key

gpg --full-generate-key

List GPG keys

gpg --list-secret-keys --keyid-format=long

Export a GPG key

gpg --armor --export <key>

Set up signing every commit globally

git config --global user.signingkey <Key from your list>
git config --global gpg.program $(which gpg)
git config --global commit.gpgsign true
export GPG_TTY=$(tty)

If for whatever reason GPG signing stops working, it’s probably caused by Shell losing the export. Do STEP 4 or this: Make sure GPG_TTY is added to the shell (export GPG_TTY=$(tty) in .zshrc)

Test if GPG signing works

echo "test" | gpg --clearsign

Sign an individual commit

git commit -S
git  commit --amend -S

Resources