More at source: On undoing, fixing, or removing commits in git - A git choose-your-own-adventure!ⓡ
See also Post-Production Editing Using Git for great tutorial on git rebase -i $EARLIER_SHA
$ git reset HEAD^
$ git add -p
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.[…]
$ 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 --stdout
Print all commits to the standard output in mbox format, instead of creating a file for each one.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
[TODO]
See: git help apply
git apply [OPTIONS] [<patch>...]
--recount
— tries to apply changes in fuzzy way--index
— puts the changes into staged area tooDoes not create a commit.
$ git log --graph --oneline --decorate --all "$@"
(old version: git log --graph --decorate --pretty=oneline --abbrev-commit --all "$@"
)
$ git log --graph --all --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(cyan)%an%Creset' --abbrev-commit
More at source: How to revert all local changes in a GIT managed project to previous state?
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
Sources: Git Conflict Resolution, man git
git -c merge.conflictstyle=diff3 checkout -m $FILE
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.-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.In other words, a “faster grep
“:
git grep ...
Bonus: can be set as :grep
in vim with:
:set gp=git\ grep\ -n
Git 1.7+
(unverified yet)
The .git/info/sparse-checkout
file uses patterns “similar to .gitignore” [2][3]
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
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
(unverified)
echo '*' > .git/info/sparse-checkout
git read-tree -mu HEAD
git config core.sparsecheckout false
Sources: stackoverflow
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: stackoverflow
git log --diff-filter=D --summary
Source: stackoverflow
git commit --verbose
Source: stackoverflow
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 HEADHEAD^2
— chooses other parent in merge commitsHEAD^^
, 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 anywherev0.1^{/"commit message regexp"}
— newest commit with regexp, reachable from v0.1refs/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
Fetch all reviews of a particular change
Note: for change NNN, NN := NNN mod 100 (e.g. for NNN=187, NN=87).
git fetch gerrit refs/changes/NN/NNN/*:refs/changes/NN/NNN/*
Based on: https://blogs.gnome.org/markmc/2011/09/25/gerrit-patch-review-from-the-command-line/
find DIRNAME -type f -print0 |
xargs -0 stat --format '%Y :%y %n' 2>/dev/null |
sort -nr |
cut -d: -f2- |
head
[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
Useful for naming Go packages. Uses the “expression register” "=
.
In normal mode:
"=expand("%:h:t")<Enter>p
In insert mode (equivalent but using <C-R>
):
<C-R>=expand("%:h:t")<Enter>
For more info about %:h:t
etc., see :help filename-modifiers
.
: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: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’
:let @a = "foobar"
— put text foobar
in register a
:let @f = @%
— put current filename in register f
; can be then reopened in a different window with :e <C-R>f
Source: stackoverflow.com
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)$'
Source: stackoverflow
[Enter] [
~
] [.
]
Also, to break out of a nested hanged session (i.e. ssh within ssh), repeat [~
] n times. E.g. for session 1 level deep:
[Enter] [
~
] [~
] [.
]
Other tricks:
[Enter][
~
][#
] — list forwarded ports in current session[Enter][
~
][?
] — list all available~
-commands
Source: lonesysadmin.net (via)
$ ssh -t -i $PRIVATE_KEY $USER@$MACHINE 'sudo -u $GUI_USER bash -c "DISPLAY=:0.0 import -window root -screen /home/$GUI_USER/foo.png" '
[sudo] password for $USER:
Connection to $MACHINE closed.
if $USER has SSH login disabled, enable it temporarily:
LOCAL$ sudo usermod --expiredate "" $USER
LOCAL$ sudo passwd -u $USER
deploy private SSH key on $REMOTE machine (e.g. as $KEY_PATH), and put the public counterpart of the key in /home/$USER/.ssh/authorized_keys on $LOCAL machine
Note: the private key must have
chmod 400
setup a reverse tunnel, on $REMOTE machine:
REMOTE$ ssh -i $KEY_PATH -o StrictHostKeyChecking=no \
-Nf -R 16003:localhost:22 $USER@$LOCAL
login via the reverse tunnel:
LOCAL$ ssh localhost -p 16003
do what you need via reverse ssh…
cleanup:
REMOTE$ rm $KEY_PATH
REMOTE$ ps aux | grep ssh
REMOTE$ kill ... # kill the process from step 4
If step 2 was done, reverse it:
LOCAL$ sudo usermod --expiredate 1 $USER
LOCAL$ sudo passwd -l $USER
If possible (root access), run an extra instance of sshd on a distinct port, with detailed logs printed to console:
$ sudo /usr/sbin/sshd -d -p 2222
Source: unix.stackexchange
Run ssh with -vvv
flag for detailed logs. Together with the extra server as above, this may look like:
$ ssh -vvv -p 2222 $USER@$HOST
Source: askubuntu
Best solution:
$ ssh-keygen -E md5 -lf <( ssh-keyscan localhost 2>/dev/null )
$ ssh-keygen -E sha256 -lf <( ssh-keyscan localhost 2>/dev/null )
Sources: unix.stackexchange [1] [2]
Alternative:
$ ssh-keygen -E md5 -lf /etc/ssh/ssh_host_rsa_key.pub
$ ssh-keygen -E md5 -lf /etc/ssh/ssh_host_ecdsa_key.pub
One-liner:
ls -1 /etc/ssh/ssh_host_*.pub | join -j2 <( printf "md5\nsha256" ) - | awk '{system("ssh-keygen -E "$1" -lf "$2)}'
same but wrapped:
ls -1 /etc/ssh/ssh_host_*.pub |
join -j2 <( printf "md5\nsha256" ) - |
awk '{system("ssh-keygen -E "$1" -lf "$2)}'
Sources:
For all hosts:
$ ssh-keygen -l -f ~/.ssh/known_hosts
$ ssh-keygen -l -f ~/.ssh/known_hosts -E md5
For single host:
$ ssh-keygen -l -f ~/.ssh/known_hosts -F $HOST:$PORT
$ ssh-keygen -l -f ~/.ssh/known_hosts -F $HOST:$PORT -E md5
Source: stackoverflow
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
func ... {
type change struct {from, to X}
switch (change{a, b}) {
case change{Foo, Bar}:
...
}
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 ...
}
}
sed 1w/dev/stderr
Source: GNU sed manual
sudo netstat -l -p | grep $PORT
-l
— show listening server sockets-p
— show PID/program name-W
— wide: don’t truncate IP addresses)For installed package:
dpkg-query -L $PACKAGE
Other possibilities: http://askubuntu.com/a/32509/111779
$ sudo dpkg-reconfigure virtualbox-dkms
$ sudo dpkg-reconfigure virtualbox
$ sudo modprobe vboxdrv
(via)
set bytea_output='escape';
(via)
Login to remote server with serial port, then start serving the port:
client$ ssh -L 3334:localhost:3334 server
server$ sudo socat -ddd -ddd /dev/ttyS0,raw,b115200,$(cat stty-flags | sed '1,4d' | sed 's/-\([^ ]*\)/\1=0/g' | tr ' ' '\n' | grep -vE 'cmspar|iutf8|extproc' | paste -d, -s ) tcp-l:3334
Create a local virtual serial port (will get assigned a name in /dev/pts/*
) forwarded to remote one:
client$ sudo socat -ddd -ddd pty,link=foobar,raw,echo=0 tcp:localhost:3334
$ curl --resolve "named-host.com:80:$IP" http://named-host.com
Source: some stackoverflow
$ openssl x509 -in $CERTFILE.cer -noout -text
Source: serverfault.com
$ echo | openssl s_client -connect $HOST:443 2>/dev/null |
openssl x509 -noout -enddate
notAfter=...
Source: realguess.net
Fingerprints for ~/.ssh/authorized_keys{,2}:
cat ~/.ssh/authorized_keys | xargs -n1 -I% bash -c 'ssh-keygen -l -f /dev/stdin <<<"%"'
same but wrapped:
cat ~/.ssh/authorized_keys |
xargs -n1 -I% bash -c 'ssh-keygen -l -f /dev/stdin <<<"%"'
Host fingerprints: see SSH command above
Any of the following should do:
$ curl ipinfo.io/ip
$ curl ifconfig.me
$ curl icanhazip.com
$ curl ident.me
$ curl checkip.amazonaws.com
Source: askubuntu.com
$ cat ... > index.html
$ python -m SimpleHTTPServer $PORT
Source: gist