码迷,mamicode.com
首页 > 其他好文 > 详细

git 安装与基本操作

时间:2017-08-13 23:20:03      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:git

it是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。

Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。

Git 与常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,不必服务器端软件支持。


安装环境:

[root@nagios_client2 git]# cat /etc/redhat-release 

CentOS release 6.9 (Final)

[root@nagios_client2 git]# uname -r                

2.6.32-696.el6.x86_64


[root@nagios_client2 git]# cd /root/tool/git/

#安装依赖包:

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服务器作为私有仓库使用。


1、安装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


2、创建证书登录

收集所有需要登录的用户的公钥,公钥位于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


3、初始化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


4、克隆仓库

$ 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


git 安装与基本操作

标签:git

原文地址:http://butterflykiss.blog.51cto.com/3354010/1955926

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!