在 Linux 上使用 PhpStorm 进行版本控制
一 环境准备
- 安装 Git(不同发行版命令不同)
- Ubuntu/Debian:sudo apt update && sudo apt install git
- CentOS/RHEL:sudo yum install git(或 dnf install git)
- 验证:git --version
- 配置 Git 全局身份(提交会附带此信息)
- git config --global user.name “Your Name”
- git config --global user.email “you@example.com”
- 在 PhpStorm 中确认 Git 路径
- 打开:File > Settings > Version Control > Git
- 在 Path to Git executable 填入 Git 可执行文件路径,常见为 /usr/bin/git,点击 Test 验证。
二 初始化或导入项目
- 新建项目并创建本地仓库
- File > New Project,勾选 Create Git Repository(或在项目创建后 VCS > Enable Version Control Integration 选择 Git)。
- 将已有项目纳入版本控制
- VCS > Enable Version Control Integration > 选择 Git。
- 克隆远程仓库
- File > New > Project from Version Control > Git,填入仓库 URL 与本地目录,点击 Clone。
三 日常操作
- 提交(Commit)
- VCS > Commit 或 Commit 工具窗口,勾选要提交的文件,填写提交信息后提交。
- 推送与拉取(Push / Pull)
- VCS > Git > Push(首次推送需设置上游分支),VCS > Git > Pull(建议先拉取再推送以减少冲突)。
- 分支管理
- 右下角 Git 分支 按钮或 VCS > Git > Branches,支持创建、切换、合并与删除分支。
- 查看历史与对比
- VCS > Show History 查看提交历史与差异;Version Control 工具窗口的 Log 页查看详细日志。
四 远程仓库与协作
- 添加远程仓库
- VCS > Git > Remotes… > +,名称常用 origin,填入远程 URL。
- 认证与凭据
- 推荐使用 SSH 或 HTTPS 凭据助手;在 PhpStorm:File > Settings > Appearance & Behavior > System Settings > Passwords,启用 Store passwords located in secure password storage,避免重复输入凭据。
五 冲突解决与其他 VCS
- 冲突解决
- 拉取或合并发生冲突时,PhpStorm 会提示冲突文件;打开 Merge 工具,使用 “Accept Yours / Accept Theirs / Merge Manually” 完成解决后提交。
- 其他版本控制系统
- 支持 SVN/Mercurial:File > Settings > Version Control 中配置 Subversion/Mercurial 的可执行路径并测试;随后通过 VCS 菜单执行相应操作。