| Setup | CLI cheatsheet
CLI cheatsheet


This page lists some of my frequently used CLI commands.


Delete a full line using Control + K

Capistrano deploy

From https://jeromezng.com/work/operating/engineering/development-guides/how-to-deploy-a-rails-app-using-digital-ocean

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
bundle exec cap production deploy

Creating shell scripts

I frequently use the capistrano deploy commands listed above. Instead of having to constantly open my notes and copy and paste these commands, I decided to wrap it into a simple shell script which can be called from my CLI.

Open nano ~/.zshrc and paste in your script. In this case, I made a function called ssh_cap_deploy to wrap my commands.

function ssh_cap_deploy() {
  eval "$(ssh-agent -s)"
  ssh-add ~/.ssh/id_ed25519
  bundle exec cap production deploy

Now I can simply use a single ssh_and_deploy command instead of pasting in three commands.


Modifying commit messages

$ GIT_EDITOR=nano git rebase -i HEAD~1

## In the editor, replace pick with reword for the commit to modify

reword cea1fb88a fix: update dependency verdaccio to ^4.3.3
pick aa540c364 fix: update dependency webpack-dev-server to ^3.8.2

## Modify the commit message
$ git push --force

Updating the location of a git repository

When the location of a git repository is moved or renamed, you also need to update your local repository to reference the new repository location.

In the example below, I move/rename an application from git@code.jeromezng.com:my-projects/sandbox/trello-clone.git to git@code.jeromezng.com:my-projects/sandbox/rails-vuejs-trello-clone.git

Navigate to the repository locally and list out the current remote references

git remote -v

You should see the current remote paths listed:

origin  git@code.jeromezng.com:my-projects/sandbox/trello-clone.git (fetch)
origin  git@code.jeromezng.com:my-projects/sandbox/trello-clone.git (push)

Add the updated remote path

git remote set-url origin --add git@code.jeromezng.com:my-projects/sandbox/rails-vuejs-trello-clone.git

Delete the old remote paths

git remote set-url origin --delete git@code.jeromezng.com:my-projects/sandbox/trello-clone.git

Check that the remote paths are updated:

git remote -v
origin  git@code.jeromezng.com:my-projects/sandbox/rails-vuejs-trello-clone.git (fetch)
origin  git@code.jeromezng.com:my-projects/sandbox/rails-vuejs-trello-clone.git (push)

Make a new commit and git push to test that this fully works.

Fetching changes from an upstream repo

Clone the original repo:

git clone git@code.jeromezng.com:my-projects/sandbox/upstream_app.git new_app

Check the remote path for thte git repository:

cd new_app
git remote show origin

You'll see something that looks like this:

Fetch URL: https://code.jeromezng.com/my-projects/sandbox/upstream_app.git
Push  URL: https://code.jeromezng.com/my-projects/sandbox/upstream_app.git
HEAD branch: main

Rename the origin path to upstream_app or another similar name

git remote rename origin upstream_app

Create a new blank project in GitLab / Github and set the path of the new repo as a remote origin

git remote add origin git@code.jeromezng.com:my-projects/sandbox/new_app.git

Push your application into the newly created blank project.

git push -u origin main

Confirm you have both upstream_app and origin showing the correct paths

git remote show upstream_app
git remote show origin

Fetch new changes from the upstream_app

git fetch upstream_app

Merge in changes from upstreamapp into newapp and resolve conflicts when needed.

git merge upstream_app/main

Diff two repos

In the first repo, add the second repo as a remote source

git remote add -f b git@gitlab.com:group_name/repo_name.git
git remote update

Compare the two repos

git diff main remotes/b/main

For a summary use

git diff-tree main remotes/b/main --compact-summary

To remove the remote source

git remote rm b

Update git config

List your git config settings

git config --list

Update your name and email

git config --global user.name "YOUR_NAME"
git config --global user.email "YOUR_EMAIL"

Dump a Postgres database

Dump a database into a file

pg_dump -U username -h host -p port -d database -W | gzip > database_name.gz

Create a database

psql postgres
psql=# create database database_name with owner = username;
psql=# \q

Extract database from file

gunzip -c database_name.gz | psql database_name

Copy a file from server to local

scp username@ local_file

Kill Rails

Find the process which is running on your rails port lsof -wni tcp:3000

ruby    53543 jeromezng   10u  IPv4       0t0  TCP (LISTEN)
ruby    53543 jeromezng   11u  IPv6       0t0  TCP [::1]:hbci (LISTEN)

Kill the PID by running kill -9 53543