命令范围:SSH Key 与剪贴板示例适用于带 OpenSSH 的 macOS / 类 Unix 终端,
pbcopy为 macOS 命令;服务端账户配置以 Linux 为例。新建密钥前先检查现有文件,避免覆盖。
ssh key 配置
一、生成SSH Key
1 、检查.ssh文件夹是否存在
$ ls -al ~/.ssh2、如果不存在,新建 .ssh 文件夹并限制目录权限:
$ mkdir -p ~/.ssh
$ chmod 700 ~/.ssh3、使用支持 Ed25519 的 OpenSSH 生成密钥,将示例邮箱改为自己的邮箱:
$ cd ~/.ssh
$ ssh-keygen -t ed25519 -C "your_email@example.com"按提示选择保存位置和密钥口令;使用默认位置时生成 id_ed25519 与 id_ed25519.pub。公钥用于上传,私钥应留在本机。
4、查看公钥
$ cat id_ed25519.pub5、将公钥复制到剪贴板(macOS):
$ pbcopy <id_ed25519.pubgit服务器禁止用户shell登录
(1)禁止的原因
为了进行团队项目开发,我们经常会使用分布式版本管理系统,其中git是现今最为重要和常用的分布式管理系统。 GitHub Free 支持公开和私有仓库;私有仓库的部分协作功能受套餐限制。是否自建 Git 服务器,应依据部署环境、访问控制和运维需求决定,而不是因为免费账户不能建私有仓库。 在建立好git服务器后,大家都可以通过
$ git clone git@10.3.0.99:project.git克隆代码到本地。 这同时说明了大家也可以通过
$ ssh git@10.3.0.99ssh连接服务器,登录到服务器上,对服务器进行各种操作,这通常很不安全,也不合适,成员只需要能对仓库操作就可以了,不需要更大的权限。
(2)禁止方法
编辑/etc/passwd文件完成。找到类似下面的一行:
git:x:1001:1001:,,,:/home/git:/bin/bash
改为:
git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell这样可限制交互式 shell,同时允许 Git 的 SSH 命令;实际可执行文件路径、账户策略与 git-shell-commands 配置仍需按服务器环境核对。
Git 将一个项目同时推送到多个远程仓库
- 解决方法
查看远程仓库的情况,
git remote -v输出如下,可以看到只有一个叫 origin 的远程仓库。
origin git@github.com:xxx/xxx-ai.git (fetch)
origin git@github.com:xxx/xxx-ai.git (push)使用如下命令添加远程仓库。(预先创建好一个空库)
git remote set-url --add origin git@192.168.31.111:xxx/xxx-ai.git查看远程仓库情况,
git remote -v可以看到 origin 远程仓库有两个 push 地址。
origin git@github.com:xxx/xxx-ai.git (fetch)
origin git@github.com:xxx/xxx-ai.git (push)
origin git@192.168.31.111:xxx/xxx-ai.git (push)前端代码git规范
(uftp_eff_20250301)[马明娟]fix(效能中心|20250301):贯标字段调整
特别注意
注意
在项目中统一配置用户信息
git config user.name 马明娟 # Set the repository-local user name下面这句每个项目下面要单独执行,不然提交代码时项目信息展示为空
git config user.project uftp_eff_20250301 # 配置所属项目
git config user.projectCn 效能中心 # 配置项目中文名方法一
通过git 命令配置内容
git config --global user.name 马明娟 # 配置全局用户名
git config user.project uftp_eff_202503 # 配置所属项目
git config user.projectCn 效能中心 # 配置项目中文名
git config --global alias.fix '!f() { git commit -m "($(git config user.project))[$(git config user.name)]fix($(git config user.projectCn)|$(date +%Y%m%d)):$1"; }; f' # 修复bug
git config --global alias.feat '!f() { git commit -m "($(git config user.project))[$(git config user.name)]fix($(git config user.projectCn)|$(date +%Y%m%d)):$1"; }; f' # 新功能
git config --global alias.docs '!f() { git commit -m "($(git config user.project))[$(git config user.name)]fix($(git config user.projectCn)|$(date +%Y%m%d)):$1"; }; f' # 文档
git config --global alias.style '!f() { git commit -m "($(git config user.project))[$(git config user.name)]fix($(git config user.projectCn)|$(date +%Y%m%d)):$1"; }; f' # 格式,不影响代码运行的变动
git config --global alias.refactor '!f() { git commit -m "($(git config user.project))[$(git config user.name)]fix($(git config user.projectCn)|$(date +%Y%m%d)):$1"; }; f' # 重构,既不是新功能,也不是修改bug
git config --global alias.chore '!f() { git commit -m "($(git config user.project))[$(git config user.name)]fix($(git config user.projectCn)|$(date +%Y%m%d)):$1"; }; f' # 构建过程或辅助工具的变动
git config --global alias.revert '!f() { git commit -m "($(git config user.project))[$(git config user.name)]fix($(git config user.projectCn)|$(date +%Y%m%d)):$1"; }; f' # 撤销,版本回退
git config --global alias.pref '!f() { git commit -m "($(git config user.project))[$(git config user.name)]fix($(git config user.projectCn)|$(date +%Y%m%d)):$1"; }; f' # 性能优化
git config --global alias.test '!f() { git commit -m "($(git config user.project))[$(git config user.name)]fix($(git config user.projectCn)|$(date +%Y%m%d)):$1"; }; f' # 测试
git config --global alias.improvement '!f() { git commit -m "($(git config user.project))[$(git config user.name)]fix($(git config user.projectCn)|$(date +%Y%m%d)):$1"; }; f' # 改进
git config --global alias.build '!f() { git commit -m "($(git config user.project))[$(git config user.name)]fix($(git config user.projectCn)|$(date +%Y%m%d)):$1"; }; f' # 打包
git config --global alias.ci '!f() { git commit -m "($(git config user.project))[$(git config user.name)]fix($(git config user.projectCn)|$(date +%Y%m%d)):$1"; }; f' # 持续集成
git config --global alias.update '!f() { git commit -m "($(git config user.project))[$(git config user.name)]fix($(git config user.projectCn)|$(date +%Y%m%d)):$1"; }; f' # 更新方法二
通过shell脚本配置
#!/bin/bash
# 定义用户信息
USER_NAME=$(git config user.name)
USER_PROJECT=$(git config user.project)
USER_PROJECTCN=$(git config user.projectCn)
# 定义数组
aliases = ("fix", "feat", "docs", "Style", "refactor", "chore", "revert", "pref", "test", "improvement", "build", "ci", "update")
# 循环遍历数组并配置别名
for alias in "${aliases[@]}"; do
git config --global alias."$alias" '!f() { git commit -m "($(USER_PROJECT))[$(USER_NAME)]$(alias)($(USER_PROJECTCN)|$(date +%Y%m%d));$1"; }; f'
done
echo "Git aliases with user information have been set."运行脚本
- 将脚本保存为setup_git_aliases_with_user.sh
- 确保你已经配置了Git用户信息(git config user.name 和git config user.project 和git config user.projectCn)
- 给脚本添加执行权限
chmod +x setup_git_aliases_with_user.sh- 运行脚本
./setup_git_aliases_with_user.sh方法三
手动更新用户信息
这里是根据分支名称来配置
git config --global --edit配置文件中的内容
[alias]
fix="!f(){ branch=$(git rev-parse --abbrev-ref HEAD); git commit -m \"($branch)[马明娟]fix(效能中心|$(date +%Y%m%d)); $1\"; }; f" # 修复bug
feat="!f(){ branch=$(git rev-parse --abbrev-ref HEAD); git commit -m \"($branch)[马明娟]feat(效能中心|$(date +%Y%m%d)); $1\"; }; f" # 新功能
docs="!f(){ branch=$(git rev-parse --abbrev-ref HEAD); git commit -m \"($branch)[马明娟]docs(效能中心|$(date +%Y%m%d)); $1\"; }; f" # 文档
style="!f(){ branch=$(git rev-parse --abbrev-ref HEAD); git commit -m \"($branch)[马明娟]style(效能中心|$(date +%Y%m%d)); $1\"; }; f" # 格式,不影响代码运行的变动
refactor="!f(){ branch=$(git rev-parse --abbrev-ref HEAD); git commit -m \"($branch)[马明娟]refactor(效能中心|$(date +%Y%m%d)); $1\"; }; f" # 重构,既不是新功能,也不是修改的bug
chore="!f(){ branch=$(git rev-parse --abbrev-ref HEAD); git commit -m \"($branch)[马明娟]chore(效能中心|$(date +%Y%m%d)); $1\"; }; f" # 构建过程或辅助工具的变动
revert="!f(){ branch=$(git rev-parse --abbrev-ref HEAD); git commit -m \"($branch)[马明娟]revert(效能中心|$(date +%Y%m%d)); $1\"; }; f" # 撤销,版本回退
pref="!f(){ branch=$(git rev-parse --abbrev-ref HEAD); git commit -m \"($branch)[马明娟]pref(效能中心|$(date +%Y%m%d)); $1\"; }; f" # 性能优化
test="!f(){ branch=$(git rev-parse --abbrev-ref HEAD); git commit -m \"($branch)[马明娟]test(效能中心|$(date +%Y%m%d)); $1\"; }; f" # 测试
improvement="!f(){ branch=$(git rev-parse --abbrev-ref HEAD); git commit -m \"($branch)[马明娟]improvement(效能中心|$(date +%Y%m%d)); $1\"; }; f" # 改进
build="!f(){ branch=$(git rev-parse --abbrev-ref HEAD); git commit -m \"($branch)[马明娟]build(效能中心|$(date +%Y%m%d)); $1\"; }; f" # 打包
ci="!f(){ branch=$(git rev-parse --abbrev-ref HEAD); git commit -m \"($branch)[马明娟]ci(效能中心|$(date +%Y%m%d)); $1\"; }; f" # 持续集成
update="!f(){ branch=$(git rev-parse --abbrev-ref HEAD); git commit -m \"($branch)[马明娟]update(效能中心|$(date +%Y%m%d)); $1\"; }; f" # 更新