it是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。ios
Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。c++
Git 与经常使用的版本控制工具 CVS, Subversion 等不一样,它采用了分布式版本库的方式,没必要服务器端软件支持。git
安装环境:github
[root@nagios_client2 git]# cat /etc/redhat-release shell
CentOS release 6.9 (Final)缓存
[root@nagios_client2 git]# uname -r bash
2.6.32-696.el6.x86_64服务器
[root@nagios_client2 git]# cd /root/tool/git/app
#安装依赖包:ssh
yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel -y
yum install gcc-c++ perl-ExtUtils-MakeMaker -y
#安装git:
wget https://www.kernel.org/pub/software/scm/git/git-2.9.5.tar.gz
tar zxvf git-2.9.5.tar.gz
cd git-2.9.5
make configure
./configure --prefix=/usr/local/git
make profix=/usr/local/git
make install
#加入环境变量:
echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/profile
source /etc/profile
#检查安装版本:
git --version
显示结果:git version 2.9.5
Git 建立仓库:
[root@nagios_client2 ~]# mkdir /home/learngit
[root@nagios_client2 ~]# cd /home/learngit/
初始化:
[root@nagios_client2 learngit]# git init
Initialized empty Git repository in /home/learngit/.git/
会生成一个隐藏的.git目录,全部 Git 须要的数据和资源都存放在这个目录中。
若是当前目录下有几个文件想要归入版本控制,须要先用 git add 命令告诉 Git 开始对这些文件进行跟踪,而后提交:
touch file1.txt
git add file1.txt
git commit -m 'filesersion'
显示以下结果:
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'root@nagios_client2.(none)')
运行:
it config --global user.email "441009395@qq.com"
git config --global user.email "alone"
git commit -m 'filesersion'
显示:
[master (root-commit) 5a61707] filesersion
1 file changed, 0 insertions(+), 0 deletions(-)
编辑file1.txt文件
vi file1.txt
Git is a distributed version control system.
Git is free software.
运行git status命令看看结果:
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: file1.txt
no changes added to commit (use "git add" and/or "git commit -a")
git status命令可让咱们时刻掌握仓库当前的状态,上面的命令告诉咱们,readme.txt被修改过了,但尚未准备提交的修改。
虽然Git告诉咱们readme.txt被修改了,但若是能看看具体修改了什么内容,天然是很好的。好比你休假两周从国外回来,第一天上班时,已经记不清上次怎么修改的readme.txt,因此,须要用git diff这个命令看看:
git diff file1.txt
显示结果:
diff --git a/file1.txt b/file1.txt
index e69de29..9247db6 100644
--- a/file1.txt
+++ b/file1.txt
@@ -0,0 +1,2 @@
+Git is a distributed version control system.
+Git is free software.
git reset HEAD
git reset HEAD 命令用于取消已缓存的内容。
git rm
git rm 会将条目从缓存区中移除。这与 git reset HEAD 将条目取消缓存是有区别的。 "取消缓存"的意思就是将缓存区恢复为咱们作出修改以前的样子。
默认状况下,git rm file 会将文件从缓存区和你的硬盘中(工做目录)删除。
若是你要在工做目录中留着该文件,可使用 git rm --cached:
git mv
git mv 命令用于移动或重命名一个文件、目录、软链接。
一
Git 远程仓库(Github)
Git 并不像 SVN 那样有个中心服务器。
目前咱们使用到的 Git 命令都是在本地执行,若是你想经过 Git 分享你的代码或者与其余开发人员合做。 你就须要将数据放到一台其余开发人员可以链接的服务器上。
添加远程库
要添加一个新的远程仓库,能够指定一个简单的名字,以便未来引用,命令格式以下:
git remote add [shortname] [url]
本例以Github为例做为远程仓库,若是你没有Github能够在官网https://github.com/注册。
因为你的本地Git仓库和GitHub仓库之间的传输是经过SSH加密的,因此咱们须要配置验证信息:
使用如下命令生成SSH Key:
$ ssh-keygen -t rsa -C "youremail@example.com"
后面的 your_email@youremail.com 改成你在 github 上注册的邮箱,以后会要求确认路径和输入密码,咱们这使用默认的一路回车就行。成功的话会在~/下生成.ssh文件夹,进去,打开 id_rsa.pub,复制里面的 key。
回到 github 上,进入 Account => Settings(帐户配置)。
二
搭建git私有服务器:
Git 服务器搭建
远程仓库使用了 Github,Github 公开的项目是免费的,可是若是你不想让其余人看到你的项目就须要收费。
这时咱们就须要本身搭建一台Git服务器做为私有仓库使用。
一、安装Git
$ yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel
$ yum install git
接下来咱们 建立一个git用户组和用户,用来运行git服务:
$ groupadd git
$ adduser git -g git
二、建立证书登陆
收集全部须要登陆的用户的公钥,公钥位于id_rsa.pub文件中,把咱们的公钥导入到/home/git/.ssh/authorized_keys文件里,一行一个。
若是没有该文件建立它:
$ cd /home/git/
$ mkdir .ssh
$ chmod 700 .ssh
$ touch .ssh/authorized_keys
$ chmod 600 .ssh/authorized_keys
三、初始化Git仓库
首先咱们选定一个目录做为Git仓库,假定是/home/gitrepo/runoob.git,在/home/gitrepo目录下输入命令:
$ cd /home
$ mkdir gitrepo
$ chown git:git gitrepo/
$ cd gitrepo
$ git init --bare runoob.git
Initialized empty Git repository in /home/gitrepo/runoob.git/
以上命令Git建立一个空仓库,服务器上的Git仓库一般都以.git结尾。而后,把仓库所属用户改成git:
$ chown -R git:git runoob.git
四、克隆仓库
$ git clone git@23.247.78.254:/home/gitrepo/runoob.git
Cloning into 'runoob'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.
23.247.78.254 为 Git 所在服务器 ip
这样咱们的 Git 服务器安装就完成了,接下来咱们能够禁用 git 用户经过shell登陆,能够经过编辑/etc/passwd文件完成。找到相似下面的一行:
git:x:503:503::/home/git:/bin/bash
改成:
git:x:503:503::/home/git:/sbin/nologin