Skip to content

Instantly share code, notes, and snippets.

@algorythm
Last active February 20, 2020 14:29
Show Gist options
  • Save algorythm/af96bced233f0109e1e7dee2293efe35 to your computer and use it in GitHub Desktop.
Save algorythm/af96bced233f0109e1e7dee2293efe35 to your computer and use it in GitHub Desktop.
Useful Git Tips

Setup GPG Key

Install latest GPG version (the one MacOS ships with is out of date):

$ brew install gpg2 gnupg pinentry-mac

Create a new GPG key

$ gpg --full-generate-key

Keys can be listed with

$ gpg --list-secret-keys --keyid-format LONG
/Users/awo/.gnupg/pubring.kbx
-----------------------------
sec   rsa4096/2634D0430EC71B8E 2020-02-07 [SC]
      07DB46EAF90F61DD2B5061BA2634D0430EC71B8E
uid                 [ultimate] Anders Wiberg Olsen (GPG for Git) <[email protected]>
ssb   rsa4096/4B1E495EE34CDD95 2020-02-07 [E]

Copy 2634D0430EC71B8E (after sec rsa4096/) and paste into .gitconfig:

[user]
  name = Anders Wiberg Olsen
  email = [email protected]
  signingkey = 2634D0430EC71B8E

Then, copy your key:

$ gpg --armor --export 2634D0430EC71B8E | pbcopy

Go to Setting on Github and add your GPG key there.

To force git to always sign the commits, edit your .gitconfig:

[commit]
  gpgsign = true
[gpg]
  program = true

Now we need to ensure GPG actually works. In your ~/.bashrc, ~/.bash_profile or ~/.zshrc, add the following line:

...
export GPG_TTY=`tty`
...

Setup Pinentry

Pinentry makes it so you don't have to keep typing your password all the time.

Insert into ~/.gnupg/gpg-agent.conf following line (files and directories might not exist already):

pinentry-program /usr/local/bin/pinentry-mac

Insert into ~/.gnupg/gpg.conf following line:

use-agent

If you had to create the ~./gnupg directory, give it the correct permissions:

chmod 700 ~/.gnupg

Useful Git tips

This is a small guide for myself to help me remember some git bash stuff.

Basic actions

Unstage a file: git reset <filename>

Remove a file: git rm --cached <filename>

Reset changes in a file: git reset HEAD <filename> --hard

Remove all newly uncommited files: git reset -df

Remove all untracked files (.gitignored files): git reset --dfx

List all branches: git branch -a

Rewrite commit message for ast commit: git commit --amend

Remove all local branches that are removed on remote: git fetch --prune

Squash previous commits

This is the workflow of squashing the last two commits.

# Make some changes in the repository
$ git add -A
$ git commit -m "Add my first file"
# Make some more changes
$ git add -A
$ git commit -m "Forgot to add a line"

We now have a few changes to the repository. Let's check the commit log

$ git log --pretty=oneline --abbrev-commit
f5eee13 (HEAD -> master) Forgot to add a line
6074396 Add my first file
9819663 Initial commit

We want to squash f5eee13 into 6074396. Let's start rebasing

$ git rebase -i HEAD~2
pick 6074396 Add my first file
pick 9819663 Forgot to add a line

# Rebase 6074396..9819663 onto 9819663 (2 commands)
# [...]

The oldest commit is in the beginning. pick or p means the commit we want to use. squash or s is the commit we want to squash. We will thus change pick to squash or s for the commit 9819664 (the most resent one)

pick 6074396 Add my first file
s 9819663 Forgot to add a line

Now it will ask you to make a commit message. After that the rebase is done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment