Git 是实现 Windows 之 Linux 开发环境的利器

Windows 编译的前端代码无法在 Linux 服务器上正常运行。这是使用 Windows 进行开发的前端程序员的一大痛点。

不可行方案

  • 使用 Git Bash 编译

    失败。因为 Git Bash 只是一个执行命令的窗口,最终编译代码是由安装在 Windows 中的 Node 负责,生成的代码,依然属于 Windows 编码。

  • 使用 WSL 编译

    失败。因为安装 Node、MySQL 等开发环境非常繁琐,并且 WSL 本质仍然是处于 Windows 系统中,编译后的代码依然属于 Windows 编码。

  • 使用 VirtualBox 共享文件夹

    失败。因为共享文件夹本质依然是 Windows 文件系统,VirutalBox 中的 Linux 无法直接在这样的文件系统中进行编译操作,会报读写错误。

可行方案

注意!以下的前提是您的 Windows 账号是本地账号,而不是 Microsoft 邮箱账号。

0. 在 Linux 中使用 SSH 连接 Windows(可选)

  • Windows 开启 OpenSSH 服务器

    依次点击 设置 > 系统 > 添加可选功能 > 查看功能 ,在弹窗中找到 OpenSSH 服务器 并勾选。

    等到添加进度条完成后,使用管理员身份运行 PowerShell(不是 CMD),并执行以下命令:

    sh 复制代码
    # 启动 SSH 服务
    Start-Service sshd
    
    # 设置 SSH 服务为开机自启动
    Set-Service -Name sshd -StartupType Automatic
    
    # 检查 OpenSSH 服务状态
    Get-Service sshd

    确保 Windows 防火墙允许 SSH 连接:依次点击 控制面板 > 系统和安全 > Windows Defender 防火墙 > 高级设置 > 入站规则 ,找到 OpenSSH SSH Server (sshd) 确保它已启动。

    然后打开 C:\ProgramData\ssh\sshd_config 文件,找到以下三个配置项并去掉其注释:

    cnf 复制代码
    PasswordAuthentication yes
    PubkeyAuthentication yes
    AuthorizedKeysFile .ssh/authorized_keys

    保存后并重启 OpenSSH 服务:

    sh 复制代码
    Restart-Service sshd
  • 在 VirtualBox 的 Linux 中执行以下命令生成 RSA 公钥和私钥

    bash 复制代码
    # 生成的密钥对位于 ~/.ssh/ 目录中,例如 /root/.ssh/
    # 私钥:~/.ssh/id_ras
    # 公钥:~/.ssh/id_ras.pub
    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  • 在 Linux 命令行中将公钥推送到 Windows

    外部公钥一般保存在 ~/.ssh/authorized_keys 中。在 Windows 中,~ 表示 C:\users\<your_windows_username>

    bash 复制代码
    # <your_windows_username> 可在 Windows CMD 中执行 echo %username% 命令获得
    # <your_windows_host> 可在 Windows CMD 中执行 ipconfig 命令获得
    ssh-copy-id -i ~/.ssh/id_rsa.pub <your_windows_username>@<your_windows_host>

    到此,我们就可以在 Linux 中执行 ssh <your_windows_username>@<your_windows_host>,输入 Windows 登录密码后,即可执行 Windows 的 CMD 命令了。

1. 在 Windows 中使用 SSH 连接 Linux

  • Linux 天然支持 SSH Server,因此我们只需要把在 Windows 生成公钥发送给 Linux 即可

    在 Windows 中打开 Git Bash 输入以下命令生成公钥私钥对

    bash 复制代码
    # 生成的密钥对位于 ~/.ssh/ 目录中,例如 C:\Users\Your_Windows_User_Name\.ssh\
    # 私钥:~/.ssh/id_ras
    # 公钥:~/.ssh/id_ras.pub
    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

    紧接着在 Git Bash 中把公钥发送到 Linux 用户根目录下

    bash 复制代码
    # <your_linux_username> 即你使用的 Linux 用户名,如 root
    # <your_linux_host> 可在 Linux 中使用 ip addr 命令获得
    scp ~/.ssh/id_rsa.pub <your_linux_username>@<your_linux_host>:~/

    然后登录 Linux 系统到上述命令所指定用户,把公钥内容追加到 authorized_keys 文件中

    bash 复制代码
    # 如果 authorized_keys 文件未存在,可以使用 touch 命令创建,并赋予相应权限
    # touch ~/.ssh/authorized_keys
    # chmod 600 ~/.ssh/authorized_keys
    # chmod 700 ~/.ssh
    
    # 追加公钥到 authorized_keys 文件
    cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
    
    # 删除该公钥文件
    rm ~/id_rsa.pub

    接着在 Linux 中的 /etc/ssh/sshd_config 文件中确保以下内容开启

    config 复制代码
    PubkeyAuthentication yes
    PermitRootLogin yes

    保存完毕后重启 Linux 的 ssh 服务。至此,Windows 便可无密码登录 Linux 系统

    bash 复制代码
    systemctl restart sshd

2. 在 Linux 中创建仓库代码

你可以选择脚手架命令一键创建项目目录和代码仓库(.git)

bash 复制代码
# 例如 create-next-app 会在项目目录中自动创建 .git 子目录
npx create-next-app@latest your-project-name

你也可以选择自行创建项目目录和代码仓库(.git)

bash 复制代码
# 先创建项目目录
mkdir your-project-name

# 跳转到目录中
cd your-project-name

# 初始化代码仓库
git init

3. 在 Linux 中开发代码并打包

这里的开发指的是在 Linux 中运行,但编辑还是在 Windows 所安装的 VS Code 中完成。具体请查看 VS Code 如何连接服务器进行跨端开发

4. 在 Windows 中拉取 Linux 中的已开发好的代码

回到 Windows 系统,在你希望放置该项目的位置,执行以下命令拉取代码

bash 复制代码
# 初次拉取代码
git clone ssh://<your_linux_username>@<your_linux_host>/path/to/your-project-name

再次拉取代码前,可以先进入该目录中,再进行拉取操作。当然,拉取的代码必须是 Linux 环境中已经 commit 后的代码。不建议在 Windows 中修改代码后 push 到 Linux 环境中

bash 复制代码
# 在 Windows 中先进入目录
cd your-project-name

# 拉取代码
git pull

5. 将 Windows 中的打包发送至服务器进行部署

使用 WinSCP 软件可以把你编译后的代码发送至服务器,然后在服务器中启动/重启项目即可。

小结

由于 Windows 对 Git 仓库的路径访问进行了很大的限制,本文介绍了一种相对方便的方式,以帮助 Windows 使用者也能顺利使用 Linux 编译环境进行开发。希望对你有用。

相关推荐
星海拾遗19 小时前
git rebase记录
大数据·git·elasticsearch
ljh57464911920 小时前
PhpStorm 2022.3 版本中,修改使用 Git 提交时看到弹出式的对话框模式
ide·git·php·phpstorm
云闲不收1 天前
git rebase
git
江上清风山间明月1 天前
git pull和git checkout在恢复文件的区别
git·pull·checkout
海鸥811 天前
in argocd ‘/tmp/_argocd-repo/../.git/index.lock‘: No space left on
git·argocd
添砖java‘’1 天前
Linux信号机制详解:从产生到处理
linux·c++·操作系统·信号处理
尔嵘1 天前
git操作
大数据·git·elasticsearch
好评1241 天前
Linux文件上传git
linux·运维·git
大柏怎么被偷了1 天前
【Git】企业级开发模型
git
Garfield20051 天前
Git 分支拓扑实践
git·拓扑