linux 基础命令
pwd # 当前目录
clear # 清屏
cls # 清屏
cd / # 回到根目录
mkdir myProject # 创建文件夹
rmdir myProject # 删除文件夹
# 创建文件
touch index.js
touch index.js styles.css index.html
echo hello > file1.txt # 写入文件
echo world >> file1.txt # 追加内容 (append到下一行)
rm index.js # 永久删除文件(& 不会出现在回收站中)
rm -r myProject # 删除多个层级文件夹中的全部文件
man rm # 查看手册
# 查看当前路径下文件
ls
ls -s # (size)显示文件大小
ls -l # (long)文件权限、文件修改日期等更多信息
ls -ls # 结合上面两个标志位
ls -a # 显示隐藏文件
man ls # 查看 ls 可用的所有 flag
# copy 源 目标
copy data/index.html copied/
copy -r data/ copied/ # 递归拷贝 data 文件夹中的内容 到 copied 文件夹中
# 注意:后面加 / 代表是已经存在的文件夹,如果不加则会自动创建新的文件夹
# move 源 目标(也可以用于重命名文件)
mv data/index.html moved/ # 将 data/index.html 移动到 moved/ 文件夹中
mv index1.js index2.js # 重命名
# vscode
code # 打开 vscode
code . # 打开当前目录
code index.js # 打开指定文件
windows 基础命令
dir # 同 ls
dir -a # 同 ls -a
git
本质是追踪变化;
- 一般添加新功能都不会提交到主分支上,会新建一个分支,命名类似于
feat/cart-logic
原理
当在一个新目录或已有目录执行 git init 时,Git 会创建一个 .git 目录。 这个目录包含了几乎所有 Git 存储和操作的东西。 如若想备份或复制一个版本库,只需把这个目录拷贝至另一处即可。
HEAD
- 指向哪里,实际看到的文件就是哪里的;
- 指向分支名,实际就是指向分支的最新 commit;
detached HEAD
- 指向非分支名的 commit 的时候;
- 此时不能做新的 commit,只能查看;
- 如果一定要 commit,可以在此新建一个分支,然后 commit:
git branch detached-head 037e3f3 # 在指定的 commitID 位置创建新 branch
branch
- 初始就有一个 master 分支;
- 每个分支名都是一个指针,指向该分支的最新的 commit;
配置账户等信息
# 配置 google 'git config'
git config --help # config help
git config -h # 简短版本
git config --global user.name "Shiwen Cheng" # 设置用户签名(必须)
git config --global user.email "912284855@qq.com"
git config --global core.editor "code --wait" # 设置 vscode 为默认编辑器,且需等待用户关闭编辑器
git config --global core.autocrlf true # 设置空格相关,true for windows
git config --global core.autocrlf input # for mac & linux
git config --global diff.tool vscode # 配置默认的 diff 工具
git config --global difftool.vscode.cmd "code --wait --diff $LOCAL $REMOTE" # 打开文件看一下,占位符可能丢掉,要再写到文件里
git config --global -e # 查看配置文件;也可以在使用的用户目录下(C:\Users\chengshiwen),找其中的 .gitconfig 文件
基础
git --version # 查看版本
git update-git-for-windows # 更新 windows 版本
# 初始化,先切换到项目文件夹
git init
git status # working directory 状态
git add index.js # 从 工作区 添加到 暂存区
git add .
git ls-files # 查看暂存区文件
git commit -m 'initial commit.'# 提交至本地仓库
# 查看提交记录
git log
git log --oneline # 查看 HEAD 指针之前的 commit
git log --stat # 查看每次提交的文件改变
git log --oneline --reverse # 逆序
git log --oneline --stat # the diff line numbers
git log --oneline --patch # 细节
git branch # 查看全部 branch & HEAD 在哪个 branch
git branch newBranch # 创建新 branch
git branch detached-head 037e3f3 # 在指定的 commitID 位置创建新 branch
# checkout 可用于 commit,也可用于分支
git checkout <commitID> # HEAD 移动到某个 commit
git checkout <branchName> # HEAD 移动到某个分支
git checkout -b bugfix # 创建分支 bugfix 并移动 HEAD
git branch -m master main # 将 master 分支 rename 为 main
# switch 只用于 branch
git switch branch-3 # 移动 HEAD 至某个分支
git switch -c branch-4 # 创建新分支并移动 HEAD
# 合并分支
git switch master # 先切换到 master/main
git merge other-branch # 将 other-branch 合并到当前分支
git branch -D other-branch # 删除已经 merge 的分支
.gitignore
*.log
node_modules/
!test.log # test.log文件不会被忽略
撤销相关
git rm <file(s)> # 同时在工作区 & 暂存区中删除 git mv <file(s)> # 同时在工作区 & 暂存区中移动
undo for unstaged changes
对于 tracked & unstaged 的更改
# 将 HEAD 中的内容写到工作区
git checkout --
git checkout
git restore .
# 进一步指定文件
git checkout <filename>
git restore <filename>
对于 untracked(肯定也是 unstaged)
递归清理工作树,(清理的是不受版本控制的文件)
-n 试运行,查看要删除的内容 -f 删除文件
git clean -dn # 先查看即将被删除的文件列表
git clean -df # (force) 删除上条命令列出的文件
undo for staged changes
方法 1
# 1. 将最新的提交内容更新到暂存区
git reset <filename>
# 2. 将最新的提交更新到工作区
git checkout <filename>
方法 2
# 1.
git restore --staged <file> # 必须要给 path
# 2.
git checkout <file>
git checkout .
delete commits
本质是移动 HEAD
# 删除一个 commit,但暂存区和工作区依然保留 changes
git reset --soft HEAD~1
# 删除一个 commit,并且更新上次的暂存区,但工作区依然有 changes
git reset HEAD~1
# 删除一个 commit,& 还原暂存区 & 还原工作区
git reset --hard HEAD~1
删除分支
# 用于删除已 merge 的分支
git branch -d branch-1
# 强制删除
git branch -D branch-1
git branch -D branch-1 branch-2
进阶
git stash
用于保存 uncommitted & unstaged changes
git stash # 将工作区的 uncommitted & unstaged changes 推到 stash 中保存
git stash push -m 'fixing bug' # 同上,可以附加信息
git stash apply # 取出最新的 stash 并应用到工作区,但不会从 stash 中删除
git stash pop # 同上 && 会从 stash 中删除
git stash pop 0 # 同上,可以指定编号 && 会从 stash 中删除
git stash list # 查看全部的 stash changes
git stash drop 0 # 删除指定编号
git stash clear # 清除全部
git reflog 还原删除的信息
存储过去 30 天的操作记录 可在其中找到被删除的 commitID, 然后用 reset 来移动 HEAD
git reset --hard <commitID>
同样对于删除的 branch,也可以找到该 branch 上的最新 commitID,然后
git checkout <commitID> # 进入 detached HEAD
git switch -c newBranch # 在此新建并切换 branch,相当于把原来删掉的 branch 拿回来了
merge 两个 branch
主要分为两种 : fast-forward & non-fast-forward(包括 recursive、octopus、ours、subtree)
fast-forward
- 用于 master 分支没有新的 commit,即两条分支没有分歧的情况;
- 不会创建新的 commit 节点,就是把 HEAD 指针移动到指定分支的最新 commit;
git checkout master # 先切换到主分支
git merge branch-1 # 将 branch-1 merge 到主分支
squash merge 壁球合并
- 当 要被合并的分支上的历史 commit 不干净的时候,即只想要被合并分支上的最新 commit,并不想要它的历史 commit 时,可以新建一个 commit(包含全部的历史 commit 内容),然后再进行 fast-forward merge
git merge --squash branch-2
git commit -m 'hhhhhhh' # 创建新 commit
recursive merge
git merge # 两个分支有分歧的时候,该命令默认用的就是 recursive merge
git merge --no-ff branch-3 # 也可以直接指定不用 ff merge
也可以用 squash,会将被 merge 分支的最新的 commit 更新到工作区,然后再 commit 就可以了
rebase
- 从 master 分出另外一个分支的节点就是 base;
- rebase 就是将另一个分支从 master 的最新 commit 中分出;
- 然后用 ff merge;
注意:
- 不是移动 commit,而是要创建新的 commit(虽然其内容一样,但是 commitID 不一样,所以不适合在 push 以后跟他人的合作项目中使用);
- 不适合在 push 以后使用(不适合在与他人共享的 repository 中使用),最好只是在本地使用;
git switch branch-4 # 先移动到被 merge 的分支 branch-4
git rebase master # rebase,将 branch-4 的分支出口移动到 master 最新的 commit
# 再切换回 master ,用 ff merge
git switch master
git merge branch-4
使用场景:
- 当 master 和 other-branch 有分歧,并且 other-branch 中的 commit 依赖于 master 中的最新 commit,可以用;会影响 other-branch 中的 commit 的 ID;
- 或者是不想创建新的 merge commit 时,也可以用
处理 merge conflicts
git switch master
git merge branch-5
# 如果有冲突会跳出来,此时也可以 git status 查看可选择的命令
git merge --abort # 可以在这时终止 merge
git log --merge # 查看正在 merge 的两个 commit
# 解决冲突后
git add
git commit
Merge vs Rebase vs Cherry-Pick
Cherry-Pick 并不想将 other-branch 中的全部 commit merge 到 master,只是想挑出 other-branch 中的一个 commit 放到 master 的最新 commit;
# 先找到在 other-branch 想挑的 commit 的 ID
git switch master
git cherry-pick <commitID>
# 本质是复制了一个 commit(并且有新的 id)
git tag
一般都是用带注释的 tag
git tag # 查看全部 tags
git tag 1.0 <commitID> # 给指定 commit 添加 名为 1.0 的轻量标签
git show 1.0 # show 用于对象,commit 就是对象
git checkout 1.0 # 其实 tag 就可以像 id 一样使用
git tag -d 1.0 # 删除 tag
# annotated tag 带注释的 tag
git tag -a 2.0 -m 'test' # 可以添加注释信息
git show 2.0 # 对于带注释的 tag,可以看到 who、when 做的 tag
basic 命令
git diff # 比较 workdir 和 index 区别
git diff
git diff # 比较 workdir 和指定的,,,
git cherry-pick # 将指定 的 changes 整合到当前 branch
github
git clone <url> # 克隆仓库
git clone <url> . # 克隆仓库到当前文件夹(就不会在其中再创建文件夹了)
# git clone 会自动创建本地跟踪分支
# 一般工作流程
# 现在本地写代码
# 然后在 github 新建 repo
git remote add origin URL # 建立本地库与云端库的连接
git push origin main # 推送 main 分支到云端
git push # 从本地推到云端
git pull origin main # 拉取 main 分支
git pull # 将云端最新状态拉取到本地
# 先在 github 新建一个 repository
git remote add origin https://github.com/shiwen-cheng/github-basics.git # origin 指向这个云端库(就是指向后面这个连接),可以自定义名字(相当于一个 shorthand)
git branch -M main # rename 主分支 为 main,可以跳过这一步
git push -u origin main # git push origin master
git push origin master 本质是:
先将 changes 更新到远程跟踪分支(这里是 remote/origin/master,其实就是本地分支的 copy),然后再将远程跟踪分支更新到 云端库
git branch -a # 查看全部分支
git branch -r # 查看远程分支
git branch -vv # 查看分支的更多详细信息
git ls-remote # 列出云端的分支
git remote # 显示配置的远程服务器
git remote show origin # 查看配置的细节信息(包括跟踪分支等等)
同理 git pull origin master本质也是先将远程分支更新到 远程跟踪分支,然后再更新到本地分支;
git pull 本质是git fetch & git merge 的组合;
四种 branch
- local branch(本地分支)
- remote branch(远程分支),在 github 上看 or
git ls-remote - tracking branch(跟踪分支),用于在本地分支和远程分支之间交换信息
- remote tracking branch(远程跟踪分支) 获取远程分支的最新状态
git fetch;只读 - local tracking branch(本地跟踪分支)获取本地分支的最新状态可编辑
- remote tracking branch(远程跟踪分支) 获取远程分支的最新状态
git branch --track branchName origin/branchName # 创建跟踪 origin/branchName 的本地跟踪分支
git branch --delete --remote origin/feature # 删除远程跟踪分支
git push origin --delete feature # 删除远程分支
合作 & 贡献(是两种不同的协作方式)
权限的设置:账户设置优先于 repo 的设置
collaboration
contribution 贡献
- fork,一份完整的在云端的 repo copy;
- 然后在 fork 后的云端进行 clone、push 来更新代码;
- 更新完成后, 在原本的 repo 中选择 pull requests 通知原本的 repo 拥有者,拥有者可以选择 accept
vscode
设置
AutoSave -> onFocusChange WordWarp -> On FormatOnSave -> √
node
基础
node -v # 最好装偶数版本
没有 window、document
语义版本控制 / Semantic Versioning / SemVer
Major.Minor.Patch Major: 大更改,可能破坏 API Minor: 增加不破坏 api 的新特性 Patch: fix bugs, 打补丁
^4.13.6 :后两个接受改变 ~4.13.6 :最后一个接受改变
npm
找包:npm 包
安装 node 自带 npm
npm -v
npm install -g npm@latest # 更新 npm 版本
npm init # 创建 package.json
npm init -y
npm init --yes
npm i underscore # 安装包
npm i jshint --save-dev # 安装开发用依赖库
react
基础
npx create-react-app myProject # 初始化一个 react 项目(自动 git、.gitignore 等)
cd myProject
code .
npm start
index.js 是程序入口
npm run eject # 可以看到 webpack 相关的配置信息,但是该过程不可逆
语法
组件 OneTwoThree .jsx 文件: oneTwoThree.jsx (tsx)
原理
diff 算法
webpack
npm init -y
npm i webpack webpack-cli -D
npm install babel-loader -D
# 写好代码后
npx webpack --mode=development # 因为默认是 production 模式
webpack 有默认的配置,如默认的入口文件是 ./src,默认打包到 dist/main.js。更多的默认配置可以查看: node_modules/webpack/lib/WebpackOptionsDefaulter.js