zoukankan      html  css  js  c++  java
  • Linux下安装Git

    1. 安装Git

    1.1 Ubuntu12.04下

    可以使用apt-get方式安装,也可以下载源代码安装【1】,我们这里使用apt-git安装。

    但由于直接使用 sudo apt-get install git 安装的版本较老,因此我们参考【2】中给出的PPA源。

    sudo add-apt-repository ppa:git-core/ppa
    sudo apt-get update
    sudo apt-get install git

    安装完成后,检查是否安装成功

    git --version

    显示 git version 2.1.1,表明安装成功。

    1.2 CentOS6.6下

    在CentOS5的时代,由于yum源中没有git,所以需要预先安装一系列的依赖包。但在CentOS6的yum源中已经有git的版本了,可以直接使用yum源进行安装。

    $ sudo yum install git

    但是yum源中安装的git版本是1.7.1,太老了,Github等需要的Git版本最低都不能低于1.7.2 。所以我们一般不用上面的方法。而是下载git源码,编译安装。

    编译安装的步骤是【4】:

    (1)首先先更新系统

    sudo yum update

    (2)安装依赖的包

    sudo yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker

    (3)下载git源码并解压缩

    $ wget https://github.com/git/git/archive/v2.3.0.zip
    $ unzip v2.3.0.zip
    $ cd git-2.3.0

    (4)编译安装

    将其安装在“/usr/local/git”目录下。

    make prefix=/usr/local/git all
    sudo make prefix=/usr/local/git install

    (5)此时你如果使用git --version 查看git版本的话,发现git仍然是1.7.1版本。这是因为它默认使用了"/usr/bin"下的git。

    你可以用下面的命令查看git所在的路径:

    $ whereis git
    git: /usr/bin/git /usr/local/git /usr/share/man/man1/git.1.gz

    (6)我们要把编译安装的git路径放到环境变量里,让它替换"/usr/bin"下的git。为此我们可以修改“/etc/profile”文件(或者/etc/bashrc文件)。

    sudo vim /etc/profile

    然后在文件的最后一行,添加下面的内容,然后保存退出。

    export PATH=/usr/local/git/bin:$PATH

    (7)使用source命令应用修改。

    source /etc/profile

    (8)然后再次使用git --version 查看git版本,发现输出2.3.0,表明安装成功。

    2. 设置Git

    (1)设置用户名和email。

    git config --global user.name "Your Name"
    git config --global user.email "youremail@domain.com"

    此时,Home目录下会新建一个.gitconfig文件

    3. 为GitHub账号添加SSH Keys

    以公钥认证方式访问SSH协议的Git服务器时无需输入口令,而且更安全。(访问HTTP协议的Git服务器时,比如提交修改,每次都需要输入口令。)

    (1)创建SSH key

    $ ssh-keygen -t rsa -C "youremail@163.com"

    系统会提示key的保存位置(一般是~/.ssh目录)和指定口令,保持默认,连续三次回车即可。

    (2)Copy SSH Key

    然后用vim打开该文件,id_rsa.pub文件内的内容,粘帖到github帐号管理的添加SSH key界面中。

    vim ~/.ssh/id_rsa.pub

    (3)添加到GitHub

    登录github-> Accounting settings图标-> SSH key-> Add SSH key-> 填写SSH key的名称(可以起一个自己容易区分的),然后将拷贝的~/.ssh/id_rsa.pub文件内容粘帖-> add key”按钮添加。

    (4)测试

    ssh -T git@github.com

    4. 为GitHub上的Repository提交修改

    (1)git clone已存在GitHub上的Repository。(在新建的~/MyTestFolder目录中)

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

    (2)修改一个文件,然后提交

    vim README.md
    git status
    git add README.md
    git status
    git commit -m "Edit by WorkUbuntu 1204"
    git status
    git remote add origin https://github.com/zhchnchn/ZhchnchnTest.git

    这时会报错误:

    fatal: remote origin already exists.

    解决办法【3】:

    $ git remote rm origin

    然后再次执行 git remote add origin https://github.com/zhchnchn/ZhchnchnTest.git

    (3)之后,需要将修改push到GitHub上

    git push -u origin master

    执行该条命令后,会要求输入GitHub账户的用户名和密码。

    (4)提交完成后,查看GitHub上的Repository,会发现内容修改成功。

    Refer

    【1】How To Install Git on Ubuntu 14.04(https://www.digitalocean.com/community/tutorials/how-to-install-git-on-ubuntu-14-04)

    【2】Installing Latest version of git in ubuntu(http://stackoverflow.com/questions/19109542/installing-latest-version-of-git-in-ubuntu)

    【3】Ubuntu12.04下安装Git(http://blog.csdn.net/biosd/article/details/9115837

    【4】CentOS6.6源码安装git-2.3.0(http://blog.csdn.net/goritcly/article /details/43878209)(http://www.centoscn.com/image-text/install/2015/0225 /4735.html)

  • 相关阅读:
    【leetcode】1630. Arithmetic Subarrays
    【leetcode】1629. Slowest Key
    【leetcode】1624. Largest Substring Between Two Equal Characters
    【leetcode】1620. Coordinate With Maximum Network Quality
    【leetcode】1619. Mean of Array After Removing Some Elements
    【leetcode】1609. Even Odd Tree
    【leetcode】1608. Special Array With X Elements Greater Than or Equal X
    【leetcode】1603. Design Parking System
    【leetcode】1598. Crawler Log Folder
    Java基础加强总结(三)——代理(Proxy)Java实现Ip代理池
  • 原文地址:https://www.cnblogs.com/dcrq/p/5717582.html
Copyright © 2011-2022 走看看