Skip to main content

10 posts tagged with "Tools "

View All Tags

SSH

· One min read

Open in Notion

Log into server with SSH keys

Generate SSH keys

ssh-keygen -t rsa -b 4096

Your keys will be created at ~/.ssh/id_rsa.pub and ~/.ssh/id_rsa

Transfer Your Public Key to the Server

ssh-copy-id user@remote_server

If ssh-copy-id is unavailable, please

cat ~/.ssh/id_rsa.pub | ssh user@remote_server "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

ngrok

· One min read

Open in Notion

Installation

bookmark

Commands

Run an instance

# start in background and forward to https://localhost:3000
nohup ./ngrok http https://localhost:3000 &

Get Tunnels

curl \
-X GET \
-H "Authorization: Bearer {API_KEY}" \
-H "Ngrok-Version: 2" \
https://api.ngrok.com/tunnels

Response

{
"tunnels": [
{
"id": "tn_2NTVGqGpa5w2LExRzILEksu7FOa",
"public_url": "https://b8ad9cf4eff6.ngrok.paid",
"started_at": "2023-03-24T19:59:25Z",
"proto": "https",
"region": "us",
"tunnel_session": {
"id": "ts_2NTVGmVw5yMzr02ZZzlX4VC6b6L",
"uri": "https://api.ngrok.com/tunnel_sessions/ts_2NTVGmVw5yMzr02ZZzlX4VC6b6L"
},
"endpoint": {
"id": "ep_2NTVGqGpa5w2LExRzILEksu7FOa",
"uri": "https://api.ngrok.com/endpoints/ep_2NTVGqGpa5w2LExRzILEksu7FOa"
},
"forwards_to": "http://localhost:80"
},
{
"id": "tn_2NTVGiqsZQ8EGqJ7HgcysTvGPAN",
"public_url": "://:0",
"started_at": "2023-03-24T19:59:24Z",
"region": "us",
"tunnel_session": {
"id": "ts_2NTVGfpxBknN9h3rpLqPiV7NiAw",
"uri": "https://api.ngrok.com/tunnel_sessions/ts_2NTVGfpxBknN9h3rpLqPiV7NiAw"
},
"labels": {
"baz": "qux",
"foo": "bar"
},
"forwards_to": "http://localhost:80"
}
],
"uri": "https://api.ngrok.com/tunnels",
"next_page_uri": null
}

Get Tunnel

curl \
-X GET \
-H "Authorization: Bearer {API_KEY}" \
-H "Ngrok-Version: 2" \
https://api.ngrok.com/tunnels/tn_2NTVGqGpa5w2LExRzILEksu7FOa

Response

{
"id": "tn_2NTVGqGpa5w2LExRzILEksu7FOa",
"public_url": "https://b8ad9cf4eff6.ngrok.paid",
"started_at": "2023-03-24T19:59:25Z",
"proto": "https",
"region": "us",
"tunnel_session": {
"id": "ts_2NTVGmVw5yMzr02ZZzlX4VC6b6L",
"uri": "https://api.ngrok.com/tunnel_sessions/ts_2NTVGmVw5yMzr02ZZzlX4VC6b6L"
},
"endpoint": {
"id": "ep_2NTVGqGpa5w2LExRzILEksu7FOa",
"uri": "https://api.ngrok.com/endpoints/ep_2NTVGqGpa5w2LExRzILEksu7FOa"
},
"forwards_to": "http://localhost:80"
}

Tmux

· One min read

Open in Notion

Installing

bookmark

Script Examples

Start a main pane with other panes

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ +
+ +
+ main +
+ +
+ +
+ +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ a + b + c +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
tmux new-session -d -s sessionName; \
tmux split-window -v; \
tmux send-keys "echo a" Enter; \
tmux split-window -v; \
tmux send-keys "echo b" Enter; \
tmux split-window -v; \
tmux send-keys "echo c" Enter; \
tmux select-layout main-horizontal; \
tmux select-pane -t 0; \
tmux a;

Start there same size panes

tmux new-session -d -s sessionName; \
tmux send-keys "echo a" Enter; \
tmux split-window -v; \
tmux send-keys "echo b" Enter; \
tmux split-window -v; \
tmux send-keys "echo c" Enter; \
tmux select-layout even-horizontal; \
tmux a;

Others

# Layouts
tmux select-layout main-vertical
tmux select-layout main-horizontal
tmux select-layout even-vertical
tmux select-layout even-horizontal

# Enable pane border labels
tmux set pane-border-status top

#Enable Mouse
tmux set mouse on

Oh my tmux

https://github.com/gpakosz/.tmux

Do not forget to source your config file via:

tmux source-file ~/.tmux.conf.local

Git

· 10 min read

Open in Notion

Download and Installation

Quick start (most-used)

git status
git add .
git commit -m "message"
git pull --rebase
git push

Config

Identity

git config --global user.name "Bendy Zhang"
git config --global user.email "81795705@qq.com"
git config --global --list

Helpful defaults

git config --global init.defaultBranch main
git config --global pull.rebase true
git config --global rebase.autoStash true
git config --global fetch.prune true
git config --global rerere.enabled true

Ignore files globally

git config --global core.excludesfile ~/.gitignore_global
# edit ~/.gitignore_global

Workspace (working tree / index / HEAD)

  • Working tree (workspace): Files you see and edit in your filesystem.
  • Index / Staging area: What you get after git add; ready to be committed.
  • HEAD: The currently checked-out commit (usually the tip of the current branch).

Check workspace status

git status
git diff # workspace vs index
git diff --staged # index vs HEAD

Move changes between workspace and staging

git add <file>
git add -p
git restore --staged <file> # unstage

Discard workspace changes (dangerous)

git restore <file>
git restore .

Clean untracked files in workspace

git clean -n -d # dry run
git clean -fd # really delete

Temporarily save workspace changes (stash)

git stash
git stash pop

Repo basics

Init / clone

git init
git clone <repo>
git clone --depth 1 <repo> # shallow clone
git clone --filter=blob:none <repo> # partial clone (faster for big repos)

Remotes

git remote -v
git remote add origin <url>
git remote add upstream <url>
git remote set-url origin <new-url>
git remote rename origin old-origin
git remote remove upstream

Fetch / pull

git fetch --all --prune
git pull
git pull --rebase
git pull --rebase --autostash

Commit

Commit with empty message

git config --global alias.nccommit "commit -a --allow-empty-message -m ''"
git nccommit

Amend / change commit info

git commit --amend --no-edit
git commit --amend --author="Bendy Zhang <zbatbndy.net>" --no-edit

git commit --amend --date="Sun Aug 7 13:05 2022 +0800" --no-edit
GIT_COMMITTER_DATE="Sun Aug 7 14:10 2022 +0800" git commit --amend --no-edit

Fix the last commit without changing message (add forgotten files)

git add .
git commit --amend --no-edit

Create commits without committing staged changes (split work)

git add -p # stage interactively
git commit -m "..."

Branch

List / switch / create

git branch
git branch -a
git switch <branch>
git switch -c <new-branch>
# old style:
git checkout <branch>
git checkout -b <new-branch>

New branch from remote

git checkout -b localBranch origin/remote_branch

Push changes to new branch

git checkout -b newBranch
git push -u origin newBranch # -u is short for `-set-upstream`

Bind local branch with remote branch

git branch --set-upstream-to=origin/remote_branch your_branch

Rename branch

git branch -m oldName newName
git branch -m newName # rename current branch
git push origin :oldName newName
git push -u origin newName

Delete branches

git branch -d <branch> # safe delete (only if merged)
git branch -D <branch> # force delete

git push origin --delete <branch>

Remove multiple branches

git branch -d `git branch --list '3.2.*'`

Reset local branch to remote branch

git fetch origin && git reset --hard origin/master

Merge & rebase

git merge master
git merge master --squash # merge master branch to current branch and keep changes without commits
git rebase master
git rebase --interactive --root # squash all of your commits down to the root commit

Cherry-pick

git cherry-pick <commitid>
# if conflicts, resolve then:
git add . && git cherry-pick --continue

git cherry-pick A..B # The commit A should be older than B which will not include A. If requires A, please use `A^~B`

Diff

git diff # working tree vs index
git diff --staged # index vs HEAD
git diff HEAD # working tree + index vs HEAD
git diff <a>..<b>
git diff <a>...<b> # changes on branch b since it diverged from a

Undoing things / recovery

Unstage / discard

git reset # unstage all
git reset HEAD <file>
git restore --staged <file>
git checkout -- <file> # discard changes in working directory (legacy)
git restore <file> # discard changes (recommended)

Reset commits

git reset --soft HEAD~1 # undo commit, keep changes staged
git reset --mixed HEAD~1 # undo commit, keep changes unstaged (default)
git reset --hard HEAD~1 # undo commit and discard changes

Revert (safe for shared branches)

git revert <commit>
git revert <old>..<new> # revert a range (creates multiple commits)

Recover lost work

git reflog
git fsck --lost-found

Remove untracked files & directories

git clean -n -d # dry run
git clean -fd # remove untracked files + dirs
git clean -fX # remove ignored files
git clean -fx # remove ignored and non-ignored files

Stash usage

git stash // stash current changes
git stash pop // pop last stash and remove from history
git stash save 'message' // stash current changes with message
git stash list // show all stashes
git stash apply <id> // apply <id> stash
git stash drop <id> // delete one stash
git stash clear // delete all stashes

Log

$ git log --follow --pretty=oneline things/text.txt view log of the renamed file

Common log views

git log --oneline --decorate --graph --all
git show <commit>
git show <commit>:<path/to/file>
git blame <file>

Search history

git log -S "searchText" # pickaxe
git log -G "regex" # regex search in diffs
git log -- path/to/file # file history
git log --since="2024-01-01" --until="2024-02-01"
git log --author="zb@bndy.net" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -

Output:

added lines: 545140, removed lines: 1152979, total lines: -607839
git log --pretty=format:%h,%an,%ae,%ad,%s --name-only

Output:

acb533d,Bendy Zhang,zb@bndy.net,Sun Jul 12 16:17:14 2020 +0800,commit message
.gitignore
README.md
  • -since=<date>, --after=<date> Show commits more recent than a specific date.
  • -until=<date>, --before=<date> Show commits older than a specific date.
  • --author=<pattern>, --committer=<pattern> Limit the commits output to ones with author/committer header lines that match the specified pattern (regular expression). With more than one -author=<pattern>, commits whose author matches any of the given patterns are chosen (similarly for multiple -committer=<pattern>).

Pretty formats

Tag

# create tag
git tag -a <tagname> -m '<tagcomment>'

# show tags
git tag
git tag -n

# show specified tag
git show <tagname>

# push to remote
git push origin <tagname>

# push all tags to remote
git push origin --tags

# checkout tag
git checkout <tagname>

# delete tag
git tag -d <tagname>

# delete remote tag
git push origin <tagname>
git push --delete origin tagname

Clone subfolder (sparse-checkout)

By following ways, you will get the folder fastest that you want.

git clone -n --depth=1 --filter=blob:none git@github.com:bndynet/bndynet.git
cd bndynet
git sparse-checkout set "/_data/*" --no-cone
git checkout

Or

git init
git sparse-checkout init --no-cone
git sparse-checkout set "/_data/*" --no-cone
git remote add origin git@github.com:bndynet/bndynet.git
git config core.sparsecheckout true
echo "partialclonefilter = blob:none" >> .git/config
git pull --depth=1 origin master

Subtree

# with squash, this repo history commits will not be merged into parent repo
git subtree add --prefix=subfolder https://github.com/subrepo.git master --squash
git subtree push --prefix=dist https://github.com/subrepo.git gh-pages

Submodule

git submodule add submodule-repo path
git submodule update --init --recursive
git submodule status
git submodule sync --recursive
git rm submodule-name
git rm submodule-name --cached
# update submodule to master
cd submodule_folder
git checkout master
git pull
cd ../
git add .
git commit -m ''

Workflow (fork / PR)

with Git: Fork, Branching, Commits, and Pull Request

  1. Fork a repo.

  2. Clone the forked project to your local machine:

    git clone git@github.com:USERNAME/<forked_repo>

  3. Configure remotes:

    git remote add upstream git://github.com/<origin_repo>

  4. Create a branch for new feature:

    git checkout -b my-new-branch

  5. Develop on my-new-branch branch only, but Do not merge my-new-branch branch to the your master (as it should stay equal to upstream master)!!

  6. Commit changes to my-new-branch branch:

    git add .
    git commit -m "commit message"
  7. Push branch to GitHub, to allow your mentor to review your code:

    $ git push origin my-new-branch

  8. Repeat steps 5-7 till development is complete.

  9. Fetch upstream changes that were done by other contributors:

    $ git fetch upstream

  10. Update local master:

git checkout master
git pull upstream master

ATTENTION: any time you lost of track of your code – launch “gitk —all” in source folder, UI application come up that will show all branches and history in pretty view, explanation.

  1. Rebase my-new-branch branch on top of the upstream master:
git checkout my-new-branch
git rebase master
  1. In the process of the rebase, it may discover conflicts. In that case it will stop and allow you to fix the conflicts. After fixing conflicts, use git add . to update the index with those contents, and then just run:
git rebase --continue
  1. Push branch to GitHub (with all your final changes and actual code):

We forcing changes to your issue branch(our sand box) is not common branch, and rebasing means recreation of commits so no way to push without force. NEVER force to common branch.

git push origin my-new-branch --force
  1. Created build for testing and send it to any mentor for testing.
  2. Only after all testing is done – Send a Pull Request.

Attention: Please recheck that in your pull request you send only your changes, and no other changes!! Check it by command: git diff my-new-branch upstream/master

QA / troubleshooting

.gitignore can not ignore a file

Need run following command to remove all files from the repository and add them back.

git rm -rf --cached .
git add .

Fix line endings issues (CRLF/LF)

git config --global core.autocrlf input # mac/linux
git config --global core.autocrlf true # windows (typical)
git config --global core.safecrlf warn

Remove file from history (BFG / filter-repo)

# Prefer git-filter-repo (recommended modern tool)
# After rewriting history, force-push is required.
git filter-repo --path <path> --invert-paths
git push --force --all
git push --force --tags

Common alias

git config --global alias.s "status"
git config --global alias.a "\!git add . && git status"
git config --global alias.au "\!git add -u . && git status"
git config --global alias.aa "\!git add . && git add -u . && git status"
git config --global alias.b "branch"
git config --global alias.c "commit"
git config --global alias.cm "commit -m"
git config --global alias.ca "commit --amend"
git config --global alias.ac "\!git add . && git commit"
git config --global alias.acm "\!git add . && git commit -m"
git config --global alias.l "log --graph --all --pretty=format:'%C(yellow)%h%C(cyan)%d%Creset %s %C(white)- %an, %ar%Creset'"
git config --global alias.ll "log --stat --abbrev-commit"
git config --global alias.lg "log --color --graph --pretty=format:'%C(bold white)%h%Creset -%C(bold green)%d%Creset %s %C(bold green)(%cr)%Creset %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"
git config --global alias.llg "log --color --graph --pretty=format:'%C(bold white)%H %d%Creset%n%s%n%+b%C(bold blue)%an <%ae>%Creset %C(bold green)%cr (%ci)' --abbrev-commit"
git config --global alias.d "diff"
git config --global alias.master "checkout master"
git config --global alias.main "checkout main"
git config --global alias.alias "\!git config --list | grep 'alias\.' | sed 's/alias\.\([^=]*\)=\(.*\)/\1\ => \2/' | sort"
git config --global alias.hi "\!echo 'Hello World!'"
git config --global --unset alias.hi

IntelliJ IDEA

· One min read

Open in Notion

  • Preferences → Build, Execute, Deployment → Compiler → [x] Build project automatically

  • Command + Shift + A (on MacOS) → Type "Registry" → [x] compiler.automake.allow.when.app.running

  • Add dependency to pom.xml

    \<dependency\>
    \<groupId\>org.springframework.boot\</groupId\>
    \<artifactId\>spring-boot-devtools\</artifactId\>
    \<version\>2.5.0\</version\>
    \<optional\>true\</optional\>
    \</dependency\>

Keycloak

· One min read

Open in Notion

docker run -p 9900:8080 -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin quay.io/keycloak/keycloak:13.0.1

New Client

  1. Add client: Client ID: foo, Access Type: confidential
  2. Go to Credentials tab, and get client secret.

OAuth2 Endpoints

Configurations for getting access token

Vim

· One min read

Open in Notion

**:s/foo/bar/g**Find each occurrence of 'foo' (in the current line only), and replace it with 'bar'.

**:%s/foo/bar/g**Find each occurrence of 'foo' (in all lines), and replace it with 'bar'.

**:%s/foo/bar/gc**Change each 'foo' to 'bar', but ask for confirmation first.

**:%s/\<foo\>/bar/gc**Change only whole words exactly matching 'foo' to 'bar'; ask for confirmation.

**:%s/foo/bar/gci**Change each 'foo' (case insensitive due to the i flag) to 'bar'; ask for confirmation.:%s/foo\c/bar/gc is the same because \c makes the search case insensitive.This may be wanted after using :set noignorecase to make searches case sensitive (the default).

**:%s/foo/bar/gcI**Change each 'foo' (case sensitive due to the I flag) to 'bar'; ask for confirmation.:%s/foo\C/bar/gc is the same because \C makes the search case sensitive.This may be wanted after using :set ignorecase to make searches case insensitive.

nodejs and npm

· 2 min read

Open in Notion

Installation on CentOS 7

curl -sL https://rpm.nodesource.com/setup_12.x | sudo bash -
sudo yum install nodejs
node --version

Installation via NVM

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
nvm --version. # restart terminal
nvm install node

nvm install --lts
nvm install 8.12.0
nvm ls
nvm use 10.13.0
nvm alias default 10.13.0
nvm ls-remote

Mirrors

npm config set registry https://registry.npm.taobao.org # or
npm install -g cnpm --registry=https://registry.npm.taobao.org

or via alias:

alias cnpm="npm --registry=https://registry.npm.taobao.org \
--cache=$HOME/.npm/.cache/cnpm \
--disturl=https://npm.taobao.org/dist \
--userconfig=$HOME/.cnpmrc"

# Or alias it in .bashrc or .zshrc
$ echo '\n#alias for cnpm\nalias cnpm="npm --registry=https://registry.npm.taobao.org \
--cache=$HOME/.npm/.cache/cnpm \
--disturl=https://npm.taobao.org/dist \
--userconfig=$HOME/.cnpmrc"' >> ~/.zshrc && source ~/.zshrc

Install development tools

To be able to build native modules from npm we will need to install the development tools and libraries:

sudo yum install gcc-c++ make

NPM commands

npm start --prefix subapp # run `start` in subapp folder

# publish your package to npmjs
npm adduser
npm publish

Test package locally

Run the following commands to generate a package.

npm run build
npm pack --pack-destination ~ # it will pack all files under `files` section in package.json file

Above will output a file.

~/your-package-1.0.0.tgz

Install this package on your other project.

"dependencies": {
"your-package": "file:~/your-package-1.0.0.tgz"
}

and then install.

npm install

Alternatively, you can use the following way to debug in real-time.

cd ~/your-pkg-source
npm link // creates global link

cd ~/your-work
npm link your-pkg

You may also shortcut the two steps in one. For example, to do the above use-case in a shorter way:

cd ~/projects/your-project # go into the dir of your main project
npm link ../your-pkg-dir # link the dir of your dependency

Update all package versions

npm install -g npm-check-updates # global to install the tool
ncu # check updates
ncu -u # update the versions for all packages in package.json file
npm install

Microsoft Terminal

· One min read

Open in Notion

Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
Set-ExecutionPolicy -Scope CurrentUser Bypass

oh-my-posh

Install from local

  1. Download the nupkg file from https://www.powershellgallery.com/packages?q=oh-my-posh

  2. Run below command to register repository and install

    Register-PSRepository -Name 'oh-my-posh' -SourceLocation 'your-nupkg-folder'
    Install-Module oh-my-posh -Scope CurrentUser -Repository oh-my-posh
    Import-Module oh-my-posh

Commands

Get-PoshThemes
Get-PoshThemes -list

iTerm2 on MacOS

· 4 min read

Open in Notion

iTerm2

brew cask install iterm2

Or, if you do not have homebrew (you should ;)): Download and install iTerm2

iTerm2 has better color fidelity than the built in Terminal, so your themes will look better.

Get the iTerm color settings

Just save it somewhere and open the file(s). The color settings will be imported into iTerm2. Apply them in iTerm through iTerm → preferences → profiles → colors → load presets. You can create a different profile other than Default if you wish to do so.

Oh My Zsh

More info here: https://github.com/robbyrussell/oh-my-zsh

Install with curl

sh -c “$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)”

When the installation is done, edit ~/.zshrc and set ZSH_THEME="agnoster"

Powerlevel10k

Note that P9k had a substantial impact on CLI UX, and its legacy is now continued by P10k.

https://github.com/romkatv/powerlevel10k

Fonts with icon

Patched Fonts: https://github.com/ryanoasis/nerd-fonts

Set this font in iTerm2 (14px is my personal preference) (iTerm → Preferences → Profiles → Text → Change Font).

Restart iTerm2 for all changes to take effect.

The font SourceCodePro+Powerline+Awesome+Regular will be better to show the icons include git.

Further tweaking

Things like

  • auto suggestions
  • word jumping with arrow keys / natural text editing
  • shorter prompt style
  • syntax highlighting
  • visual studio code config

can be found in the section below.

Auto suggestions (for Oh My Zsh)

Just follow these steps: https://github.com/zsh-users/zsh-autosuggestions/blob/master/INSTALL.md#oh-my-zsh

If the auto suggestions do not appear to show, it could be a problem with your color scheme. Under “iTerm → Preferences → Profiles → Colors tab”, check the value of Black Bright, that is the color your auto suggestions will have. It will be displayed on top of the Background color. If there is not enough contrast between the two, you won’t see the suggestions even if they’re actually there..

Enable word jumps and word deletion, aka natural text selection

By default, word jumps (option + → or ←) and word deletions (option + backspace) do not work. To enable these, go to “iTerm → Preferences → Profiles → Keys → Load Preset… → Natural Text Editing → Boom! Head explodes”

Custom prompt styles

By default, your prompt will now show “user@hostname” in the prompt. This will make your prompt rather bloated. Optionally set DEFAULT_USER in ~/.zshrc to your regular username (these must match) to hide the “user@hostname” info when you’re logged in as yourself on your local machine. You can get your exact username value by executing whoami in the terminal.

For further customisation of your prompt, you can follow a great guide here: https://code.tutsplus.com/tutorials/how-to-customize-your-command-prompt–net-24083

Syntax highlighting

brew install zsh-syntax-highlighting

If you do not have or do not like homebrew, follow the installation instructions instead.

After installation through homebrew, add

source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

to the end of your .zshrc file. After that, it’s best to restart your terminal. Sourcing your ~/.zshrc does not seem to work well with this plugin.

Visual Studio Code config

Installing a patched font will mess up the integrated terminal in VS Code unless you use the proper settings. You’ll need to go to settings (CMD + ,) and add or edit the following values:

  • for Source Code Pro: "terminal.integrated.fontFamily": "Source Code Pro for Powerline"
  • for Meslo: "terminal.integrated.fontFamily": "Meslo LG M for Powerline"
  • for other fonts you’ll need to check the font name in Font Book.

You can also set the fontsize e.g.: "terminal.integrated.fontSize": 14

Font with icons

Copy patched fonts files to ~/.fonts and install all fonts, and set the installed font for iTerm2. Then add below code to first line of ~/.zshrc. Restart your iTerm2.

source ~/.fonts/*.sh POWERLEVEL9K_MODE="awesome-patched"