• $ git reset HEAD^
    $ git add -p
  • Single commit/patch

    $ git --git-dir=../source_repo/.git \
    format-patch -k -1 --stdout \
    $COMMIT_SINCE | \
    git am -3 -k --committer-date-is-author-date

    where format-patch options are:

    • -k, --keep-subject Do not strip/add [PATCH] from the first line of the commit log message.
    • -1, -<n> Prepare patches from the topmost <n> commits.
    • --stdout Print all commits to the standard output in mbox format, instead of creating a file for each one.
  • Multiple commits

    Assuming a repository like:

    [A] -> [B] -> [C] -> [D] -> [E] ...

    You can transplant it onto unrelated repository [X], to achieve something like:

    [X] -> [C] -> [D] -> [E] ...

    To do that, first, git fetch both repositories into one, then:

    git rebase B E --onto X --committer-date-is-author-date

    Source: http://stackoverflow.com/a/9091843/98528

  • Apply patch in fuzzy way

    [TODO]

    See: git help apply

    • git apply [OPTIONS] [<patch>...]
    • --recount — tries to apply changes in fuzzy way
    • --index — puts the changes into staged area too

    Does not create a commit.

  • Simpler variant, possible to quickly type by hand from memory

    $ git log --graph --oneline --decorate --all "$@"

    <sub>(old version: git log --graph --decorate --pretty=oneline --abbrev-commit --all "$@")</sub>

  • Better variant, impossible to type quickly from memory, paste it into a script

    $ git log --graph --all --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(cyan)%an%Creset' --abbrev-commit
  • If you want to revert changes made to your working copy, do this:

    git checkout .

    If you want to revert changes made to the index (i.e., that you have added), do this:

    git reset
  • git merge -Xignore-space-change
  • To create a merge like in diagram below:

    o     merge!
    |\
    | o   [snapshot]
    o     [old]
    |
    o
    |
    o     rev0

    where “snapshot” is newer than “old”, do as follows:

    git checkout old
    git merge -ssubtree snapshot -Xignore-space-change
  • git -c merge.conflictstyle=diff3 checkout -m $FILE
  • a) Find commits which add/remove lines matching a regex (i.e. search history)

    git log -G"$REGEX" -p --decorate -a
    • -p shows the actual diff, only of the files containing the change (I think)
    • --decorate is optional (shows info about tags, branches etc.)
    • -a I think makes it look into all commits on all branches. I don’t know yet how to sort the results in sensible order (e.g. by date), however.
    • alternatively, instead of -G, can use -S for simple strings, but it has slightly different semantics! It shows commits where the total count of lines matching the string changes.
  • b) Find text in current repository

    In other words, a “faster grep“:

    git grep ...
  • Initial setup, Linux <sup>[1]</sup><sup>[2]</sup>

    (unverified yet)

    The .git/info/sparse-checkout file uses patterns “similar to .gitignore” <sup>[2]</sup><sup>[3]</sup>

    git config core.sparsecheckout true
    git remote add -f origin git://...
    ## NOTE: below no whitespace between '*' and '>'
    #echo PATH/TO/SUBDIR/*> .git/info/sparse-checkout
    echo some/dir/ >> .git/info/sparse-checkout
    echo another/sub/tree >> .git/info/sparse-checkout
    git checkout BRANCHNAME
  • Existing repo, or changes <sup>[1]</sup>

    git config core.sparsecheckout true
    echo some/dir/ >> .git/info/sparse-checkout
    echo another/sub/tree >> .git/info/sparse-checkout
    git read-tree -mu HEAD
  • Disable <sup>[1]</sup>

    (unverified)

    echo '*' > .git/info/sparse-checkout
    git read-tree -mu HEAD
    git config core.sparsecheckout false
  • git show $REV:$PATH

    Since git ~1.7+, $PATH can be relative with ./path.

  • Alternatively, if the file is to be restored at its original place, a shortcut is:

    git checkout $REV $PATH

    Source: http://stackoverflow.com/a/2734035

  • git log --diff-filter=D --summary
    • git ls-tree --full-tree -r HEAD — list all tracked files already in repo (via);
    • git ls-files — list files including staged (via);
  • git commit --verbose
  • git log --pretty="format:" --name-only [RANGE...]
  • #!/bin/sh
    # git-attic [-M] [PATH] - list deleted files of Git repositories
    #
    # Use -M to not show renamed files, and other git-log options as you like.
    
    git log --raw --no-renames --date=short --format="%h %cd" "$@" |
      awk '/^[0-9a-f]/ { commit=$1; date=$2 }
           /^:/ && $5 == "D" { print date, commit "^:" $6 }' |
      git -p column

    Source: http://chneukirchen.org/blog/archive/2013/01/a-grab-bag-of-git-tricks.html

  • ORIGIN_URL=$( git --git-dir $SUBREPO/.git config --get remote.origin.url | tee /dev/stderr )
    
    git submodule add $ORIGIN_URL $SUBREPO

    Sources: man git-submodule, remote.origin.url

  • Commit:

    • HEAD^ — commit before HEAD
      • HEAD^2 — chooses other parent in merge commits
      • HEAD^^, HEAD^^^, … — grandparent of HEAD, grand-grandparent, etc.
    • HEAD~3 = HEAD^^^ = HEAD^1^1^1
    • :/"commit message regexp" — newest commit with regexp match in commit message, reachable from anywhere
      • v0.1^{/"commit message regexp"} — newest commit with regexp, reachable from v0.1
    • refs/heads/master, refs/tags/v0.1 — “fully qualified” names of: branch master, tag v0.1

    (See: git help revisions)

  • Range of commits:

    • HEAD^!HEAD^..HEAD
  • vim: reopen all files edited before crash/kill

  • vim: new buffer (pseudo-“scratch”)

    :new

    Source: vim wikia

  • Various often used stuff

  • Windows, tabs, buffers

    • [Ctrl-w], [Shift-T] — “promote” current window into a new tab
    • :tabmv -1 — move current tab 1 position to the left (similar for +1 etc.)
    • :tabcl — close current tab
    • :tabo — leave only current tab (close all others)
    • :tabdo windo e — reload all buffers in all windows in all tabs
  • Quickfix window

    • :cex system('go build ./...') — run specified command and load the result in quickfix list. If the result can be parsed using ‘errorformat’, cursor will be moved to position of first error.
      • | copen — add after above command to also show the quickfix window
    • :cn / :cp — jump to next/previous error from quickfix list
    • :ccl — close quickfix window

    [TODO] describe stuff in ‘set errorformat’

  • Registers (copy, paste, …)

    • :let @a = "foobar" — put text foobar in register a
  • To exclude *_test.go (and also pipe through less while keeping colours and grouping):

    ack --go $PATTERN \
      --pager='less -R' --ignore-file=match:_test.go
  • Other examples of excludes:

    --ignore-dir=_vendor
    --ignore-file=match:'\.(txt|out|log)$'
  • [Enter], [~], [.]

  • Windows: ignore vendor in go test

    Powershell script [TODO: how one runs it?]:

    $GOPKG = (go list ./... ) | Out-String -stream | select-string -NotMatch vendor
    iex "go test -v $GOPKG"

    Source: golang.org/issue/15355

  • Pattern 1: poor man’s pattern-matching switch

    func ... {
      type change struct {from, to X}
      switch (change{a, b}) {
      case change{Foo, Bar}:
        ...
      }
  • Pattern 2: easy reentrant Close for goroutines

    func NewX() *X {
      x := &X{
        quit: make(chan struct{}),
      }
      go x.loop()
      return x
    }
    
    func (x *X) Close() error {
      <-x.quit
    }
    
    func (x *X) loop() {
      for {
        select {
        case x.quit<-struct{}{}:
          close(x.quit)
          return
        case y, ok := <-x.myChan:
          if !ok {
            // myChan closed, but we still have to allow x.Close()
            close(x.quit)
            return
          }
          // ... real work ...
        }
    }
  • Commands reuse

    • [Esc], [#] (or [Alt-#] or [Alt-3] or [Esc], [3]) — insert comment sign (“#”) at start of current line, then cancel the line (like Ctrl-C), saving it to history (for later fixing/completing & reuse)
    • [Alt-.] — paste last argument from previous command (press again for earlier commands)
      • [Alt-n], [Alt-.] — paste n-th argument from previous command (starting from 0)
    • [Ctrl-r], some-text — incremental search backwards through history

    Moving, editing

    • [Ctrl-a] / [Ctrl-e] — move to start/end of current line
    • [Alt-b] / [Alt-f] — move 1 word backwards/forwards
    • [Ctrl-k] — kill (delete) part of line after cursor (to line end)
    • [Shift-PgUp] / [Shift-PgDn] — scroll terminal window up/down
  • sudo netstat -l -p | grep $PORT
    • -l — show listening server sockets
    • -p — show PID/program name
    • (-W — wide: don’t truncate IP addresses)
  • WARNING: These techniques should only be used for non-merge commits. If you have a merge commit, you are better off deleting the merge and recreating it.

    If you want to perform significant work on the last commit, you can simply git reset HEAD^. This will undo the commit (peel it off) and restore the index to the state it was in before that commit, leaving the working directory with the changes uncommitted, and you can fix whatever you need to fix and try again.

    […]

  • [FIXME]

    vim -r $(find . -regex '.*/\.\(.*\.\)?sw[po]')
    # or, vim -p + 'cut'? kinda:
    vim -p $(find . -regex '.*/\.\(.*\.\)?sw[po]' | sed 's/\.[^.]*$//' )
    # maybe better:
    vim -p $(ls -1t $(find . -regex '.*/\.\(.*\.\)?sw[po]' ) | sed 's#/\.\([^/]*\)\.[^.]*$#/\1#')

    Based on: :help recovery

{"cards":[{"_id":"37ff2e2157a8f60d0300001b","treeId":"37ff2da057a8f60d03000018","seq":1,"position":1,"parentId":null,"content":"## Split top commit in half\n\nMore at source: _[On undoing, fixing, or removing commits in git - A git choose-your-own-adventure!ⓡ](http://sethrobertson.github.io/GitFixUm/fixup.html)_\n\nSee also _[Post-Production Editing Using Git](http://sethrobertson.github.io/GitPostProduction/gpp.html)_ for great tutorial on `git rebase -i $EARLIER_SHA`"},{"_id":"37ff378b57a8f60d0300001c","treeId":"37ff2da057a8f60d03000018","seq":1,"position":1,"parentId":"37ff2e2157a8f60d0300001b","content":" $ git reset HEAD^\n $ git add -p"},{"_id":"37ff438157a8f60d0300001f","treeId":"37ff2da057a8f60d03000018","seq":1,"position":1,"parentId":"37ff378b57a8f60d0300001c","content":"> **WARNING:** These techniques should only be used for non-merge commits. If you have a merge commit, you are better off deleting the merge and recreating it.\n\n> If you want to perform significant work on the last commit, you can simply `git reset HEAD^`. This will undo the commit (peel it off) and restore the index to the state it was in before that commit, leaving the working directory with the changes uncommitted, and you can fix whatever you need to fix and try again.\n\n> [...]"},{"_id":"37ff460957a8f60d03000020","treeId":"37ff2da057a8f60d03000018","seq":1,"position":2,"parentId":null,"content":"## Transplant a patch from unrelated repository\n\nSource: http://stackoverflow.com/a/9507417/98528"},{"_id":"37ff48b857a8f60d03000021","treeId":"37ff2da057a8f60d03000018","seq":8409954,"position":1,"parentId":"37ff460957a8f60d03000020","content":"## Single commit/patch\n\n $ git --git-dir=../source_repo/.git \\\n format-patch -k -1 --stdout \\\n $COMMIT_SINCE | \\\n git am -3 -k --committer-date-is-author-date\n\nwhere `format-patch` options are:\n\n- `-k, --keep-subject` Do not strip/add [PATCH] from the first line of the commit log message.\n- `-1, -<n>` Prepare patches from the topmost <n> commits.\n- `--stdout` Print all commits to the standard output in mbox format, instead of creating a file for each one."},{"_id":"39f5cf4452acc3bc89000018","treeId":"37ff2da057a8f60d03000018","seq":5444117,"position":2,"parentId":"37ff460957a8f60d03000020","content":"## Multiple commits\n\nAssuming a repository like:\n\n [A] -> [B] -> [C] -> [D] -> [E] ...\n\nYou can transplant it onto unrelated repository `[X]`, to achieve something like:\n\n [X] -> [C] -> [D] -> [E] ...\n\nTo do that, first, `git fetch` both repositories into one, then:\n\n git rebase B E --onto X --committer-date-is-author-date\n\n_Source: http://stackoverflow.com/a/9091843/98528_"},{"_id":"5fa71cb3b7eb27769400002d","treeId":"37ff2da057a8f60d03000018","seq":5444230,"position":3,"parentId":"37ff460957a8f60d03000020","content":"## Apply patch in fuzzy way\n\n*[TODO]*\n\nSee: `git help apply`\n\n* `git apply [OPTIONS] [<patch>...]`\n* `--recount` -- tries to apply changes in fuzzy way\n* `--index` -- puts the changes into staged area too\n\nDoes *not* create a commit."},{"_id":"37ff4b0057a8f60d03000022","treeId":"37ff2da057a8f60d03000018","seq":1,"position":3,"parentId":null,"content":"## Show history using pretty ASCII-Art tree graph"},{"_id":"37ff4c9357a8f60d03000023","treeId":"37ff2da057a8f60d03000018","seq":5975599,"position":1,"parentId":"37ff4b0057a8f60d03000022","content":"## Simpler variant, possible to quickly type by hand from memory\n\n $ git log --graph --oneline --decorate --all \"$@\"\n\n<sub>(old version: `git log --graph --decorate --pretty=oneline --abbrev-commit --all \"$@\"`)</sub>\n"},{"_id":"49768fc3ad0d219571000030","treeId":"37ff2da057a8f60d03000018","seq":5975600,"position":2,"parentId":"37ff4b0057a8f60d03000022","content":"## Better variant, impossible to type quickly from memory, paste it into a script\n\n $ git log --graph --all --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(cyan)%an%Creset' --abbrev-commit"},{"_id":"37ff59c357a8f60d03000024","treeId":"37ff2da057a8f60d03000018","seq":1,"position":4,"parentId":null,"content":"## Remove local changes\n\nMore at source: _[How to revert all local changes in a GIT managed project to previous state?](http://stackoverflow.com/q/1146973/98528)_"},{"_id":"37ff5c9b57a8f60d03000025","treeId":"37ff2da057a8f60d03000018","seq":1,"position":1,"parentId":"37ff59c357a8f60d03000024","content":"If you want to revert changes made to your working copy, do this:\n\n git checkout .\n\nIf you want to revert changes made to the index (i.e., that you have added), do this:\n\n git reset\n"},{"_id":"38c4e76c7518cf646f000014","treeId":"37ff2da057a8f60d03000018","seq":1,"position":4.5,"parentId":null,"content":"## Merge, ignoring whitespace\n\nSource: http://stackoverflow.com/a/9784089/98528"},{"_id":"38c4e7f27518cf646f000015","treeId":"37ff2da057a8f60d03000018","seq":1,"position":1,"parentId":"38c4e76c7518cf646f000014","content":" git merge -Xignore-space-change"},{"_id":"399aaa1ccc1512596f000016","treeId":"37ff2da057a8f60d03000018","seq":1,"position":4.75,"parentId":null,"content":"## Merge a related snapshot repo\n\nSource: http://stackoverflow.com/questions/18319485/forcing-git-to-merge-add-add-conflicts#comment26892583_18319485\n"},{"_id":"399aaceecc1512596f000017","treeId":"37ff2da057a8f60d03000018","seq":1,"position":1,"parentId":"399aaa1ccc1512596f000016","content":"To create a merge like in diagram below:\n\n o merge!\n |\\\n | o [snapshot]\n o [old]\n |\n o\n |\n o rev0\n\nwhere \"snapshot\" is newer than \"old\", do as follows:\n\n git checkout old\n git merge -ssubtree snapshot -Xignore-space-change\n\n"},{"_id":"5035c4248ca4ea275300003c","treeId":"37ff2da057a8f60d03000018","seq":1669185,"position":4.8125,"parentId":null,"content":"## 3-way diff of a file during merge/rebase\n\nSources: *[Git Conflict Resolution](http://tedfelix.com/software/git-conflict-resolution.html)*, *[man git](https://www.kernel.org/pub/software/scm/git/docs/git.html)*"},{"_id":"5035ca658ca4ea275300003d","treeId":"37ff2da057a8f60d03000018","seq":1669197,"position":1,"parentId":"5035c4248ca4ea275300003c","content":" git -c merge.conflictstyle=diff3 checkout -m $FILE"},{"_id":"6289b5e17d48f4419a000030","treeId":"37ff2da057a8f60d03000018","seq":5975558,"position":4.84375,"parentId":null,"content":"## Search repo/history"},{"_id":"6289b6c47d48f4419a000031","treeId":"37ff2da057a8f60d03000018","seq":5975570,"position":1,"parentId":"6289b5e17d48f4419a000030","content":"## a) Find commits which add/remove lines matching a regex (i.e. search history)\n\n git log -G\"$REGEX\" -p --decorate -a\n\n* `-p` shows the actual diff, *only of the files containing the change* (I think)\n* `--decorate` is optional (shows info about tags, branches etc.)\n* `-a` I think makes it look into all commits on all branches. I don't know yet how to sort the results in sensible order (e.g. by date), however.\n* alternatively, instead of `-G`, can use `-S` for simple strings, but it has slightly *different semantics!* It shows commits where the *total count* of lines matching the string changes."},{"_id":"6289c5f47d48f4419a000032","treeId":"37ff2da057a8f60d03000018","seq":5975569,"position":2,"parentId":"6289b5e17d48f4419a000030","content":"## b) Find text in *current* repository\n\nIn other words, a \"faster `grep`\":\n\n git grep ..."},{"_id":"3c6e167873ff47ac71000019","treeId":"37ff2da057a8f60d03000018","seq":1,"position":4.875,"parentId":null,"content":"## Partial (\"sparse\") checkouts\n\nGit 1.7+"},{"_id":"3c6e1c3c73ff47ac7100001a","treeId":"37ff2da057a8f60d03000018","seq":1,"position":1,"parentId":"3c6e167873ff47ac71000019","content":"### Initial setup, Linux <sup>[[1]](http://stackoverflow.com/a/4909267/98528)</sup><sup>[[2]](http://jasonkarns.com/blog/subdirectory-checkouts-with-git-sparse-checkout/)</sup>\n(unverified yet)\n\nThe `.git/info/sparse-checkout` file uses patterns \"similar to .gitignore\" <sup>[[2]](http://jasonkarns.com/blog/subdirectory-checkouts-with-git-sparse-checkout/)</sup><sup>[[3]](http://schacon.github.com/git/git-read-tree.html#_sparse_checkout)</sup>\n\n git config core.sparsecheckout true\n git remote add -f origin git://...\n ## NOTE: below no whitespace between '*' and '>'\n #echo PATH/TO/SUBDIR/*> .git/info/sparse-checkout\n echo some/dir/ >> .git/info/sparse-checkout\n echo another/sub/tree >> .git/info/sparse-checkout\n git checkout BRANCHNAME\n"},{"_id":"3c6e336473ff47ac7100001c","treeId":"37ff2da057a8f60d03000018","seq":1,"position":1.5,"parentId":"3c6e167873ff47ac71000019","content":"### Existing repo, or changes <sup>[[1]](http://jasonkarns.com/blog/subdirectory-checkouts-with-git-sparse-checkout/)</sup>\n\n git config core.sparsecheckout true\n echo some/dir/ >> .git/info/sparse-checkout\n echo another/sub/tree >> .git/info/sparse-checkout\n git read-tree -mu HEAD\n"},{"_id":"3c6e3db873ff47ac7100001d","treeId":"37ff2da057a8f60d03000018","seq":1,"position":3,"parentId":"3c6e167873ff47ac71000019","content":"### Disable <sup>[[1]](http://schacon.github.io/git/git-read-tree.html#_sparse_checkout)</sup>\n(unverified)\n\n echo '*' > .git/info/sparse-checkout\n git read-tree -mu HEAD\n git config core.sparsecheckout false\n"},{"_id":"5366a1b7276bb07a6a000024","treeId":"37ff2da057a8f60d03000018","seq":2121560,"position":4.90625,"parentId":null,"content":"## Retrieve a file from specific revision\n\nSources: [stackoverflow](http://stackoverflow.com/questions/610208/how-to-retrieve-a-single-file-from-specific-revision-in-git)"},{"_id":"5366a426276bb07a6a000025","treeId":"37ff2da057a8f60d03000018","seq":2121572,"position":1,"parentId":"5366a1b7276bb07a6a000024","content":" git show $REV:$PATH\n\nSince git ~1.7+, `$PATH` can be relative with `./path`."},{"_id":"6feb83a8c88a4edacd000052","treeId":"37ff2da057a8f60d03000018","seq":8595567,"position":2,"parentId":"5366a1b7276bb07a6a000024","content":"Alternatively, if the file is to be restored at its original place, a shortcut is:\n\n git checkout $REV $PATH\n\n*Source: http://stackoverflow.com/a/2734035*"},{"_id":"60164f449bbfa86d0d00002e","treeId":"37ff2da057a8f60d03000018","seq":5535841,"position":4.9140625,"parentId":null,"content":"## Browse deleted files per commit\n\nSource: [stackoverflow](http://stackoverflow.com/a/953573/98528)"},{"_id":"601650f29bbfa86d0d00002f","treeId":"37ff2da057a8f60d03000018","seq":5535842,"position":1,"parentId":"60164f449bbfa86d0d00002e","content":" git log --diff-filter=D --summary"},{"_id":"6a1a5b73e079b665ba000049","treeId":"37ff2da057a8f60d03000018","seq":7719985,"position":4.916015625,"parentId":null,"content":"## List all files in repo\n"},{"_id":"6a1a5bcae079b665ba00004a","treeId":"37ff2da057a8f60d03000018","seq":7719992,"position":1,"parentId":"6a1a5b73e079b665ba000049","content":"* `git ls-tree --full-tree -r HEAD` &mdash; list all tracked files already in repo *([via](http://stackoverflow.com/a/8533413/98528))*;\n* `git ls-files` &mdash; list files including staged *([via](http://stackoverflow.com/a/14406253/98528))*;"},{"_id":"65948a08416d884d9f000036","treeId":"37ff2da057a8f60d03000018","seq":6577409,"position":4.91796875,"parentId":null,"content":"## commit: show full diff in editor\n\n*Source: [stackoverflow](http://stackoverflow.com/questions/4750148/git-show-index-diff-in-commit-message-as-comment)*"},{"_id":"65948bce416d884d9f000037","treeId":"37ff2da057a8f60d03000018","seq":6577410,"position":1,"parentId":"65948a08416d884d9f000036","content":" git commit --verbose"},{"_id":"6649f3e8c276bfeb4e000039","treeId":"37ff2da057a8f60d03000018","seq":6698693,"position":4.919921875,"parentId":null,"content":"## log/diff: file names only\n\n*Source: [stackoverflow](http://stackoverflow.com/a/14224142)*"},{"_id":"6649f542c276bfeb4e00003a","treeId":"37ff2da057a8f60d03000018","seq":6698698,"position":1,"parentId":"6649f3e8c276bfeb4e000039","content":" git log --pretty=\"format:\" --name-only [RANGE...]"},{"_id":"56a5ae937ba505b0eb000037","treeId":"37ff2da057a8f60d03000018","seq":2643923,"position":4.921875,"parentId":null,"content":"## Git attic: list deleted files"},{"_id":"56a5af597ba505b0eb000038","treeId":"37ff2da057a8f60d03000018","seq":2643926,"position":1,"parentId":"56a5ae937ba505b0eb000037","content":"````\n#!/bin/sh\n# git-attic [-M] [PATH] - list deleted files of Git repositories\n#\n# Use -M to not show renamed files, and other git-log options as you like.\n\ngit log --raw --no-renames --date=short --format=\"%h %cd\" \"$@\" |\n awk '/^[0-9a-f]/ { commit=$1; date=$2 }\n /^:/ && $5 == \"D\" { print date, commit \"^:\" $6 }' |\n git -p column\n````\n\n*Source: http://chneukirchen.org/blog/archive/2013/01/a-grab-bag-of-git-tricks.html*"},{"_id":"593a60e1fa7f33d274000029","treeId":"37ff2da057a8f60d03000018","seq":3053144,"position":4.9296875,"parentId":null,"content":"## Git submodule from existing .git subdir\n"},{"_id":"593a61dafa7f33d27400002a","treeId":"37ff2da057a8f60d03000018","seq":7720037,"position":1,"parentId":"593a60e1fa7f33d274000029","content":"````\nORIGIN_URL=$( git --git-dir $SUBREPO/.git config --get remote.origin.url | tee /dev/stderr )\n\ngit submodule add $ORIGIN_URL $SUBREPO\n````\n\n*Sources: [man git-submodule](https://www.kernel.org/pub/software/scm/git/docs/git-submodule.html), [remote.origin.url](http://stackoverflow.com/a/4090938)*"},{"_id":"66dc40579a5318cae200003d","treeId":"37ff2da057a8f60d03000018","seq":6808927,"position":4.93359375,"parentId":null,"content":"## Ways to specify a commit\n"},{"_id":"66dc40f09a5318cae200003e","treeId":"37ff2da057a8f60d03000018","seq":6809083,"position":1,"parentId":"66dc40579a5318cae200003d","content":"Commit:\n\n- `HEAD^` -- commit before HEAD\n - `HEAD^2` -- chooses other parent in merge commits\n - `HEAD^^`, `HEAD^^^`, ... -- grandparent of HEAD, grand-grandparent, etc.\n- `HEAD~3` = `HEAD^^^` = `HEAD^1^1^1`\n- `:/\"commit message regexp\"` -- newest commit with regexp match in commit message, reachable from anywhere\n - `v0.1^{/\"commit message regexp\"}` -- newest commit with regexp, reachable from *v0.1*\n- `refs/heads/master`, `refs/tags/v0.1` -- \"fully qualified\" names of: branch *master*, tag *v0.1*\n\n*(See: git help revisions)*"},{"_id":"66dc5a319a5318cae200003f","treeId":"37ff2da057a8f60d03000018","seq":6809081,"position":2,"parentId":"66dc40579a5318cae200003d","content":"Range of commits:\n\n- `HEAD^!` ≟ `HEAD^..HEAD`"},{"_id":"536532aed958b5bc3b000024","treeId":"37ff2da057a8f60d03000018","seq":2119665,"position":4.9375,"parentId":null,"content":"# Gerrit"},{"_id":"536532eed958b5bc3b000025","treeId":"37ff2da057a8f60d03000018","seq":2119666,"position":4.96875,"parentId":null,"content":"Fetch all reviews of a particular change\n"},{"_id":"5365345dd958b5bc3b000026","treeId":"37ff2da057a8f60d03000018","seq":2119682,"position":1,"parentId":"536532eed958b5bc3b000025","content":"Note: for change NNN, ***NN := NNN mod 100*** (e.g. for NNN=187, NN=87).\n\n git fetch gerrit refs/changes/NN/NNN/*:refs/changes/NN/NNN/*\n\n*Based on: https://blogs.gnome.org/markmc/2011/09/25/gerrit-patch-review-from-the-command-line/*"},{"_id":"385c9350b13fad348e000011","treeId":"37ff2da057a8f60d03000018","seq":1,"position":5,"parentId":null,"content":"# NON-git"},{"_id":"385c93f5b13fad348e000012","treeId":"37ff2da057a8f60d03000018","seq":1,"position":6,"parentId":null,"content":"## find most recent files, recursively\n"},{"_id":"385c9510b13fad348e000013","treeId":"37ff2da057a8f60d03000018","seq":1,"position":1,"parentId":"385c93f5b13fad348e000012","content":" find DIRNAME -type f -print0 |\n xargs -0 stat --format '%Y :%y %n' 2>/dev/null |\n sort -nr |\n cut -d: -f2- |\n head\n\nbased on: http://stackoverflow.com/questions/5566310/how-to-recursively-find-and-list-the-latest-modified-files-in-a-directory-with-s"},{"_id":"6923143cf981145c19000044","treeId":"37ff2da057a8f60d03000018","seq":7556750,"position":6.5,"parentId":null,"content":"## vim: various stuff\n"},{"_id":"5b60f3688f307226c700002b","treeId":"37ff2da057a8f60d03000018","seq":7556751,"position":1,"parentId":"6923143cf981145c19000044","content":"## vim: reopen all files edited before crash/kill"},{"_id":"5b60f5628f307226c700002c","treeId":"37ff2da057a8f60d03000018","seq":4784549,"position":1,"parentId":"5b60f3688f307226c700002b","content":"[FIXME]\n\n vim -r $(find . -regex '.*/\\.\\(.*\\.\\)?sw[po]')\n # or, vim -p + 'cut'? kinda:\n vim -p $(find . -regex '.*/\\.\\(.*\\.\\)?sw[po]' | sed 's/\\.[^.]*$//' )\n # maybe better:\n vim -p $(ls -1t $(find . -regex '.*/\\.\\(.*\\.\\)?sw[po]' ) | sed 's#/\\.\\([^/]*\\)\\.[^.]*$#/\\1#')\n\n*Based on: [:help recovery](http://vimdoc.sourceforge.net/htmldoc/usr_11.html#11.2)*"},{"_id":"62e5b1771f12a78c75000036","treeId":"37ff2da057a8f60d03000018","seq":7556752,"position":2,"parentId":"6923143cf981145c19000044","content":"## vim: new buffer (pseudo-\"scratch\")\n\n :new\n\n*Source: [vim wikia](http://vim.wikia.com/wiki/Vim_buffer_FAQ)*"},{"_id":"69231624f981145c19000045","treeId":"37ff2da057a8f60d03000018","seq":7556759,"position":3,"parentId":"6923143cf981145c19000044","content":"## Various often used stuff\n"},{"_id":"6923171bf981145c19000046","treeId":"37ff2da057a8f60d03000018","seq":7556828,"position":4,"parentId":"6923143cf981145c19000044","content":"### Windows, tabs, buffers\n\n- [Ctrl-w], [Shift-T] — \"promote\" current window into a new tab\n- `:tabmv -1` — move current tab 1 position to the left (similar for +1 etc.)\n- `:tabcl` — close current tab\n- `:tabo` — leave only current tab (close all others)\n- `:tabdo windo e` — reload all buffers in all windows in all tabs"},{"_id":"69558ef9f981145c19000047","treeId":"37ff2da057a8f60d03000018","seq":7604044,"position":5,"parentId":"6923143cf981145c19000044","content":"### Quickfix window\n\n* `:cex system('go build ./...')` — run specified command and load the result in quickfix list. If the result can be parsed using 'errorformat', cursor will be moved to position of first error.\n * ...` | copen` — add after above command to also show the quickfix window\n* `:cn` / `:cp` — jump to next/previous error from quickfix list\n* `:ccl` — close quickfix window\n\n[TODO] describe stuff in 'set errorformat'"},{"_id":"696f0c13f981145c19000048","treeId":"37ff2da057a8f60d03000018","seq":7604046,"position":6,"parentId":"6923143cf981145c19000044","content":"### Registers (copy, paste, ...)\n\n- `:let @a = \"foobar\"` — put text `foobar` in register `a`\n"},{"_id":"62965aa2a8be1063a2000033","treeId":"37ff2da057a8f60d03000018","seq":5986001,"position":8,"parentId":null,"content":"## ack: exclude some files from search\n\n*Source: [stackoverflow.com](http://stackoverflow.com/a/19458758/98528)*"},{"_id":"62965c74a8be1063a2000034","treeId":"37ff2da057a8f60d03000018","seq":5986014,"position":1,"parentId":"62965aa2a8be1063a2000033","content":"To exclude _*_test.go_ (and also pipe through less while keeping colours and grouping):\n\n ack --go $PATTERN \\\n --pager='less -R' --ignore-file=match:_test.go"},{"_id":"65d40b82281aa0f21e000038","treeId":"37ff2da057a8f60d03000018","seq":6622459,"position":2,"parentId":"62965aa2a8be1063a2000033","content":"Other examples of excludes:\n\n --ignore-dir=_vendor\n --ignore-file=match:'\\.(txt|out|log)$'"},{"_id":"668f617067ea131b6b00003c","treeId":"37ff2da057a8f60d03000018","seq":6751571,"position":9,"parentId":null,"content":"## ssh: break hanged session\n\n*Source: [stackoverflow](http://askubuntu.com/a/29952)*"},{"_id":"668f62ec67ea131b6b00003d","treeId":"37ff2da057a8f60d03000018","seq":6751580,"position":1,"parentId":"668f617067ea131b6b00003c","content":"[Enter], [`~`], [`.`]"},{"_id":"678a2d57cced290396000040","treeId":"37ff2da057a8f60d03000018","seq":7900684,"position":10,"parentId":null,"content":"## Go: tips, tricks & patterns\n\n\n"},{"_id":"678a2f9acced290396000041","treeId":"37ff2da057a8f60d03000018","seq":7900682,"position":1,"parentId":"678a2d57cced290396000040","content":"### Windows: ignore vendor in go test\n\nPowershell script [TODO: how one runs it?]:\n\n $GOPKG = (go list ./... ) | Out-String -stream | select-string -NotMatch vendor\n iex \"go test -v $GOPKG\"\n\n*Source: [golang.org/issue/15355](https://github.com/golang/go/issues/15355)*\n\n"},{"_id":"6b4ad0c572e50a63fd00004b","treeId":"37ff2da057a8f60d03000018","seq":7900688,"position":2,"parentId":"678a2d57cced290396000040","content":"### Pattern 1: poor man's pattern-matching switch\n\n```go\nfunc ... {\n type change struct {from, to X}\n switch (change{a, b}) {\n case change{Foo, Bar}:\n ...\n }\n```\n\n"},{"_id":"6b4ad84f72e50a63fd00004c","treeId":"37ff2da057a8f60d03000018","seq":7900694,"position":3,"parentId":"678a2d57cced290396000040","content":"### Pattern 2: easy reentrant Close for goroutines\n\n```go\nfunc NewX() *X {\n x := &X{\n quit: make(chan struct{}),\n }\n go x.loop()\n return x\n}\n\nfunc (x *X) Close() error {\n <-x.quit\n}\n\nfunc (x *X) loop() {\n for {\n select {\n case x.quit<-struct{}{}:\n close(x.quit)\n return\n case y, ok := <-x.myChan:\n if !ok {\n // myChan closed, but we still have to allow x.Close()\n close(x.quit)\n return\n }\n // ... real work ...\n }\n}\n```\n"},{"_id":"69150eb4f981145c19000042","treeId":"37ff2da057a8f60d03000018","seq":7546686,"position":11,"parentId":null,"content":"## bash: keyboard shortcuts\n"},{"_id":"69150f53f981145c19000043","treeId":"37ff2da057a8f60d03000018","seq":7546835,"position":1,"parentId":"69150eb4f981145c19000042","content":"### Commands reuse\n* **[Esc], [\\#]** (or **[Alt-#]** or [Alt-3] or [Esc], [3]) — insert comment sign (\"#\") at start of current line, then cancel the line (like Ctrl-C), saving it to history (for later fixing/completing & reuse)\n* **[Alt-.]** — paste last argument from previous command (press again for earlier commands)\n * **[Alt-*n*], [Alt-.]** — paste *n*-th argument from previous command (starting from 0)\n* **[Ctrl-r], *some-text*** — incremental search backwards through history\n\n### Moving, editing\n* **[Ctrl-a]** / **[Ctrl-e]** — move to start/end of current line\n* **[Alt-b]** / **[Alt-f]** — move 1 word backwards/forwards\n* **[Ctrl-k]** — kill (delete) part of line after cursor (to line end)\n* **[Shift-PgUp]** / **[Shift-PgDn]** — scroll terminal window up/down\n"},{"_id":"7049c66804cfa1fbef00004f","treeId":"37ff2da057a8f60d03000018","seq":8650132,"position":12,"parentId":null,"content":"## netstat: what's listening on port\n"},{"_id":"7049c81104cfa1fbef000050","treeId":"37ff2da057a8f60d03000018","seq":8650140,"position":1,"parentId":"7049c66804cfa1fbef00004f","content":" sudo netstat -l -p | grep $PORT\n\n* `-l` &mdash; show listening server sockets\n* `-p` &mdash; show PID/program name\n* (`-W` &mdash; wide: don't truncate IP addresses)"}],"tree":{"_id":"37ff2da057a8f60d03000018","name":"git","publicUrl":"git-notes"}}