下载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,会发现内容修改成功。