整理自「开发 Tips」系列,汇总 Git 协作与开发环境配置相关实践。
1. warning: LF will be replaced by CRLF
Windows、Linux 和 Mac 在处理文件换行时,标示符是不一致的。Windows 使用 CRLF 作为结束符,而 Linux 和 Mac 使用 LF 作为结束符。
对待换行符,Git 有两种模式。查看 Git 配置。
1
| git config core.autocrlf
|
如果显示为 true,则每一次当你 git commit 时,如果存在文本文件,那么 git 会自动帮你将末尾的换行符改为 CRLF,省去了烦心的转换工作。
如果显示为 false,则 git 不会对换行符进行修改,保持原始的内容。
Linux 和 Mac 开发者,这个配置应当为 false,而 Windows 开发者,则应当设置为 true。
1
| git config --global core.autocrlf false
|
2. 如何合并两个 fork 仓库
查看本地远程源:
1
2
3
| git remote -v
origin https://github.com/yourname/celery.git (fetch)
origin https://github.com/yourname/celery.git (push)
|
添加需要合并的远程源:
1
| git remote add upstream https://github.com/celery/celery.git
|
查看本地远程源:
1
2
3
4
5
| git remote -v
origin https://github.com/yourname/celery.git (fetch)
origin https://github.com/yourname/celery.git (push)
upstream https://github.com/celery/celery.git (fetch)
upstream https://github.com/celery/celery.git (push)
|
获取远程源的最新更新:
1
2
3
4
| git fetch upstream
From https://github.com/celery/celery
* [new branch] var.ci -> upstream/var.ci
* [new branch] workhorse-pool -> upstream/workhorse-pool
|
合并远程 fork 分支到当前分支:
1
| git merge upstream/master
|
3. 基于 Tag 进行 Git 开发
基于 Tag 创建分支:
1
| git branch v4.2.0_docs v4.2.0
|
实际上,branch 可以是 分支,Tag,甚至 commit id。
切换到新建的分支:
1
| git checkout v4.2.0_docs
|
查看提交者用户名和邮箱信息:
1
2
| git config user.name
git config user.email
|
将新建的分支推送到远程:
1
| git push origin v4.2.0_docs
|
4. Linux 下设置 Git 访问凭证
Windows 或 OS X 上有 keychain 工具管理账户凭证,在 Linux 系统上使用 Http/Https 协议访问 Git 仓库时,每次都需要输入账户密码。通过下面的配置,可以省略这一过程。
- 新建凭证文件
1
| touch ~/.git-credentials
|
- 编辑文件,添加凭证信息
1
| https://{username}:{password}@git-domain.com
|
- 使凭证生效
1
| git config --global credential.helper store
|
执行完毕,会生成 Git 配置文件 ~/.gitconfig。
cat ~/.gitconfig
[user]
email = yourmail
name = yourname
[credential]
helper = store
5. pipenv 使用
- Pipfile
Pipfile 是社区拟定的依赖管理文件,用于替代 requirements.txt。Pipfile.lock 中记录了当前环境中安装的版本号和哈希值。
- 安装
- 创建环境
1
2
3
4
5
6
7
8
| # Python3
pipenv --three
# Python2
pipenv --two
# 指定版本
pipenv --python 3.6
# 指定解释器
pipenv --python pypy3
|
这里的解释器,需要已经在本地安装,可以不加入 PATH 环境变量中。
- 进入环境
6. pipenv 应该与 pyenv 配合使用
pipenv 可以管理项目的依赖环境,隔离每一个项目。
pipenv 还可以管理解释器,允许指定 Python 的版本。
1
2
| pipenv --python 3.6
pipenv --python 2.7.14
|
执行上述命令时,pipenv 首先会在系统中寻找合适的版本。如果没有找到,同时安装了 pyenv,pipenv 会自动调用 pyenv 下载对应版本的 Python 解释器。
如果没有安装 pyenv,pipenv 仅会提示找不到匹配的版本。因此,在使用 pipenv 时,最好能配合 pyenv 使用。
7. Windows 下快速搭建开发环境
将工作 PC 的操作系统升级到 Windows 10,下面是我快速搭建 Python 开发环境的步骤:
管理员权限, CMD 下执行:
1
| @"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
|
新建文件 packages.config,内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| <?xml version="1.0" encoding="utf-8"?>
<packages >
<package id="foxitreader" />
<package id="googlechrome" />
<package id="winrar" />
<package id="everything" />
<package id="notepadplusplus" />
<package id="vscode" />
<package id="msys2" />
<package id="conemu" />
<package id="git" />
<package id="tortoisesvn" />
<package id="hg" />
<package id="python2" />
<package id="pip" />
<package id="vcpython27" />
<package id="jdk8" />
<package id="nvm" />
<package id="redis-64" />
<package id="mysql" />
<package id="rabbitmq" />
<package id="docker-for-windows" />
</packages>
|
执行命令,安装软件:
1
| choco install packages.config -y
|
将 C:\tools\msys64\usr\bin 目录加入 PATH,从 gist 上下载 VS Code 的配置,这样基本开发软件就全都有了。
主要使用的 Console 是 Powershell,执行 bash 可以进入 msys2。
8. 如何启动 Windows 下的免安装版 MySQL
修改配置文件 my.ini:
1
2
3
4
5
| [mysqld]
port=3306
basedir=C:\\tools\\mysql\\current
datadir=C:\\ProgramData\\MySQL\\data
skip-grant-tables
|
新增 skip-grant-tables 用于跳过权限表的限制,不用验证密码,直接登录。
将 MySQL 注册为系统服务
1
| "C:\tools\mysql\current\bin\mysqld" --install MySQL --defaults-file="C:\tools\mysql\current\my.ini"
|
启动 MySQL
9. 如何将 MongoDB 注册为 Windows 服务
下载安装之后,将目录 C:\Program Files\MongoDB\Server\4.0\bin\ 加入 PATH。
以管理员权限执行命令:
1
| mongod.exe --config "C:\Program Files\MongoDB\Server\4.0\bin\mongod.cfg" --install -serviceName "MongoDB"
|
启动服务 MongoDB:
1
2
3
| C:\Windows\system32>net start mongodb
mongodb 服务正在启动 ..
mongodb 服务已经启动成功。
|