Awesome
Learning Bash Scripts
Just one of the things I'm learning. https://github.com/hchiam/learning
A nice article on key things to know: https://www.joshwcomeau.com/javascript/terminal-for-js-devs (my own summary notes are below)
To enable running a script upon click, you need to edit permissions. For example, for chrome-incognito-shortcut.app
:
chmod 744 chrome-incognito-shortcut.app
Related Repos
https://github.com/hchiam/learning-powershell
https://github.com/hchiam/learning-dependency-cruiser
https://github.com/hchiam/learning-https-server
>
and <
and >>
and <<
in bash
>
= redirect output = "(over)write to", e.g.:echo 'hello' > file.txt
<
= redirect input = "read", e.g.:cat < file.txt
(same as justcat file.txt
)>>
= append to end of file, e.g.:echo 'new last line of text' >> file.txt
cat << EOF >> file.txt
= "start multiline input session that ends when you typeEOF
and hit Enter" (but personally,nano
seems to do this simpler)
Useful git
commands
-
git commit --amend
-
git reset HEAD~1
to "un-commit" -
git pull origin dev
(if you want to merge commits intodev
branch into your local branch) and then type:qa
and hit enter. Now you cangit push
. -
git tag <name-of-tag>; git push --tags
(and then you can draft a new release on the same tag name to add details to it on GitHub)
git update-index --skip-worktree your-file-to-ignore-future-revisions-for.txt
git update-index --no-skip-worktree your-file-to-start-watching-revisions-for.txt
From https://www.smashingmagazine.com/make-life-easier-when-using-git/ :
git status -sb
- git hooks
- Try using git
bisect
with "binary search" automation to find the breaking change
git bisect start
git bisect good c5ba734 # if c5ba734 is a commit without the bug
git bisect bad 6c093f4 #if 6c093f4 is a commit with the bug
# run test: git bisect run ./test-bug (or: git bisect run jest)
git bisect bad # if the current commit has the bug
git bisect good # if current commit does not have the bug
# (repeats until find the first commit with the bug)
git bisect reset # or: git bisect reset HEAD (or: git bisect reset <commit-id>)
git rebase -i --exec "yarn test" d294ae9 # test all commits from d294ae9 to HEAD, until hit first failing commit
From https://dev.to/g_abud/advanced-git-reference-1o9j#git-commands :
-
git rm --cached <file-name>
to stop tracking<file-name>
in version control -
git diff --staged
-
git diff branch1..branch2
-
git commit --amend
to add more changes to the last commit -
git cherry-pick <commit-sha>
to bring in a commit from a different branch -
git checkout <branch-name> <file-name>
to bring in a file from a differnt branch -
git reset <commit-sha>
-
git reset --hard HEAD~1
= undo last commit and rewrite history (thengit push -f
to push rewritten history) -
git reset --hard HEAD~n # where n is the last n commits
= undo last n commit and rewrite history (thengit push -f
to push rewritten history) -
git reset --hard <commit-sha>
= undo comments and rewrite history (thengit push -f
to push rewritten history) -
--soft
= uncommit but still staged;--mixed
= uncommit+unstage, but changes kept locally;--hard
= uncommit+unstage+delete changes -
git stash
to save your changes to top of stash stack (add-u
to stash untracked files as well) -
git stash list
to see what's in your stash -
git stash save "message to go along with changes"
-
git stash pop
From https://www.youtube.com/watch?v=ecK3EnyGD8o :
-
git stash save nameforlater
- and then later
git stash list
(note the index) - and then
git stash apply <index>
to use<index>
- and then later
-
git branch -M main
to rename branch to main, and thengit push -u origin main
(more info) -
git log --graph --oneline --decorate
for a pared-down git log -
lots more git tips at a Fireship.io YouTube video
How to revert changes of a git push:
This shows the commit hash: (looks like l0ngStr1ng0fL3t7er5AndNum83rz
)
git log
then use the hash of the commit you want to revert: (and when you're in the in-CLI editor, type :qa
and then hit Enter)
git revert l0ngStr1ng0fL3t7er5AndNum83rz
How to create a branch with no history
https://stackoverflow.com/a/34954852
git checkout --orphan <name_you_choose_for_orphan_branch>
git commit
git push <remote-name> <branch-name>
Random Notes
Get file info like edit date: stat filename
Example of script used by Travis CI npm run build
to copy files to /public
folder: copy-to-public-folder.sh
Example of script used to generate and insert a package SemVer number for a JS file: package.sh
Exit/quit:
exit
Get list of globally installed packages
yarn global list
npm list -g --depth 0
for just top-level
npm list -g browser-sync
or a specific package (this example checks if browser-sync
is installed globally)
npm scripts that accept parameters
Examples I set up in package.json:
{
"test": "tsc $npm_config_name.ts --lib es6,dom && node $npm_config_name.js",
"demo": "echo \"HELLO $npm_config_file_name.ts GOODBYE (\\$npm_config_whatever will match --whatever=...)\""
}
So you can do this:
npm run demo --file_name=someFileName
# prints out: HELLO someFileName.ts GOODBYE [...]
and:
npm run test --name=sameFileName
# will run: tsc sameFileName.ts --lib es6,dom && node sameFileName.js
Install package without dynamic version (i.e. without caret ^)
npm install <package-name> --save-exact
so you get 1.2.3
instead of ^1.2.3
in your package.json
Actually upgrade node
You can upgrade to the latest npm
with npm install -g npm@latest
But installing node manually doesn't always work, and using n
or clean cache didn't seem to work for me. https://stackoverflow.com/questions/23940172/not-seeing-latest-version-when-updating-node-js-via-installer-msi-windows-7/31229369#31229369
For Mac/OSX, try this first:
brew install node
Otherwise this:
nvm install node --lts
node -v
If you accidentally installed the latest but want to downgrade to the last stable instead:
nvm install 14.15.0 # whatever the latest unstable versioni on https://nodejs.org/en/
nvm use 14.15.0
switching from bash
to zsh
: (and back)
https://github.com/hchiam/learning-zsh
Format whole directory instead of waiting for each file save in VSCode
prettier --write .
or
npx prettier --write .
how to sync forked repo with the original repo
https://stackoverflow.com/questions/7244321/how-do-i-update-or-sync-a-forked-repository-on-github
git remote add upstream https://github.com/<owner>/<repo>.git
git fetch upstream
gh
CLI commands
https://github.com/hchiam/learning-gh
setting up GitHub PATs (Personal Access Tokens)
For gh
CLI: copy your PAT, run gh auth login
, and paste your PAT when prompted to.
For npm pkg: copy your PAT, open ~/.npmrc
, and paste your PAT to be the "TOKEN" part of a string //npm.pkg.github.com/:_authToken=TOKEN
in that ~/.npmrc
file.
quickly set up a basic server to serve index.html (or whatever's in the current folder)
python3 -m http.server 8000
or set up shortcut srv
in .bash_profile:
alias srv='python3 -m http.server 8000'
https://www.joshwcomeau.com/javascript/terminal-for-js-devs
- Bash ≈ Zsh
- the terminal is kinda like the console log in browser dev tools, but can switch between shell languages
echo
≈console.log
$
notation is just for start of promptpwd
= print the current working directory folder pathls
= list current folder files/subfoldersls -la
=-l
ong data too like updated dates, and-a
ll files including hidden files/folders
cd
= change directory~
= home folder, like/Users/yourusernamehere
.
= current folder..
= parent folder- use the tab key to auto-complete!
rm
= removerm -r foldername
= removefoldername
and-r
ecursively inside it toorm -rf foldername
= do that, but-f
orce remove regardless of permissions
man somecommand
= show a printout of theman
ual for somecommand- hit
q
toq
uit (arrow keys to scroll)
- hit
ping 8.8.8.8
= continuously check latency against IP address 8.8.8.8 (Google's DNS server), which can be used to continuously check if the server is online (Ctrl
+c
orCtrl
+d
to stop)-
= "previous"cd -
= toggle to previous foldergit checkout -
= toggle to previously-checked-out branch
alias hi='echo "Hello world!"'
- now you can just do
hi
to get a printout ofHello world!
- now you can just do
command1 && command2
= do command1 and then to command2- stuck in Vi/Vim?
Ctrl
+c
won't work! hitEscape
->:
->q!
->Enter
- common web dev tasks: https://www.joshwcomeau.com/javascript/terminal-for-js-devs/#common-development-tasks
- reinstall dependencies:
rm -rf node_modules; npm install;
- reinstall dependencies:
more to learn for unix:
- https://www.tutorialspoint.com/unix/unix-special-variables.htm
- https://www.tutorialspoint.com/unix/unix-using-arrays.htm
- https://www.tutorialspoint.com/unix/unix-basic-operators.htm
- https://www.tutorialspoint.com/unix/unix-decision-making.htm
- https://www.tutorialspoint.com/unix/unix-shell-loops.htm
- https://www.tutorialspoint.com/unix/unix-quoting-mechanisms.htm for getting variables and special characters parsed or not parsed in strings
- https://www.tutorialspoint.com/unix/unix-io-redirections.htm - there's more to it than just > file
- https://www.tutorialspoint.com/unix/unix-shell-functions.htm
- https://www.tutorialspoint.com/unix/unix-manpage-help.htm
- https://www.tutorialspoint.com/unix/unix-useful-commands.htm
5 linux command tricks from fireship.io
https://www.youtube.com/shorts/fwBIZRq-vzY
mkdir
, but with multiple sibling folders at the same time:mkdir folder
then:mkdir -p folder/{sibling1,sibling2}/{commonA,b,c}
- gives you this:
/folder /sibling1 /commonA /b /c /sibling2 /commonA /b /c
cd
, but back to where you last were instead ofcd ..
a bunch of times:cd ./somewhere/deeply/nested
then to get back to./
:cd -
touch
, but create a bunch of files with names with a pattern:-
=touch file{1..10}.txt
file1.txt file2.txt file3.txt file4.txt file5.txt file6.txt file7.txt file8.txt file9.txt file10.txt
- (note: the one-liner command doesn't pad with 0s on my machine despite
{01..10}
) - two 0-padding alternatives that worked for me: (
%02g
= zero-padding of width 2 of general number format)-
touch $(seq -f "file%02g.txt" 1 10)
- or this: (the space after
txt
matters for this one!)touch $(printf "file%02g.txt " {1..10})`
-
-
tail err.log
to read a file, buttail -f err.log
to "follow" changes- (note: doesn't seem to print out the right contents if i edit in another tab with
nano
)
- (note: doesn't seem to print out the right contents if i edit in another tab with
history
, but to get the last 5 commands:-
history 5
-