阿里云主机折上折
  • 微信号
您当前的位置:网站首页 > 初次运行Git前的配置

初次运行Git前的配置

作者:陈川 阅读数:8499人阅读 分类: 开发工具

在使用Git进行版本控制之前,必须完成一些基础配置。这些配置包括用户信息、默认编辑器、行尾处理等,确保Git能够正确识别操作者并适应开发环境。

用户信息配置

首次运行Git需要设置全局用户名和邮箱,这些信息会附加在每次提交记录中。通过以下命令配置:

git config --global user.name "你的姓名"
git config --global user.email "你的邮箱@example.com"

如果想为特定项目使用不同身份,可以在项目目录中执行不带--global参数的相同命令:

git config user.name "项目专用姓名"
git config user.email "project-email@example.com"

默认文本编辑器设置

Git需要文本编辑器来编写提交信息,默认情况下会使用系统默认编辑器。可以通过以下命令修改:

git config --global core.editor "code --wait"  # 使用VSCode
git config --global core.editor "vim"        # 使用Vim

Windows用户可能需要指定完整路径:

git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -nosession"

行尾换行符处理

跨平台协作时需要统一换行符风格。Windows使用CRLF,而Linux/macOS使用LF:

# Windows系统配置
git config --global core.autocrlf true

# Linux/macOS系统配置
git config --global core.autocrlf input

仓库中可以添加.gitattributes文件强制统一标准:

* text=auto
*.js text eol=lf
*.html text eol=lf

别名配置

创建常用命令的快捷方式能显著提高效率:

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.unstage 'reset HEAD --'

高级别名示例(显示带分支图的日志):

git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

颜色输出配置

启用颜色输出使终端信息更易读:

git config --global color.ui auto
git config --global color.branch.current "yellow reverse"
git config --global color.status.added "green bold"

差异工具配置

配置可视化差异对比工具(以VSCode为例):

git config --global diff.tool vscode
git config --global difftool.vscode.cmd "code --wait --diff $LOCAL $REMOTE"

使用方式:git difftool <filename>

凭证存储配置

避免每次推送都输入密码:

# Windows
git config --global credential.helper wincred

# macOS
git config --global credential.helper osxkeychain

# Linux
git config --global credential.helper cache
git config --global credential.helper 'cache --timeout=3600'

忽略文件配置

全局忽略模板配置(如IDE配置文件):

git config --global core.excludesfile ~/.gitignore_global

.gitignore_global文件示例内容:

.DS_Store
.idea/
.vscode/
*.log
node_modules/

SSH密钥配置

生成SSH密钥并添加到Git服务商:

ssh-keygen -t ed25519 -C "your_email@example.com"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

~/.ssh/id_ed25519.pub内容添加到GitHub/GitLab的SSH Keys设置中。

默认分支名称修改

将默认分支名从master改为main:

git config --global init.defaultBranch main

自动修正配置

开启命令自动修正功能:

git config --global help.autocorrect 20  # 2秒后自动执行修正后的命令

查看所有配置

验证配置结果:

git config --list

或者查看特定配置项:

git config user.name
git config user.email

配置文件存储位置

Git配置存储在三个位置:

  • 系统级:/etc/gitconfig(Linux)
  • 用户级:~/.gitconfig~/.config/git/config
  • 仓库级:.git/config

优先级:仓库 > 用户 > 系统

多账户配置示例

开发者在不同平台使用不同账户时,可以创建条件配置:

[includeIf "gitdir:~/work/"]
    path = ~/work/.gitconfig

~/work/.gitconfig内容:

[user]
    name = 公司账号
    email = work@company.com

本站部分内容来自互联网,一切版权均归源网站或源作者所有。

如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn

前端川

前端川,陈川的代码茶馆🍵,专治各种不服的Bug退散符💻,日常贩卖秃头警告级的开发心得🛠️,附赠一行代码笑十年的摸鱼宝典🐟,偶尔掉落咖啡杯里泡开的像素级浪漫☕。‌