SWTM-2088_Atlassian-Git-Cheatsheet.pdf

Oh Shit, Git!?!

To remove a recently committed file from git history (works best if there aren’t too many commits in between you and the one you want to amend):

  • Stash any local changes
  • git reset --soft to the commit with the file you want to remove
  • Stash local changes
  • git rm <path/to/file>
  • git commit --amend --no-edit
  • git push --force
  • Un-stash changes from reset commits and redo them

Git stash tricks

Stash unstaged changes

git stash push -k

Stash unstaged changes, including untracked (new) files

git stash push -k -u

How do you fix a bad merge, and replay your good commits onto a fixed merge?

Git bisect

Find bad commit in git history with known good and bad commits using git bisect

How to use git bisect?

Git - git-bisect Documentation

Git hooks

GitHub - evilmartians/lefthook: Fast and powerful Git hooks manager for any type of projects.

https://github.com/arxanas/git-branchless

Git reflog

Untitled

Sign git commits with gpg key

Sign git commits on GitHub with GPG in macOS

Generate ssh key and gpg key

mkdir -p ~/dev/path/to/repo
mkdir -p ~/dev/path/to/repo/.ssh
ssh-keygen -t rsa -b 4096 -C "your.name.here@somewhere.com.au" -f ~/dev/path/to/repo/.ssh/id_rsa-somewhere
ssh-add --apple-use-keychain ~/dev/path/to/repo/.ssh/id_rsa-somewhere
cat ~/dev/path/to/repo/.ssh/id_rsa-somewhere.pub | pbcopy # ...and then paste this into Azure DevOps' settings 
gpg --full-generate-key # Choose default (RSA and RSA), then 4096 bits, no expiry, then enter your (insert name here)'s account details

Setup global gitconfig

[user]
	name = Your Name Here
[includeIf "gitdir:~/dev/path/to/repo/"]
	path = ~/dev/path/to/repo/.gitconfig

Create gitconfig for /path/to/repo

[user]
	email = your.name.here@somewhere.com.au
	signingkey = XXXXXXX # Run `gpg --list-secret-keys --keyid-format=long your.name.here@somewhere.com.au | sed -nr 's#^sec   rsa4096/([A-F0-9]+) .*#\1#p'` to get this - see https://docs.github.com/en/authentication/managing-commit-signature-verification/telling-git-about-your-signing-key
[commit]
	gpgsign = true
[core]
	sshCommand = "ssh -i ~/dev/path/to/repo/.ssh/id_rsa-somewhere"

Now, every git command against this repo will need password verification for your gpg key… So how do we make this less annoying?

brew install gpg-suite-no-mail

In mac app “GPGPreferences”, setup “Default key”, and “Remember for…” - 600 seconds - asking for password every hour.

Add the following line to ~/.gpg/gpg-agent.conf

pinentry-program /usr/local/MacGPG2/libexec/pinentry-mac.app/Contents/MacOS/pinentry-mac

…should look like this:

Untitled

Troubleshooting

My ssh key is registered with GitHub/ADO/AWS, my gitconfig is wired up to use said key, but I’m still getting authentication issues

Try adding the ssh key identity to the ssh agent:

ssh-add --apple-use-keychain ~/dev/path/to/repo/.ssh/id_rsa-somewhere

How to use Mac OS X Keychain with SSH keys?

Git blame

A better git blame with --ignore-rev