踩坑日记 6 - git&npm 问题

git 任何操作都出现端口拒绝 connection refused 问题,可能是

网络问题

原因可能是:本机网络设置的 DNS 服务器解析 Github 相关域名到遭受污染的 IP 地址,这些 IP 地址要么本身无法访问,要么节点过远,从而导致了访问失败或者速度缓慢。

  • 检查网络是否可用

  • 配置 host 文件:点击查看

      # update: 20220222
      # Github Hosts
      # domain: github.com
      140.82.112.4 github.com
      140.82.113.9 nodeload.github.com
      140.82.112.6 api.github.com
      140.82.114.9 codeload.github.com
      185.199.108.133 raw.github.com
      185.199.108.153 training.github.com
      185.199.108.153 assets-cdn.github.com
      185.199.108.153 documentcloud.github.com
      140.82.114.17 help.github.com
    
      # domain: githubstatus.com
      185.199.108.153 githubstatus.com
    
      # domain: fastly.net
      199.232.69.194 github.global.ssl.fastly.net
    
      # domain: githubusercontent.com
      185.199.108.133 raw.githubusercontent.com
      185.199.108.154 pkg-containers.githubusercontent.com
      185.199.108.133 cloud.githubusercontent.com
      185.199.108.133 gist.githubusercontent.com
      185.199.108.133 marketplace-screenshots.githubusercontent.com
      185.199.108.133 repository-images.githubusercontent.com
      185.199.108.133 user-images.githubusercontent.com
      185.199.108.133 desktop.githubusercontent.com
      185.199.108.133 avatars.githubusercontent.com
      185.199.108.133 avatars0.githubusercontent.com
      185.199.108.133 avatars1.githubusercontent.com
      185.199.108.133 avatars2.githubusercontent.com
      185.199.108.133 avatars3.githubusercontent.com
      185.199.108.133 avatars4.githubusercontent.com
      185.199.108.133 avatars5.githubusercontent.com
      185.199.108.133 avatars6.githubusercontent.com
      185.199.108.133 avatars7.githubusercontent.com
      185.199.108.133 avatars8.githubusercontent.com
    

    最上面这四个总会变,其他的一般不变。所以如果访问不了了,自己用 ipaddress 查一下最新的 Ip:

    - 140.82.114.4 http://github.com
    - 140.82.114.10 http://nodeload.github.com
    - 140.82.114.6 http://api.github.com
    - 140.82.114.10 http://codeload.github.com
    
  • 清除 DNS 缓存代码:ipconfig/flushdns

git ssh key

  • 生成 ssh key: ssh-keygen -t rsa -C "[email protected]",默认回车不设置密码

  • 将 C 盘用户目录下的.ssh 目录下生成的公钥 id_rsa.pub 添加到 github:

    • 查看并复制:~/.ssh/id_rsa.pub
    • 点击 github 个人头像的 Setting
    • 点击 SSH and GPG keys
    • 点击 New SSH key or Add SSH key
    • 输入 Title 和 Key 并保存
  • 问题 1:将公钥添加到 GitHub 后,每次提交还要输入用户名与密码,需要切换成 SSH 协议:

    只需将 .git/config 文件中的:url = https://github.com/Name/project.git,改为:url = [email protected]:Name/project.git

  • 问题 2:Permission denied (publickey);

    • 确保打开 ssh-agent:eval "$(ssh-agent -s)"
    • 检查你的私钥添加到 ssh-agent:ssh-add -l

sourceTree 回滚代码到指定版本

sourceTree 回滚代码到指定版本

personal token

git clone https://github.com/username/repo.git
Username: your_username
Password: your_token

个人访问令牌只能用于 HTTPS Git 操作。如果您的存储库使用 SSH 远程 URL,则需要将远程从 SSH 切换到 HTTPS

如果系统未提示您输入用户名和密码,则您的凭据可能已缓存在您的计算机上。您可以更新钥匙串中的凭据以使用令牌替换旧密码。

Internat 地址或网络地址:git:https://github.com
用户名:your_username
密码:your_token

npm

发包

1
2
3
npm init 
npm login
npm publish

删除包:

1
npm umpublish packageName@version

发包 d.ts 声明文件发布被忽略问题

一 是 package.json 里的 files 字段,是否限定过发布时只包含某些文件、或剔除过某些文件。

二 是项目目录下有无一个 .npmigore 文件,里面设置了发布时忽略某些文件。

报错:filePath not found of @umijs/renderer-react/node_modules/@types/react

yarn/npm install 的时候,.umi 下生成的文件与其原有的冲突了,只需要把源文件下 src 下的.umi 删除,重新 npm/yarn install 就能解决。

镜像源

一、使用淘宝镜像

1. 临时使用

  npm --registry https://registry.npm.taobao.org install express

2. 持久使用

  npm config set registry https://registry.npm.taobao.org

3. 通过 cnpm

  npm install -g cnpm --registry=https://registry.npm.taobao.org

二、使用官方镜像

  npm config set registry https://registry.npmjs.org/

三、查看 npm 源地址

  npm config get registry