Basic bash, git, npm and yarn commands, as well as a little about package.json and semver

Good day, friends!



Here's a quick cheat sheet for basic bash, git, npm, yarn, package.json, and semver commands.



Legend: [dir-name] - means the name of the directory, | - means "or".



I recommend entering each command in the terminal and carefully studying the output, so you will quickly remember them and determine which commands you need and which ones you don't.



I apologize for any possible errors and typographical errors. I will be glad to any comments and suggestions.



Without further preface.



Table of contents:





bash



bash is a command line tool that allows you to do some common things.



Installation: In my case, bash was installed along with git.



Reference:



help


Command history:



history


Terminal cleanup:



clear


Exiting the terminal:



exit


Directory creation:



// make directory
mkdir [dir-name]
// 
mkdir my-app
//  
mkdir -p {dir1,dir2}
//   
mkdir -p my-app/{css,js}


Change directory:



// change directory
cd [dir-name]
// 
cd my-app
//   
cd !$
//  
cd ..
//    
cd ../..
//  
cd -
//  
cd ~


The path to the current directory:



// print work directory
pwd


List of files:



// list
ls
//   
ls -a | -f
//  
// ,  
ls -l


File creation:



touch [file-name]
// 
touch index.html
//  
touch my-app/{index.html,css/style.css,js/script.js}


File contents:



cat [file-name]
// 
cat index.html
//     
cat [file-name] | sort | uniq
//  
less [file-name] // q - exit
// n    
head -50 [file-name]
// n    
tail -50 [file-name]
//  
grep [string] [file-name]

//     
unzip [achive-name]

//  
file [file-name]


Copying, moving and deleting a file:



// copy
cp [file1] [file2]

// move
mv [file1] [file2]
// 
//        
mv [dir1]/*.* [dir2]

// remove
rm [file-name]
//   
rmdir [dir-name]
//   
rm -r [dir-name]
// 
rm -rf [dir-name]


Output to the terminal of the line:



echo [string]
// 
echo hello
//    
echo hello > greet.txt
//    
echo hello >> greet.txt


File upload:



wget [url]


Connectors:



true && echo hello
false || echo hello
echo hello ; ls


Conveyor:



//    - \n
cat [file] | wc -l


git



git is a distributed version control system that allows you to control the process of making changes to a project.



Pro Git book .



Screencast by Ilya Kantor .



Quick Start: Git How To .



Installation: git-scm.com .



Installation check:



git --version


Reference:



git help
git help [command-name]
git [command-name] --help | -h


Minimum settings:



// --local -    
// --global -    
// --system -    , ..   
git config --global user.name "My Name"
git config --global user.email "myemail@example.com"


Additional settings:



//   
git config --list | -l --global

//   
git config --global --edit | -e


Repository creation:



git init


Cleaning up the repository:



// -d -  , -x -   , -f - 
git clean | -dxf


Removing files and directories:



// remove
git rm [file-name]
git rm -r [dir-name]

git rm --force | -f


Moving files:



// git add + git remove
// move
git mv [old-file] [new-file]


View the state of the repository:



git status


Adding changes:



git add [file-name]

git add --force | -f

//  
git add . | --all | -A

//           .gitkeep


Adding a message (commit):



//  
git commit

//    ,    git add . | -A
//  ,      
git commit --message | -m "My Message"

//   ,  git add [file-name]   
git commit --all | -a -m | -am "My Message"

//  
git commit --amend "My Message" | --no-edit


View commit:



//  
git show

//  
git show [hash] //   4 

//       
git show :/[string]

//    
git show [tag-name]


Viewing the difference between commits:



git diff HEAD | @ // HEAD -  ,  ; @ -   HEAD

// staged
git diff --staged | --cached

git diff [hash1] [hash2]

//   
git diff [branch1]...[branch2]

//       
git commit --verbose | -v

//   
git diff --word-diff | --color-words


View change history:



git log

// n -  
git log -n
// --since, --after - 
// --until, --before - 

// 
git log -p

//  
git log --graph --oneline --stat

//  
git log --pretty=format
// 
git log --pretty=format:'%C(red)%h %C(green)%cd %C(reset)| %C(blue)%s%d %C(yellow)[%an]' --date=short | format-local:'%F %R'

//    , , ; i -   
git log --grep | -G [string] | [file] | [branch] & -i

//    
git log --grep [string1] --grep [string2] --all-match

//     
git log -L '/<head>/','/<\/head>/':index.html

//   
git log --author=[name]


Cancellation of changes:



git reset
// --hard -     
// --soft -     
// --mixed -  :   ,   

git reset --hard [hash] | @~ // @~ -    HEAD

// 
git reset --hard ORIG_HEAD

//     
git checkout

git restore


Working with branches:



//  
git branch

//  
git branch [branch-name]

//   
git checkout [branch-name]

// branch + checkout
git checkout -b [branch-name]

// 
git branch -m [old-branch] [new-branch]

//  
git branch -d [branch-name]

//  
git merge [branch-name]


Resolving merge conflicts:



// ,   ,  

//     
git checkout --ours

//     
git checkout --theirs

//  
git reset --merge
git merge --abort

//   
git checkout --conflict=diff3 --merge [file-name]

//  
git merge --continue


Remote repository:



// 
git clone [url] & [dir]

// 
git remote
git remote show
git remote add [shortname] [url]
git remote rename [old-name] [new-name]

//  
// git fetch + git merge
git pull

//  
git push


Tags:



// 
git tag

//  
git tag [tag-name]
//
git tag v1-beta

//  
git tag -a v1 -m "My Version 1"

// 
git tag -d [tag-name]


Debugging



git bisect

git blame

git grep


Saving uncommitted changes:



// 
git stash

// 
git stash pop


Copying a commit:



git cherry-pick | -x [hash]

//   
// 
git cherry-pick --abort

// 
git cherry-pick --continue

git cherry-pick --no-commit | -n

// --cherry = --cherry-mark --left-right --no-merges
git log --oneline --cherry [branch1] [branch2]


Relocation:



git rebase [branch]

//   
// 
git rebase --abort

// 
git rebase --skip

// 
git rebase --continue

//   
git rebase --preserve-merges | -p

//  
git rebase -i [branch]


Autocomplete duplicate conflicts:



// rerere - reuse recorder resolution
// rerere.enabled true | false
// rerere.autoUpdate true | false
// rerere-train.sh -    rerere
git rerere forget [file-name]


Reverse commits:



git revert @ | [hash]

//  
// git reset --hard @~  
git revert [hash] -m 1

// git merge [branch]  
//  
git revert [hash]

//    rebase
git rebase [branch1] [branch2] | --onto [branch1] [hash] [branch2]

git merge [branch]

git rebase [hash] --no-ff


An example of aliases (shortcuts) for .gitconfig:



[alias]
    aa = add -A
    co = checkout
    ci = commit -m
    st = status
    br = branch


Example .gitconfig:
[user]
	name = [My Name]
	email = [myemail@example.com]
	username = [myusername]
[core]
	editor = [myeditor]
	whitespace = fix,-indent-with-non-tab,trailing-space,cr-at-eol
	pager = delta
[web]
	browser = google-chrome
[instaweb]
	httpd = apache2 -f
[rerere]
	enabled = 1
	autoupdate = 1
[push]
	default = matching
[color]
	ui = auto
[color "branch"]
	current = yellow bold
	local = green bold
	remote = cyan bold
[color "diff"]
	meta = yellow bold
	frag = magenta bold
	old = red bold
	new = green bold
	whitespace = red reverse
[color "status"]
	added = green bold
	changed = yellow bold
	untracked = red bold
[difftool]
	prompt = false
[delta]
	features = line-numbers decorations
	line-numbers = true
[delta "decorations"]
	minus-style = red bold normal
	plus-style = green bold normal
	minus-emph-style = white bold red
	minus-non-emph-style = red bold normal
	plus-emph-style = white bold green
	plus-non-emph-style = green bold normal
	file-style = yellow bold none
	file-decoration-style = yellow box
	hunk-header-style = magenta bold
	hunk-header-decoration-style = magenta box
	minus-empty-line-marker-style = normal normal
	plus-empty-line-marker-style = normal normal
	line-numbers-right-format = "{np:^4}โ”‚ "
[github]
	user = [username]
	token = token
[gitflow "prefix"]
	versiontag = v
[sequence]
	editor = interactive-rebase-tool
[alias]
	a = add --all
	ai = add -i
	###
	ap = apply
	as = apply --stat
	ac = apply --check
	###
	ama = am --abort
	amr = am --resolved
	ams = am --skip
	###
	b = branch
	ba = branch -a
	bd = branch -d
	bdd = branch -D
	br = branch -r
	bc = rev-parse --abbrev-ref HEAD
	bu = !git rev-parse --abbrev-ref --symbolic-full-name "@{u}"
	bs = !git-branch-status
	###
	c = commit
	ca = commit -a
	cm = commit -m
	cam = commit -am
	cem = commit --allow-empty -m
	cd = commit --amend
	cad = commit -a --amend
	ced = commit --allow-empty --amend
	###
	cl = clone
	cld = clone --depth 1
	clg = !sh -c 'git clone git://github.com/$1 $(basename $1)' -
	clgp = !sh -c 'git clone git@github.com:$1 $(basename $1)' -
	clgu = !sh -c 'git clone git@github.com:$(git config --get user.username)/$1 $1' -
	###
	cp = cherry-pick
	cpa = cherry-pick --abort
	cpc = cherry-pick --continue
	###
	d = diff
	dp = diff --patience
	dc = diff --cached
	dk = diff --check
	dck = diff --cached --check
	dt = difftool
	dct = difftool --cached
	###
	f = fetch
	fo = fetch origin
	fu = fetch upstream
	###
	fp = format-patch
	###
	fk = fsck
	###
	g = grep -p
	###
	l = log --oneline
	lg = log --oneline --graph --decorate
	###
	ls = ls-files
	lsf = !git ls-files | grep -i
	###
	m = merge
	ma = merge --abort
	mc = merge --continue
	ms = merge --skip
	###
	o = checkout
	om = checkout master
	ob = checkout -b
	opr = !sh -c 'git fo pull/$1/head:pr-$1 && git o pr-$1'
	###
	pr = prune -v
	###
	ps = push
	psf = push -f
	psu = push -u
	pst = push --tags
	###
	pso = push origin
	psao = push --all origin
	psfo = push -f origin
	psuo = push -u origin
	###
	psom = push origin master
	psaom = push --all origin master
	psfom = push -f origin master
	psuom = push -u origin master
	psoc = !git push origin $(git bc)
	psaoc = !git push --all origin $(git bc)
	psfoc = !git push -f origin $(git bc)
	psuoc = !git push -u origin $(git bc)
	psdc = !git push origin :$(git bc)
	###
	pl = pull
	pb = pull --rebase
	###
	plo = pull origin
	pbo = pull --rebase origin
	plom = pull origin master
	ploc = !git pull origin $(git bc)
	pbom = pull --rebase origin master
	pboc = !git pull --rebase origin $(git bc)
	###
	plu = pull upstream
	plum = pull upstream master
	pluc = !git pull upstream $(git bc)
	pbum = pull --rebase upstream master
	pbuc = !git pull --rebase upstream $(git bc)
	###
	rb = rebase
	rba = rebase --abort
	rbc = rebase --continue
	rbi = rebase --interactive
	rbs = rebase --skip
	###
	re = reset
	rh = reset HEAD
	reh = reset --hard
	rem = reset --mixed
	res = reset --soft
	rehh = reset --hard HEAD
	remh = reset --mixed HEAD
	resh = reset --soft HEAD
	rehom = reset --hard origin/master
	###
	r = remote
	ra = remote add
	rr = remote rm
	rv = remote -v
	rn = remote rename
	rp = remote prune
	rs = remote show
	rao = remote add origin
	rau = remote add upstream
	rro = remote remove origin
	rru = remote remove upstream
	rso = remote show origin
	rsu = remote show upstream
	rpo = remote prune origin
	rpu = remote prune upstream
	###
	rmf = rm -f
	rmrf = rm -r -f
	###
	s = status
	sb = status -s -b
	###
	sa = stash apply
	sc = stash clear
	sd = stash drop
	sl = stash list
	sp = stash pop
	ss = stash save
	ssk = stash save -k
	sw = stash show
	st = !git stash list | wc -l 2>/dev/null | grep -oEi '[0-9][0-9]*'
	###
	t = tag
	td = tag -d
	###
	w = show
	wp = show -p
	wr = show -p --no-color
	###
	svnr = svn rebase
	svnd = svn dcommit
	svnl = svn log --oneline --show-commit
	###
	subadd = !sh -c 'git submodule add git://github.com/$1 $2/$(basename $1)' -
	subrm = !sh -c 'git submodule deinit -f -- $1 && rm -rf .git/modules/$1 && git rm -f $1' -
	subup = submodule update --init --recursive
	subpull = !git submodule foreach git pull --tags origin master
	###
	assume = update-index --assume-unchanged
	unassume = update-index --no-assume-unchanged
	assumed = !git ls -v | grep ^h | cut -c 3-
	unassumeall = !git assumed | xargs git unassume
	assumeall = !git status -s | awk {'print $2'} | xargs git assume
	###
	bump = !sh -c 'git commit -am \"Version bump v$1\" && git psuoc && git release $1' -
	release = !sh -c 'git tag v$1 && git pst' -
	unrelease = !sh -c 'git tag -d v$1 && git pso :v$1' -
	merged = !sh -c 'git o master && git plom && git bd $1 && git rpo' -
	aliases = !git config -l | grep alias | cut -c 7-
	snap = !git stash save 'snapshot: $(date)' && git stash apply 'stash@{0}'
	bare = !sh -c 'git symbolic-ref HEAD refs/heads/$1 && git rm --cached -r . && git clean -xfd' -
	whois = !sh -c 'git log -i -1 --author=\"$1\" --pretty=\"format:%an <%ae>\"' -
	serve = daemon --reuseaddr --verbose --base-path=. --export-all ./.git
	###
	behind = !git rev-list --left-only --count $(git bu)...HEAD
	ahead = !git rev-list --right-only --count $(git bu)...HEAD
	###
	ours = "!f() { git checkout --ours $@ && git add $@; }; f"
	theirs = "!f() { git checkout --theirs $@ && git add $@; }; f"
	subrepo = !sh -c 'git filter-branch --prune-empty --subdirectory-filter $1 master' -
	human = name-rev --name-only --refs=refs/heads/*
[filter "lfs"]
	clean = git-lfs clean -- %f
	smudge = git-lfs smudge -- %f
	process = git-lfs filter-process
	required = true




Example .gitignore:
### Node ###

# Logs
logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Optional npm cache directory
.npm

# Dependency directories
/node_modules
/jspm_packages
/bower_components

# Yarn Integrity file
.yarn-integrity

# Optional eslint cache
.eslintcache

# dotenv environment variables file(s)
.env
.env.*

#Build generated
dist/
build/

# Serverless generated files
.serverless/

### SublimeText ###
# cache files for sublime text
*.tmlanguage.cache
*.tmPreferences.cache
*.stTheme.cache

# workspace files are user-specific
*.sublime-workspace

# project files should be checked into the repository, unless a significant
# proportion of contributors will probably not be using SublimeText
# *.sublime-project


### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

### Vim ###
*.sw[a-p]

### WebStorm/IntelliJ ###
/.idea
modules.xml
*.ipr
*.iml


### System Files ###
*.DS_Store

# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent




npm



npm is a package manager that allows you to install project dependencies.



Official site: npmjs.com .



Installation.



npm installed with Node.js .



Also, npx is installed with Node.js, which allows you to run executables without installing: npx create-react-app my-app.



Installation check:



node --version | -v
npm --version | -v


Update:



npm i -g npm@latest


List of available commands:



npm help
npm help [command-name]


Project initialization:



npm init

// auto
npm init --yes | -y


Installing dependencies



npm install | i

//   
npm explore [package-name]

//   
npm doctor

// 
npm ci


Forced reinstallation of dependencies:



npm i --force | -f


Installing only production packages:



npm i --only=production | --only=prod


Add dependency:



npm i [package-name]
npm i [package-name@version]

// 
npm i express


Adding a development dependency:



npm i --save-dev | -D [package-name]

// 
npm i -D nodemon


Dependency update:



npm update | up [package-name]


Dependency Removal:



// dependency
npm remove | rm | r [package-name]

// devDependency
npm r -D [package-name]


Global install / update / uninstall package:



npm i/up/r -g [package-name]

// 
npm i -g create-react-app
// 
create-react-app my-app


Determining obsolete packages:



npm outdated
npm outdated [package-name]


List of installed dependencies:



npm list | ls

// top level
npm ls --depth=0 | --depth 0

// global + top level
npm ls -g --depth 0


Package information:



npm view | v [package-name]

// 
npm v react
npm v react.description


Script run / command execution:



npm run [script]

// 
// package.json: "scripts": { "dev": "nodemon server.js" }
npm run dev
// script start  node server.js
npm start
npm stop


Removing duplicate packages:



npm dedupe | ddp


Removing extraneous packages:



npm prune


Detection of vulnerabilities (security threats):



npm audit
// json
npm audit --json
// plain text
npm audit --parseable


Automatically fix vulnerabilities:



npm audit fix


yarn



yarn, like npm, is a package manager that lets you install project dependencies.



Official site: yarnpkg.com .



Installation:



npm i -g yarn


The "yarn dlx" command allows you to run executables without installing: yarn dlx create-react-app my-app. To do this, yarn needs to be updated to version 2: yarn set version berry.



Installation check:



yarn --version | -v


Update:



yarn set version latest


List of available commands:



yarn help
yarn help [command-name]


Project initialization:



yarn init

// auto
yarn init --yes | -y

// "private": true  package.json
yarn init --private | -p

// auto + private
yarn init -yp


Installing dependencies:



yarn
// 
yarn install

//  
yarn install --silent | -s

// 
yarn --check-files


Forced reinstallation of dependencies:



yarn install --force


Installing only production packages:



yarn install --production | --prod


Add dependency:



yarn add [package-name]
yarn add [package-name@version]

// 
yarn add express

//  
yarn add --silent
// 
yarn add -s


Adding a development dependency:



yarn add --dev | -D [package-name]

// 
yarn add -D nodemon


Dependency update:



yarn upgrade [package-name]


Dependency Removal:



yarn remove [package-name]


Global install / update / uninstall package:



yarn global add/upgrade/remove [package-name]

// 
yarn global add create-react-app
// 
create-react-app my-app


List of installed dependencies:



yarn list

// top level
yarn list --depth=0 | --depth 0


Package information:



yarn info [package-name]
// 
yarn why [package-name]

// 
yarn info react
yarn info react description
yarn why webpack


Script run / command execution:



yarn [script]
// 
yarn run [script]

// 
// package.json: "scripts": { "dev": "nodemon server.js" }
yarn dev


package.json



{
  "name": "my-app",
  "version": "1.0.0",
  "description": "my awesome app",
  "keywords": [
    "amazing",
    "awesome",
    "best"
  ],
  "private": true,
  "main": "server.js",
  "license": "MIT",
  "homepage": "https://my-website.com",
  "repository": {
    "type": "git",
    "url": "https://github.com/user/repo.git"
  },
  "repository": "github:user/repo",
  "author": {
    "name": "My Name",
    "email": "myemail@example.com",
    "url": "https://my-website.com"
  },
  "author": "My Name <myemail@example.com> (https://my-website.com)",
  "contributers": [
    {
      "name": "Friend Name",
      "email": "friendemail@example.com",
      "url": "https://friend-website.com"
    }
  ],
  "contributors": "Friend Name <friendemail.com> (https://friend-website.com)",
  "dependencies": {
    "express": "^4.17.1"
  },
  "devDependencies": {
    "nodemon": "^2.0.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "dev": "nodemon server.js"
  }
}


  • name - project name
  • version - version of the project (see versioning)
  • description - project description (why do you need a package?)
  • keywords - keywords (makes it easier to search in the npm registry)
  • private โ€” true npm
  • main โ€”
  • repository โ€” ( )
  • author โ€” ( )
  • contributors โ€” (, )
  • dependencies โ€” (, )
  • devDependencies โ€” (, )
  • scripts โ€” ( , ), , , ยซyarn devยป ยซnodemon server.jsยป


Complete list of available fields in the "package.json" file: npm-package.json



The "package-lock.json" and "yarn.lock" files contain more complete information about installed packages than package.json, for example, specific package versions instead of a range valid versions.



Versioning



Each package has a version consisting of three digits (for example, 1.0.0), where the first digit is the major version (major), the second is the minor version (minor), the third is the patch version (patch). The release of a new version is called a release.



Increasing each of these numbers according to semantic versioning rules (semver) means the following:



  • major - making changes incompatible with the previous version
  • minor - new functionality that is compatible with the previous version
  • patch - bug fixes, minor improvements


Version ranges or valid releases are determined using the following operators (comparators):



  • * - any version (same as empty string)
  • <1.0.0 - any version less than 1.0.0
  • <= 1.0.0 - any version less than or equal to 1.0.0
  • > 1.0.0 - any version greater than 1.0.0
  • > = 1.0.0 - any version greater than or equal to 1.0.0
  • = 1.0.0 - version 1.0.0 only (the "=" operator can be omitted)
  • > = 1.0.0 <2.0.0 - greater than or equal to 1.0.0 and less than 2.0.0
  • 1.0.0-2.0.0 - a set of versions inclusive
  • ^ 1.0.0 - minor and patch releases (> = 1.0.0 <2.0.0)
  • ~ .1.0.0 - only patch releases (> = 1.0.0 <1.1.0)


Details for semver: node-semver .



Thank you for attention.



All Articles