Linux 下 Git 的安装及使用方法(gitlinux下安装)

Git是当前最流行的分布式版本控制系统,可以用来管理多个文件及代码版本之间的变化,本文将介绍如何在Linux操作系统下安装Git以及如何使用Git。

一、安装Git

1.使用 yum 或 apt-get 安装Git

在Linux平台,最简单的安装Git的方式是使用 yum 或 apt-get 命令,具体代码如下:

// 使用 yum 安装
[root@localhost]# yum install git

// 使用 apt-get 安装
[root@localhost]# apt-get install git

2、使用源码安装

另外,也可以使用源码安装。具体流程是:

(1)下载 Git 源码,比如 git-2.20.1.tar.gz

(2)解压下载的源码,具体代码如下:

[root@localhost]# tar -zvxf git-2.20.1.tar.gz

(3)进入源码根文件夹,相关代码如下:

[root@localhost]# cd git-2.20.1

(4)编译安装

[root@localhost]# make prefix=/usr/local/git all
[root@localhost]# make prefix=/usr/local/git install

安装好Git之后,可以使用以下命令测试安装是否成功:

[root@localhost]# git --version

成功的话会显示出当前是git的版本号码,比如:

git version 2.20.1

二、Git的使用

1.初始化

新安装的Git,需要初步设置一下Git环境,可以使用以下命令进行初始化:

[root@localhost]# git config --global user.name "FreeBin" 
[root@localhost]# git config --global user.email freebin@.com

2.添加新仓库

新建一个名为GitDemo的文件夹,使用以下命令在该文件夹内创建一个Git仓库:

[root@localhost]# git init

执行完成之后,会在该文件夹内多出一个.git文件,即创建了该目录下的Git仓库。

3.添加文件到Git仓库

现在此文件夹内有一个test.txt文件,可以使用以下命令添加到Git仓库:

[root@localhost]# git add test.txt

4.提交修改

添加完新文件后,可以使用以下命令提交修改:

[root@localhost]# git commit -m "First commit"
[root@localhost]# git commit --amend

5.克隆Git仓库

如果想在另一台电脑上克隆Git仓库,可以把远程仓库clone到本地,具体代码如下:

[root@localhost]# git clone http://.git

以上就是Linux操作系统下安装Git以及如何使用Git的教程,希望对大家有所帮助。


数据运维技术 » Linux 下 Git 的安装及使用方法(gitlinux下安装)