git基础配置
# ssh key 配置
一、生成SSH Key
1 、检查.ssh文件夹是否存在
$ ls -al ~/.ssh
2、如果不存在新建.ssh文件平
$ mkdir ~/.ssh
3、生成KEY在命令行中输入,your_email@example.com换成自己人Email。
$ cd ~/.ssh
$ ssh-keygen -t rsa -C "your_email@example.com"
2
3
系统提示输入文件保存位置等信息,连续按三次回车即可,生成的SSH key文件的保存路径会在终端中给出:id_rsa id_rsa.pub
4、查看公钥
$ cat id_rsa.pub
5、将公钥复制过程剪贴板
$ pbcopy <id_rsa.pub
# git服务器禁止用户shell登录
(1)禁止的原因
为了进行团队项目开发,我们经常会使用分布式版本管理系统,其中git是现今最为重要和常用的分布式管理系统。 著名的gitHub是在git系统的基础上为各种有内容托管需求的人提供托管服务的一个网站,但是GitHub的免费服务只能创建公开的仓库。如果我们不想把自己的代码开源,想私有代码,那么建立一台git服务器也是一个不错的选择。 在建立好git服务器后,大家都可以通过
$ git clone git@10.3.0.99:project.git
克隆代码到本地。 这同时说明了大家也可以通过
$ ssh git@10.3.0.99
ssh连接服务器,登录到服务器上,对服务器进行各种操作,这通常很不安全,也不合适,成员只需要能对仓库操作就可以了,不需要更大的权限。
(2)禁止方法
编辑/etc/passwd文件完成。找到类似下面的一行:
git:x:1001:1001:,,,:/home/git:/bin/bash
改为:
git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell
2
3
这样,Git用户可以正常通过ssh使用git,但无法登录shell,因为我们为git用户指定的git-shell每次一登录就自动退出。
# Git 将一个项目同时推送到多个远程仓库
- 解决方法
查看远程仓库的情况,
git remote -v
2
输出如下,可以看到只有一个叫 origin 的远程仓库。
origin git@github.com:xxx/xxx-ai.git (fetch)
origin git@github.com:xxx/xxx-ai.git (push)
2
使用如下命令添加远程仓库。(预先创建好一个空库)
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)
2
3
4
# 前端代码git规范
(uftp_eff_20250301)[马明娟]fix(效能中心|20250301):贯标字段调整
特别注意
注意
在项目中统一配置用户信息
git config user.name 马明娟 # 配置全局用户名
下面这句每个项目下面要单独执行,不然提交代码时项目信息展示为空
git config user.project uftp_eff_20250301 # 配置所属项目
git config user.projectCn 效能中心 # 配置项目中文名
2
# 方法一
通过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' # 更新
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
30
# 方法二
通过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."
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
运行脚本
- 将脚本保存为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" # 更新
2
3
4
5
6
7
8
9
10
11
12
13
14
15