Setting up Git in Linux is easy, but here are five things I did to get the perfect configuration:
\1. Create global configuration
\2. Set default name
\3. Set default email address
\4. Set default branch name
\5. Set default editor
I use Git to manage my code, command line scripts, and document versions. This means that every time I start a new task, first I need to create a directory of files and add them to the Git repository:
$ mkdir newproject$ cd newproject$ git init
There are some general settings I've always wanted. Not much, but it saves me from having to configure it every time. I like to take advantage of Git's global configuration capabilities.
Git provides the git config
command for manual configuration, but there are some caveats. For example, typically you would set up an email address. You can set this by running the git config user.email your email address
command. However, this will only work within the Git directory you are currently in.
$ git config user.email alan@opensource.comfatal: not in a git directory
Additionally, when this command is run in a Git repository, it will only configure a specific repository. In the new repository, you have to repeat this step. I can avoid duplication with global configuration. The option --global
will instruct Git to write the email address into the global configuration ~/.gitconfig
file and even create it if necessary:
“
Remember, the tilde (
~
) represents your home folder. On my computer it is/home/alan
.”
$ git config -- global user.email alan@opensource.com$ cat ~/.gitconfig[user] email = alan@opensour ce.com
The disadvantage here is that if you have a lot of preferences and need to enter a lot of commands, this will take a lot of time and be error-prone. Git provides a faster and more efficient way to edit your global configuration files directly - this is the first item on my list!
If you are just starting to use Git, maybe you don't have this file yet. Don't worry, let's get straight to it. Just use the --edit
option:
$ git config --global --edit
If the file does not exist, Git will create a new file with the following content and open it using your terminal's default editor:
# This is Git's per-user configuration file. [user]# Please adapt and uncomment the following lines:# name = Alan# email = alan@hopper~~~"~/.gitconfig" 5L, 155B 1,1 All
Now that we have opened the editor and Git has created a global configuration file in the background, we can continue with the next settings.
The name is the first entry in the file, let's start with it. Setting my name from the command line is git config --global user.name "Alan Formy-Duval"
. Instead of running this command from the command line, just edit the name
entry in the configuration file:
name = Alan Formy-Duval
邮箱地址是第二个条目,让我们添加它。默认情况下,Git 使用你的系统提供的名称和邮箱地址。如果不正确或者你想要更改,你可以在配置文件中具体说明。事实上,如果你没有配置这些,Git 在你第一次提交时会友好的提示你:
Committer: Alan Your name and email address were configured automatically bas edon your username and hostname. Please check that they are accurate....
在命令行中运行 git config --global user.email "alan@opensource.com"
会设置好我的邮箱。同样,我们在配置文件中编辑 email
条目,提供你的邮箱地址:
email = alan@opensource.com
我喜欢设置的最后两个设置是默认分支名称和默认编辑器。当你仍在编辑器中时,需要添加这些指令。
目前有一种趋势,即不再使用 master
作为默认分支名称。事实上,在新存储库初始化时,Git 将通过友好的消息提示更改默认分支名称:
$ git inithint: Using 'master' as the name for the initial branch. This default branch nam ehint: is subject to change. To configure the initial branch name to use in allhint: of y our new repositories, which will suppress this warning, call:hint:hint: git config -- global init.defaultBranch
这个名为 defaultBranch
的指令需要位于一个名为 init
的新部分中。现在普遍接受的是,许多程序员使用 main
这个词作为他们的默认分支。这是我喜欢使用的。将此部分后跟指令添加到配置中:
[init] defaultBranch = main
第五个设置是设置默认的编辑器。这是指 Git 将使用的编辑器,用于在你每次将更改提交到存储库时输入你的提交消息。不论是 nano🔗 opensource.com、emacs🔗 opensource.com、vi🔗 opensource.com 还是其他编辑器,每个人都有他喜欢的。我喜欢用 vi。添加 core
部分,并设置 editor
指令为你喜欢的编辑器。
[core] editor = vi
这是最后一项。退出编辑器。Git 在主目录下保存全局配置文件。如果你再次运行编辑命令,将会看到所有内容。注意配置文件是明文存储的文本文件,因此它可以很容易使用文本工具查看,如 cat🔗 opensource.com 命令。这是我的配置文件内容:
$ cat ~/.gitconfig[user] email = alan@opensource.com name = Alan Formy- Duval[core] editor = vi[init] defaultBranch = main
这是一个简单的指南,可以让你快速开始使用 Git 和它的一些配置选项。
The above is the detailed content of 5 Git configurations I use in Linux. For more information, please follow other related articles on the PHP Chinese website!