Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Git installation and configuration

Git installation and configuration

Before using Git, we need to install Git first.

Git currently supports running on Linux/Unix, Solaris, Mac, and Windows platforms.

Git installation package download addresses for each platform are:http://git-scm.com/downloads


Installation on Linux platform

Major Linux platforms can be installed using package managers (apt-get, yum, etc.).

The command to install the latest stable version of Git on Debian/Ubuntu is:



apt-get install git

Centos/RedHat

If your system is Centos/RedHat, the installation command is:



yum -y install git-core

Fedora installation command:

# yum install git (Fedora 21 及之前的版本)

# dnf install git (Fedora 22 and newer versions)

FreeBSD installation command:

pkg install git

OpenBSD installation command:

pkg_add git

Alpine installation command:

apk add git

Installing from source

We can also download the source package from the official website to install; the latest source package download address:https://mirrors.edge.kernel.org/pub/software/scm/git/。

You can also clone the source package on GitHub:

git clone https://github.com/git/git

Extract and install the downloaded source package:

$ tar -zxf git-1.7.2.2.tar.gz

$ cd git-1.7.2.2

$ make prefix=/usr/local all

$ sudo make prefix=/usr/local install


Installation on Windows platform

Installing Git on Windows is just as easy; a project called msysGit provides an installation package. You can download the exe installation file from the GitHub page and run it:

Installation package download address:https://gitforwindows.org/

You can also download directly from the official website:https://git-scm.com/download/win。

After downloading, double-click the installation package; the interface opens as shown below. Click the "next" button to start the installation:

After completing the installation, you can use the command-line git tool (which already includes an ssh client), and there is also a graphical Git project management tool.

Find in the Start menu "Git"->"Git Bash", the Git command window will pop up, where you can perform Git operations.

Using the winget tool

If you have already installed winget, you can use the following command to install:



winget install --id Git.Git -e --source winget


Installation on Mac platform

Install via Homebrew:



brew install git

If you want to install git-gui and gitk (Git's commit GUI and interactive history browser), you can install them using homebrew:

brew install git-gui

You can also use a graphical Git installation tool; the download address is:

https://sourceforge.net/projects/git-osx-installer/

The installation interface is shown below:


Git configuration

Git provides a command called git config, used to configure or read the corresponding working environment variables.

These environment variables determine how Git works and behaves in various aspects.

These variables can be stored in the following three different places:

On Windows systems, Git looks for the .gitconfig file in the user's home directory. The home directory is the directory specified by the $HOME variable, usually C:\Documents and Settings$USER.

In addition, Git also tries to find the /etc/gitconfig file, but it uses the directory where Git was installed as the root directory for locating it.

User information

Configure personal user name and email address, so that the committer's information is recorded with each code commit:



git config --global user.name "runoob"

git config --global user.email test@runoob.com

If you use the --global option, then the configuration file changed is the one in your user's home directory, and all your future projects will use the user information configured here by default.

If you want to use a different name or email in a specific project, just remove the --global option and reconfigure; the new settings are saved in the current project's .git/config file.

Text Editor

Set the default text editor used by Git, which is generally Vi or Vim. If you have other preferences, such as VS Code, you can reset it:



git config --global core.editor "code --wait"

Difference analysis tool

Another commonly used setting is which difference analysis tool to use when resolving merge conflicts. For example, to switch to vimdiff:



git config --global merge.tool vimdiff

Git can understand the output from merge tools such as kdiff3, tkdiff, meld, xxdiff, emerge, vimdiff, gvimdiff, ecmerge, and opendiff.

Of course, you can also specify a tool you developed yourself; see Chapter 7 for details.

Viewing configuration information

To check existing configuration information, you can use the git config --list command:



$ git config --list

http.postbuffer=2M

user.name=runoob

user.email=test@runoob.com

Sometimes you may see duplicate variable names, which means they come from different configuration files (such as /etc/gitconfig and ~/.gitconfig), but ultimately Git actually uses the last one.

We can also view these configurations in ~/.gitconfig or /etc/gitconfig , as shown below:



vim ~/.gitconfig 

The displayed content is shown below:



[http]

    postBuffer = 2M

[user]

    name = runoob

    email = test@runoob.com

You can also directly look up the setting of a particular environment variable by simply following it with the specific name, like this:



$ git config user.name

runoob

Generate SSH key (optional)

If you need to perform Git operations over SSH, you can generate an SSH key and add it to your Git hosting service (such as GitHub, GitLab, etc.).



ssh-keygen -t rsa -b 4096 -C "your.email@example.com"

Complete the generation process as prompted, then add the generated public key to the corresponding platform.

Verify the installation

Run the following command in a terminal or command line to ensure Git is properly installed and configured:



git --version

git config --list