Git settings

更新时间:
复制 MD 格式

This topic describes how to perform the initial Git configuration before you use it for version control.

Prerequisites

Make sure Git is installed on your system. You can download and install the correct version for your operating system from the official Git website.

Initial Git configuration

You only need to configure the Git operating environment once on a new system. This configuration is preserved across upgrades. You can also modify the configuration at any time. For more information, see the Git-scm book and Pro Git.

Set user information

Run the following commands to set your username and email address. This information is included in every commit that you create.

git config --global user.name "Your Name"      # Replace this with your username
git config --global user.email "your@email"      # Replace this with your company email

Set command aliases

Git supports command aliases, which can simplify commands. You can also use aliases to replicate familiar commands from other version control systems. For example, the following commands create aliases that are compatible with SVN commands.

  • Set global command aliases

    # To make an alias available in all Git repositories, use the --global option:
    git config --global alias.co checkout
    git config --global alias.br branch
    git config --global alias.ci "commit -s"
    git config --global alias.st status
  • Set local command aliases

    # To set an alias only for the current repository, omit the --global option:
    git config alias.co checkout
    git config alias.br branch
    git config alias.ci commit
    git config alias.st status
  • Set custom aliases

    # Customize aliases as needed. For example, if you often use git push origin master, you can set it as an alias: git pushm
    git config --global alias.pushm "push origin master"

Chinese encoding settings

To correctly display directories and files that contain Chinese characters, apply the following Git settings:

  1. Open Git Bash and set the Git output encoding to UTF-8:

    git config --global core.quotepath false
    git config --global i18n.commit.encoding utf-8
    git config --global i18n.logoutputencoding utf-8
  2. Set the terminal encoding to UTF-8.

    Make sure your terminal, such as Git Bash, PowerShell, or CMD, uses UTF-8 encoding. In Git Bash, you can set the encoding with the following command:

    export LANG=en_US.UTF-8
  3. Set the operating system locale. Make sure the operating system locale supports Chinese and uses UTF-8 encoding.

    • In Windows, you can adjust this setting in the Region settings in the Control Panel.

    • In Linux, you can modify the /etc/locale.conf file or use the localectl command.

Other settings

Git usually enables colored output by default.

  1. Open Git Bash.

  2. Run the following commands:

    sudo git config --system color.ui auto

    When you execute a merge operation, the merge commit message is auto-generated and includes the concise commit messages from the source branch:

    git config --global merge.log true

    To automatically squash commits prefixed with "fixup!" onto the matching commit during an interactive rebase:

    git config --global rebase.autosquash true

Windows line feed issues

Windows users can configure core.autocrlf to automatically convert line endings. However, inconsistent configurations among collaborators can cause issues with line endings. To prevent this, add a .gitattributes file to the repository to enforce a standard line ending format. For more information, see Pro Git.

Check the configuration

After you complete the configuration, run the following command to verify your settings.

git config --global --list