Skip to content

Instantly share code, notes, and snippets.

@DamonOehlman
Last active November 6, 2022 09:34
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save DamonOehlman/f0aa2ab08379231c18fb85cec8999f0f to your computer and use it in GitHub Desktop.
Save DamonOehlman/f0aa2ab08379231c18fb85cec8999f0f to your computer and use it in GitHub Desktop.
Awesome one liners (not to be used in bars)

A collection of useful commands for dealing with various systems.

git

List all (local) branches and report date and sort in descending order:

git for-each-ref --sort=-committerdate refs/heads/ --format='%(committerdate:short) %(refname:short)'

Remove all untracked files:

git status --porcelain=2 | grep -E "^\?{1}" | cut -c3- | xargs rm -rf

Checking that you haven't "soiled" a local branch with a bad rebase (replace master with the base branch as required):

git diff --name-only $(git merge-base --fork-point master) | xargs git diff origin/$(git status -b | head -n1 | cut -f3 -d' ')

Getting a list of commits (in a relatively useful short form) since a branch diverged from master:

git log --format="%h %an: %s" $(git merge-base release-name master)..

All the modified files on the current branch since forking from master. To see the content of the changes, remove the --name-only flag.

git diff --name-only $(git merge-base --fork-point master)

Reset a feature branch to the latest commit on master (after a git rebase master):

git reset $(git log master --format=%H -n 1)

List all the remote branches for a particular prefix:

git branch -a --list *damon-* --color=never | cut -c3- | grep -E '^remotes\/.*\/damon-' | cut -f3 -d'/'

List all local branches that aren't tracked with a remote branch:

git branch -vv | cut -c 3- | awk '$3 !~/\[/ { print $1 }'

Create tags for a list of release- branches on remote (note the -n-2 for how many "active" branches you want to leave):

git branch -r | grep release- | head -n-2 | cut -f2 -d'/' | xargs -I '{}' git tag {} origin/{}

Move forward in a git history:

git checkout $(git log master --format="%h" | grep -B 1 $(git log -n1 --format="%h") | head -n 1)

Find all the files that are in the current branch that also exist in an another branch. This is especially useful when you have broken out another branch from an existing branch and are trying to make a series of useful pull requests:

comm -23 <(git diff --name-only master..) <(git diff --name-only damon-user-reactivation-facebook..)

Find the commits I have made on the current branch:

git log --author='Damon Oehlman' --format='%H %s'

shell

Find CRLF files in a directory.

grep -IUlr $'\r' --exclude-dir=node_modules --exclude-dir=.git

Find all files matching a particular pattern and exec a command on each of them. In the example below we are looking for all .ts and .tsx files and running prettier over them in place.

find <path> -regextype posix-extended  -regex ".*\.tsx?$" -exec $(yarn bin)/prettier --write {} \;

Using the command above, you may find that some .d.ts file have slipped in (no way around this AFAIK due to not being able to negative look behind in the regex), so you will want to unstage those:

git diff --name-only | grep -E "\.d.ts$" | xargs git checkout --

jq

Getting all the resolved paths from an npm-shrinkwrap.json file:

jq ".. | objects | select(.resolved) | { resolved: .resolved }" < npm-shrinkwrap.json

General black magic for finding the most recent versions of a packages when something like yarn outdated won't work (can't generate lock file for instance):

jq ".dependencies | keys | @tsv" -r < package.json | xargs -I "{}" -d '\t' yarn info --silent --json {} | jq "[.data.name, .data.version]"

Downloading all the files referenced in the npm-shrinkwrap file:

jq -r ".. | objects | select(.resolved) | .resolved" < /path/to/npm-shrinkwrap.json | grep -e "^http" | xargs -L1 -P50 wget -nc --no-verbose

Apparently achievable with the following also:

grep resolved < /path/to/npm-shrinkwrap.json | grep -E '\"https?' | cut -d\" -f4 | xargs -L1 -P50 wget -nc --no-verbose

Get some useful info out of the github pull requests API:

curl -s -X GET \
  -H "Accept: application/vnd.github.cerberus-preview" \
  -H "Authorization: token f333fdf73f1316116981f5b2394505aabdff3ca5"  \
  "https://api.github.com/repos/canva/canva/issues?labels=bugathon&state=all&sort=updated&direction=desc&base=master&per_page=250" \
    | jq '.[] | { title: .title, user: .user.login, state: .state, assignees: [ .assignees[] | .login ], closed_at: .closed_at }'

docker

Run the interactive command prompt on a particular container:

docker exec -it $(docker ps --filter "name=mysql*" --format "{{.ID}}") /bin/bash

Remove ALL registered containers:

docker container ls -a --format="{{.ID}}" | xargs docker container rm

Remove ALL images:

docker image ls -a --format="{{.ID}}" | xargs docker image rm

(you'll need to remove containers that might be attached to the images first)

yarn

Compiling the yarn workspaces that have changed since master:

git diff --name-only master | sed -E 's:packages/([A-Za-z0-9_\-]+)\/.*:\1:' | uniq | xargs -I {} yarn workspace {} compile

imagemagick

Convert a series of images into an MPEG file:

MAGICK_THREAD_LIMIT=2 MAGICK_MEMORY_LIMIT=2G convert -delay 5 -quality 100 *.JPG out.mpeg

The format: format allows you to specify which information you want to show. It works a little bit like printf format, with the notable exception that you get a newline with %n instead of \n.

E.g, format:"The author of %h was %an, %ar%nThe title was >>%s<<%n" would show something like this:

The author of fe6e0ee was Junio C Hamano, 23 hours ago
The title was >>t4119: test autocomputing -p<n> for traditional diff input.<<

The placeholders are:

o   %H: commit hash

o   %h: abbreviated commit hash

o   %T: tree hash

o   %t: abbreviated tree hash

o   %P: parent hashes

o   %p: abbreviated parent hashes

o   %an: author name

o   %P: parent hashes

o   %p: abbreviated parent hashes

o   %an: author name

o   %aN: author name (respecting .mailmap, see git-shortlog(1) or git-blame(1))

o   %ae: author email

o   %aE: author email (respecting .mailmap, see git-shortlog(1) or git-
    blame(1))

o   %ad: author date (format respects --date= option)

o   %aD: author date, RFC2822 style

o   %ar: author date, relative

o   %at: author date, UNIX timestamp

o   %ai: author date, ISO 8601-like format

o   %aI: author date, strict ISO 8601 format

o   %cn: committer name

o   %cN: committer name (respecting .mailmap, see git-shortlog(1) or git-
    blame(1))

o   %ce: committer email

o   %cE: committer email (respecting .mailmap, see git-shortlog(1) or git-
    blame(1))

o   %cd: committer date (format respects --date= option)

o   %cD: committer date, RFC2822 style

o   %cr: committer date, relative

o   %ct: committer date, UNIX timestamp

o   %ci: committer date, ISO 8601-like format

o   %cI: committer date, strict ISO 8601 format

o   %d: ref names, like the --decorate option of git-log(1)

o   %D: ref names without the " (", ")" wrapping.

o   %e: encoding

o   %s: subject

o   %f: sanitized subject line, suitable for a filename

o   %b: body

o   %B: raw body (unwrapped subject and body)

o   %N: commit notes

o   %GG: raw verification message from GPG for a signed commit

o   %G?: show "G" for a Good signature, "B" for a Bad signature, "U" for a
    good, untrusted signature and "N" for no signature

o   %GS: show the name of the signer for a signed commit

o   %GK: show the key used to sign a signed commit

o   %gD: reflog selector, e.g., refs/stash@{1}

o   %gd: shortened reflog selector, e.g., stash@{1}

o   %gn: reflog identity name

o   %gN: reflog identity name (respecting .mailmap, see git-shortlog(1) or git-
    blame(1))

o   %ge: reflog identity email

o   %gE: reflog identity email (respecting .mailmap, see git-shortlog(1) or
    git-blame(1))

o   %gs: reflog subject

o   %Cred: switch color to red

o   %Cgreen: switch color to green

o   %Cblue: switch color to blue

o   %Creset: reset color

o   %C(...): color specification, as described in color.branch.* config option;
    adding auto, at the beginning will emit color only when colors are enabled
    for log output (by color.diff, color.ui, or --color, and respecting the
    auto settings of the former if we are going to a terminal).  auto alone
    (i.e.  %C(auto)) will turn on auto coloring on the next placeholders until
    the color is switched again.

o   %m: left, right or boundary mark

o   %n: newline

o   %%: a raw %

o   %x00: print a byte from a hex code

o   %w([<w>[,<i1>[,<i2>]]]): switch line wrapping, like the -w option of git-
    shortlog(1).

o   %<(<N>[,trunc|ltrunc|mtrunc]): make the next placeholder take at least N
    columns, padding spaces on the right if necessary. Optionally truncate at
    the beginning (ltrunc), the middle (mtrunc) or the end (trunc) if the
    output is longer than N columns. Note that truncating only works correctly
    with N >= 2.

o   %<|(<N>): make the next placeholder take at least until Nth columns,
    padding spaces on the right if necessary

o   %>(<N>), %>|(<N>): similar to %<(<N>), %<|(<N>) respectively, but padding
    spaces on the left

o   %>>(<N>), %>>|(<N>): similar to %>(<N>), %>|(<N>) respectively, except that
    if the next placeholder takes more spaces than given and there are spaces
    on its left, use those spaces

o   %><(<N>), %><|(<N>): similar to % <(<N>), %<|(<N>) respectively, but
    padding both sides (i.e. the text is centered)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment