Skip to content

Instantly share code, notes, and snippets.

@zanona
Last active April 10, 2019 08:37
Show Gist options
  • Save zanona/36d52eb675308d70a07c to your computer and use it in GitHub Desktop.
Save zanona/36d52eb675308d70a07c to your computer and use it in GitHub Desktop.
DevTips

Git Tips

Mirror local repo to reflect remote

git fetch origin
git reset --hard origin/master

Delete remote branch

git push origin --delete <branchName>

Add submodule to specific branch

git submodule add -b branch-name [email protected]:user/repo.git directory

Force merge from remote (overwrite tree)

git fetch origin master
git reset --hard FETCH_HEAD
git clean -df

Quick Conflict resolution

git checkout [--theirs|--ours] <file|.>
git mergetool --tool=emerge [file]  # keys: (n) next, (a|b) choose side, (q) quit

Cherry-picking (merge single commit only)

git checkout new-feature
git log (get <commit-id> of the desired change)
git checkout branch-to-insert-changes
git cherry-pick <commit-id>

Track a remote repository fro branch

git remote add <remote> [email protected]:user/repo.git
git branch --set-upstream <branch-name> <remote>/<branch-name>

# Check .git/config
[remote "remote"]
  url = [email protected]:user/repo.git
  fetch = +refs/heads/*:refs/remotes/remote-name/*
[branch "branch-name"]
  remote = remote-name
   merge = refs/heads/master

Track ignored file just once

git update-index --assume-unchanged [FILENAME]

Then if you want to track the file later

git update-index --no-assume-unchanged [FILENAME]

Get repository size

git count-objects -vH

Compress repository (https://stackoverflow.com/a/2116892/165750)

git reflog expire --all --expire=now && git gc --prune=now --aggressive

Clone only latest commit

git clone --depth 1 <repository-url>

Server Tips

SSH

add the public key to remote host

ssh-copy-id user@host

IPTables

Description Command
List Rules iptables -L INPUT -n --line-numbers or iptables -L -v
Allow Rule iptables -A INPUT -p tcp --dport 9200 -j ACCEPT
└ localhost iptables -A INPUT -p tcp --dport 9200 -j ACCEPT -s localhost
Block Rule iptables -A INPUT -p tcp --dport 9200 -j DROP
Delete Rule iptables -D INPUT <<number>>
Save iptables-save

*IPTables DONT Survive Reboot on Debian (instructions):

DNS

Description Command
Check what is running on ports sudo netstat -lntp
Kill process running on port sudo kill $(sudo lsof -t -i:3000)
Clean Mac DNS sudo discoveryutil mdnsflushcache; sudo discoveryutil udnsflushcaches
  • sudo apt-get remove vim-tiny
  • sudo apt-get install vim

Unix Tips

Command manipulation

Description Command Example
Runs last command starting with ca !ca cat
Represents last argument used in command !$ mkdir /test; cd !$
Same as above, not sure what is the difference $_
Runs last command that contains etc anywhere !?etc !?etc
Reverse search for your input Ctrl+r
Run command on background & tail -f file.json &
Foreground previous command fg
List background commands jobs
Kill background command kill %[JOB_ID] kill %1

History:

use history command to list command history

Description Command
Clean command history -c
Read from .bash_history file history -r
Write history command history -w
Erase history history -cwr
Erase duplicated commands on .bash_history HISTCONTROL=erasedups

Allow to queue process

$ diff <(curl http://somesite/file1) <(curl http://somesite/file2)

Vim Tips

Utils

Description Command
Show statistcs about buffer/file g Ctrl+G
Redraw screen Ctrl+L
Highlight matches of current word shift+# (On normal mode)
Sort Lines Alphabetically selection + :sort u
Find current character code over the character in normal mode type ga

Navigation

Description Command
Scroll to beginning of file gg
Scroll to end of file G
Moves screen up one line Ctrl+e
Moves screen down one line Ctrl+y
Moves screen up ½ page Ctrl+u
Moves screen down ½ page Ctrl+d
Moves screen up one page Ctrl+b
Moves screen down one page Ctrl+f

Split Screen

Description Command
Split window :vsplit or :split
Close split Ctrl+W+q
Move around screens Ctrl+W+arrow key

HTML

Description Command
Delete inside tag dit
change inside tag cit
copy/yank inside tag yit
visual inside tag vit
paste inside tag vitp

Syntax Highlighting

Search and Replace

  • flag c -> interactive replacement
  • :%s/pattern/replacement/flags (%s - all file)
  • \< \> world boundaries
  • :%s/\%u00a0//g remove nbsp characters

Transform search in replace

  • /search_term
  • :%s/ctrl+r//replacement/flags

Search multiple files more

  • :vimgrep /pattern/g *.html
  • :vimgrep /pattern/g ## use previous file pattern

Line Wrapping

Description Command
Make text break on number of lines set textwidth=80 or set tw=80
Adjust/Wrap text already in place visual select + gq
Unwrap/Join text in place visual select + J
Show column margin set colorcolumn=80 or set cc=80
Change viewport width set columns=80
Break Line keeping indentation `set breakindent
Ajust following line breaks set breakindentopt=shift:{n}

Plugins

[Align content with Tabular] (http://vimcasts.org/episodes/aligning-text-with-tabular-vim/)

  • Use :Tabular /,\zs where , is the separator and \zs will not include the separator in the selection.
  • Or natively in UNIX :!column -t -s "," however, it will remove the separator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment