Skip to content

Instantly share code, notes, and snippets.

@DamonOehlman

DamonOehlman/docker.md

Last active Dec 18, 2020
Embed
What would you like to do?
Awesome one liners (not to be used in bars)

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)

I'm sure there's probably a more efficient way to do this, but before CampJS I'm pretty keen to bring down all the elm packages that are available from the elm package repository.

This seems to do the trick :)

curl -s http://package.elm-lang.org/all-packages
| jq -r ".[].name"
| xargs -L 1 -I{} bash -c "[[ ! -d {} ]] && git clone 'git://github.com/{}.git' '{}' || true" Requires the use of the excellent jq command-line tool.

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'

git log formatting help

The format:<string> 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)

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 --

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]"

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment