Useful Commands

Since I keep forgetting some good commands, which I have to re-search all the timem, I'll write them down in here

This post will be updated from time to time, so everything is in one place

Git

Change remote URL

Good when remote has moved, renamed or just want to replace https with ssh

Signature:

git remote set-url REMOTE_NAME REMOTE_PATH

Usage:

git remote set-url origin [email protected]:group/project/example-com.git

Change (current) branch base branch

source

Good in cases where you created new branch based on wrong branch.

For example wanted to create new branch based on master but accidentally made it based on demo and ended up with history like this:

(commit 1) -> master
                \__ (commit 2) -> demo
                                   \__ (commit 3) -> new_branch

But that’s the history you are after:

(commit 1) -> master
                |__ (commit 2) -> (commit 3) -> demo
                \__ (commit 4) -> (commit 5) -> new_branch

Use rebase –onto for that

Signature:

git rebase --onto newBase oldBase feature/branch

Usage for current example:

git rebase --onto master demo new_branch

Basically, you take all the commits from after demo up to new_branch, and rebase them onto the master commit.

Reset only file permission changes

source

Good to revert all file permission changes that have been accidentally done.

Most common use case is when sharing codebase between Windows and Linux. After editing files on Windows file permissions tend to be changed in Linux

Only works from project root!

Git keeps track of file permission and exposes permission changes when creating patches using git diff -p.

So all we need is:
  1. Create a reverse patch
  2. Include only the permission changes
  3. Apply the patch to our working copy

As a one-liner:

git diff -p -R --no-color \
    | grep -E "^(diff|(old|new) mode)" --color=never  \
    | git apply

You can also add it as an alias to your git config…

git config --global --add alias.permission-reset '!git diff -p -R --no-color | grep -E "^(diff|(old|new) mode)" --color=never | git apply'

…and you can invoke it via:

git permission-reset

Note, if your shell is bash, make sure to use ' instead of " quotes around the !git, otherwise it gets substituted with the last git command you ran.

Git make local branch exactly same as remote

source

Good to clean up some mess that you have accidentally caused on your local branch

I most cases first command is enough

First, reset to the previously fetched HEAD of the corresponding upstream branch:

git reset --hard @{u}

The advantage of specifying @{u} or its verbose form @{upstream} is that the name of the remote repo and branch don’t have to be explicitly specified.

Next, as needed, remove untracked files, optionally also with -x:

git clean -df

Finally, as needed, get the latest changes:

git pull

All in one in single line

git reset --hard @{u} && git clean -df && git pull

Git change author history

Cant remember source

Good in cases, when you accidentally set up wrong email / name in Git config, do some commits with wrong data and then want to fix it.

Note! You have to force push after running it. git push -f may result broken history and loss of code, so be careful
git filter-branch --env-filter '
WRONG_EMAIL="[email protected]"
NEW_NAME="New Name Value"
NEW_EMAIL="[email protected]"

if [ "$GIT_COMMITTER_EMAIL" = "$WRONG_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$NEW_NAME"
    export GIT_COMMITTER_EMAIL="$NEW_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$WRONG_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$NEW_NAME"
    export GIT_AUTHOR_EMAIL="$NEW_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

NPM

Run executable files directly from node_modules

It is always better to have dependencies as dev dependencies than install them globally.

There are two ways to execute project dependencies like Mocha, Nyc, ESLint etc.

Let’s use nyc + mocha test running for example

In package.json script it would be like this:

{
  "scripts": {
    "test": "nyc mocha ./test.js"
  }
}

First option - use full paths:

./node_modules/.bin/nyc ./node_modules/.bin/mocha ./test.js

Second option - use npx

npx nyc mocha ./test.js

SSH

Tunnel port from remote to local

Good way to get port from remote server to local. Especially handy when accessing databases on remote that are limited to be accessed from localhost only

Signature:

ssh -L LOCAL_PORT:localhost:REMOTE_PORT USER@SERVER

Getting MySQL port 3306 from remote to local:

ssh -L 3306:localhost:3306 [email protected]

Getting RethinkDB management UI from remote server port 8080 to local port 8085 with ssh alias

ssh -L 8085:localhost:8080 rethinkdb_server

Copy key to remote (ssh-copy-id)

It’s safer and more convenient to use ssh key to log into server compared to password

Signature:

ssh-copy-id USER@SERVER

To copy default ~/.ssh/id_rsa.pub to remote server:

ssh-copy-id [email protected]

To copy some other keyfile to remote server:

ssh-copy-id -i ~/.ssh/awskey.pem.pub [email protected]