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-originChange the author of a Git commit
git commit --amend --author="Your Name <your.name@email.com>" --no-editSet Global Git user details
git config --global user.name "Your Name"
git config --global user.email your.name@email.comRebasing
Interactively rebase the first commit
git rebase -i --rootUndo a rebase
git reset --hard ORIG_HEADTags
List tags
git tag -lCreate new tag
git tag v1.0Create & check out a new branch off a tag
git checkout -b v1.0 tags/v1.0Delete tag
git tag -d v1.0
git push origin :refs/tags/v1.0Tidy 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 originGarbage collection
Sometimes Git gets confused and shows messages, such as “unable to update local ref”
git gc --prune=nowShortcuts
Switch back to previous branch
git checkout -Create & check out a new branch off a specific commit
git checkout -b my-new-branch a7df14c7e1ebabe05015d7e462b9230e859cdbacSummarise changes between feature branch (latest commit) and main (HEAD)
git diff --name-status origin/main...HEAD | cut -f1 | sort | uniq -cLife 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.

Install GPG CLI
brew install gpg2Generate new GPG key
gpg --full-generate-keyList GPG keys
gpg --list-secret-keys --keyid-format=longExport 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 --clearsignSign an individual commit
git commit -Sgit commit --amend -SResources
- Further reading on Git signing: https://docs.github.com/en/authentication/managing-commit-signature-verification/signing-commits
- Tell Git about the signing key: https://docs.github.com/en/authentication/managing-commit-signature-verification/telling-git-about-your-signing-key
- Multiple GPG keys: https://stackoverflow.com/questions/52479641/manage-2-git-users-gpg-key-and-choose-gpg-sign-per-user/58623519#58623519