Centos7 编译安装 Git 最新版

一、假如系统已经安装了git,先删除。

  1. 如果是通过yum安装的,直接在终端使用以下指令删除:

    yum -y remove git
  2. 如果是通过源码编译安装的,参考以下文章:

    Linux ./configure && make && make install 编译安装和卸载

二、通过源码安装git。源码可以安装到最新版本的git,安装包的方式虽然安装容易卸载也容易,可惜版本总有延后性。对于技术人员来说,要敢于尝试新东西,敢于踩坑。

  1. 确保安装gcc、g++以及编译git所需要的包

    --安装gcc
    yum -y install gcc
    
    --安装g++
    yum -y install gcc-c++
    
    --安装编译所需的包
    yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
    yum -y install gcc perl-ExtUtils-MakeMaker
  2. 下载源码(*.tar.gz)到指定的目录

    --下载文件到/usr/local目录
    
    wget -P /usr/local https://www.kernel.org/pub/software/scm/git/git-2.9.5.tar.gz
  3. 切换到刚刚保存下载文件的目录并解压文件

    --切换到指定目录
    cd /usr/local/git-2.9.5
    
    --解压源码包
    tar zxvf git-2.9.5.tar.gz
  4. 进入解压目录

    cd /usr/local/git-2.9.5
  5. 配置安装目录并编译和安装

    ./configure --prefix=/usr/local/git-2.9.5 && make install
  6. 将编译好的git-2.9.5 bin目录添加到bashrc中,相当于添加全局变量

    echo 'export PATH=$PATH:/usr/local/git-2.9.5/bin' > /etc/profile.d/git.sh
  7. 使bashrc立即生效,全局变量立即生效

    source /etc/bashrc
  8. 查看git版本

    git --version
  9. 设置自己的用户名和邮箱

    命令行输入:(引号加不加无所谓)

    1
    2
    $ git config --global user.name "Your Name"
    $ git config --global user.email "[email protected]"
  10. 在本地创建仓库并初始化
    1
    2
    3
    $ mkdir test
    $ cd test
    $ git init
  11. 初始化空的 Git 版本库于 /usr/local/test/.git/
    至此git安装完成 

    在linux上安装完git,咱也应该去github注册帐号了。github网址:https://github.com/

    相信注册一个帐号对大家没什么问题的把。在这里不添加教程。实在需要帮助请留言。

    咱已经在linux创建好了一个仓库,接下来只在github创建一个仓库将这两个仓库关联就可以了。

    在github首页右下角,点击New repository。

三、使用Github

Centos7 编译安装 Git 最新版

Centos7 编译安装 Git 最新版

接下来咱就是要把git和github的库关联起来。要实现他们的远程同步就是需要用到SSH Key,github拿到了你的公钥就会知道内容是你推送的。

执行:ssh-****** -t rsa -C '你github的帐号'

1. cd /root/.ssh/
2. ssh-****** -t rsa -C '[email protected]'  
   # '[email protected]' 为 你github的帐号 

在这个目录下的id_rsa.pub里面的内容就是我们要的SSH Key,打开id_rsa.pub,将里面所有的内容复制到githubsetting里面的SSH and GPG keys的SSH 的 key文本库,title自拟。

当然你不仅可以在github根据界面查看是否成功,也可以在git测试是否成功,失败百度下是什么原因。

当然你不仅可以在github根据界面查看是否成功,也可以在git测试是否成功,失败百度下是什么原因。

vim id_rsa.pub
ssh -T [email protected]      
#测试是否连接成功

Centos7 编译安装 Git 最新版

Centos7 编译安装 Git 最新版

Centos7 编译安装 Git 最新版

Centos7 编译安装 Git 最新版

Centos7 编译安装 Git 最新版

Centos7 编译安装 Git 最新版

接下来咱将目录切换到咱的git的仓库中。新建一个文件,在里面写上 I am leaning git.

Centos7 编译安装 Git 最新版

接下来:添加test.txt到git并提交

  1. git add test.txt
  2. git commit -m test.txt

Centos7 编译安装 Git 最新版

linux的仓库也要去关联github的仓库。第二次使用就不用这行命令,以为已经关联了

克隆项目:

git clone [email protected]:SinalVee/Test(此处SinalVee为用户名,Test是你在github上创建的仓库)
正克隆到 'Test'...
Warning: Permanently added the RSA host key for IP address '192.30.252.131' to the list of known hosts.
remote: Counting objects: 6, done.
remote: Total 6 (delta 0), reused 0 (delta 0)
接收对象中: 100% (6/6), 完成.
检查连接... 完成。
1. git remote add origin [email protected]:BeautifulMeet/gitDemo.git
# 这个就是你github的地址 [email protected]:BeautifulMeet/gitDemo.git 替换成你的

接下来测试以下上传

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ cd Test/
$ ls
README.md  test.txt
$ touch hello.txt
$ vim hello.txt
你好,这是一个测试文件。
$ ls
hello.txt  README.md  test.txt
$ git add hello.txt
$ git commit -m "add file hello.txt"
[master 0e4004c] add file hello.txt
 1 file changed, 1 insertion(+)
 create mode 100644 hello.txt
$ git push
对象计数中: 3, 完成.
Delta compression using up to 4 threads.
压缩对象中: 100% (2/2), 完成.
写入对象中: 100% (3/3), 354 bytes | 0 bytes/s, 完成.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:sinalvee/Test
   984cabc..0e4004c  master -> master

最后推送到github

1. git push origin master

Centos7 编译安装 Git 最新版

四、Git 管理项目子模块

1、首先,我们创建裸仓库
        mkdir qtm.git
    2、进入目录
        cd qtm.git
    3、初始化仓库
        git init --bare
    4、使用Idea将主项目文件提交到远程服务器,Git服务器默认会创建master分支
        [email protected]:/usr/local/qtm.git
    5、在Git服务器中用git branch命令查看当前分支
        git branch
    6、创建子项目分支
         git checkout -b dev dev1 dev2 ..(根据子项目数量来创建分支)
    7、然后在Idea中分别将子项目提交到其它分支,每个人负责那个子项目就导出对应的分支节点
    8、Git鼓励大量使用分支:
        查看分支:git branch
        创建分支:git branch <name>
        切换分支:git checkout <name>
        创建+切换分支:git checkout -b <name>
        合并某分支到当前分支:git merge <name>
        删除分支:git branch -d <name>

转载于:https://my.oschina.net/gibbons/blog/1551168