阿里云主机折上折
  • 微信号
Current Site:Index > Configuration before running Git for the first time

Configuration before running Git for the first time

Author:Chuan Chen 阅读数:8501人阅读 分类: 开发工具

Before using Git for version control, some basic configurations must be completed. These configurations include user information, default editor, line ending handling, etc., to ensure Git can correctly identify the operator and adapt to the development environment.

User Information Configuration

When running Git for the first time, you need to set a global username and email, which will be attached to every commit record. Configure them with the following commands:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

If you want to use a different identity for a specific project, execute the same commands without the --global parameter in the project directory:

git config user.name "Project-Specific Name"
git config user.email "project-email@example.com"

Default Text Editor Setup

Git requires a text editor to write commit messages. By default, it uses the system's default editor. You can change it with the following command:

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

Windows users may need to specify the full path:

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

Line Ending Handling

Cross-platform collaboration requires consistent line ending styles. Windows uses CRLF, while Linux/macOS uses LF:

# Windows system configuration
git config --global core.autocrlf true

# Linux/macOS system configuration
git config --global core.autocrlf input

You can add a .gitattributes file to the repository to enforce uniform standards:

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

Alias Configuration

Creating shortcuts for commonly used commands can significantly improve efficiency:

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 --'

Advanced alias example (displaying logs with branch graphs):

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"

Color Output Configuration

Enable color output to make terminal messages more readable:

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

Diff Tool Configuration

Configure a visual diff tool (using VSCode as an example):

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

Usage: git difftool <filename>

Credential Storage Configuration

Avoid entering passwords every time you push:

# 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'

Ignore File Configuration

Global ignore template configuration (e.g., IDE configuration files):

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

Example content for .gitignore_global:

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

SSH Key Configuration

Generate an SSH key and add it to your Git service provider:

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

Add the content of ~/.ssh/id_ed25519.pub to the SSH Keys settings in GitHub/GitLab.

Default Branch Name Modification

Change the default branch name from master to main:

git config --global init.defaultBranch main

Auto-Correction Configuration

Enable command auto-correction:

git config --global help.autocorrect 20  # Executes the corrected command after 2 seconds

View All Configurations

Verify the configuration results:

git config --list

Or check specific configuration items:

git config user.name
git config user.email

Configuration File Storage Locations

Git configurations are stored in three locations:

  • System-level: /etc/gitconfig (Linux)
  • User-level: ~/.gitconfig or ~/.config/git/config
  • Repository-level: .git/config

Priority: Repository > User > System

Multi-Account Configuration Example

For developers using different accounts on different platforms, conditional configurations can be created:

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

Content of ~/work/.gitconfig:

[user]
    name = Company Account
    email = work@company.com

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

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

Front End Chuan

Front End Chuan, Chen Chuan's Code Teahouse 🍵, specializing in exorcising all kinds of stubborn bugs 💻. Daily serving baldness-warning-level development insights 🛠️, with a bonus of one-liners that'll make you laugh for ten years 🐟. Occasionally drops pixel-perfect romance brewed in a coffee cup ☕.