一、安装git
1.最新git源码下载地址:
https://github.com/git/git/releases
https://www.kernel.org/pub/software/scm/git/
安装git
yum install git
查看yum源仓库Git信息
yum info git
2.安装依赖库
[root@localhost ~]# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
[root@localhost ~]# yum install gcc-c++ perl-ExtUtils-MakeMaker
3.如果原有的git版本过低,移除默认安装的旧版git
[root@localhost ~]# git --version ## 查看自带的版本git version 1.8.3.1
[root@localhost hyx]# yum remove git ## 移除原来的版本
4.下载&安装
[root@localhost hyx]# cd /usr/src
[root@localhost src]# wget https://www.kernel.org/pub/software/scm/git/git-2.19.2.tar.gz
注意:此时对网路要求极高,如果失败,建议用迅雷等下载工具下载然后传输到Linux目录下
5.解压
[root@localhost src]# tar -xf git-2.19.2.tar.gz
6.配置编译安装
[root@localhost src]# ls
debug git-2.19.2 git-2.19.2.tar.gz kernels
[root@localhost src]# cd git-2.19.2/
[root@localhost git-2.19.2]#
[root@localhost git-2.19.2]# make configure
[root@localhost git-2.19.2]# ./configure --prefix=/usr/git ##配置目录
[root@localhost git-2.19.2]# make profix=/usr/git
[root@localhost git-2.19.2]# make install
7.加入环境变量
[root@localhost hyx]# echo "export PATH=$PATH:/usr/git/bin" >> /etc/profile
[root@localhost hyx]# source /etc/profile
8.检查版本
[root@localhost hyx]# git --version
git version 2.19.2
二、生成SSH密钥
$ ssh-keygen -t rsa -C “your email address”
[root@localhost hyx]# ssh-keygen -t rsa -C "869407584@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:8XkOGJlZG0FQYzj7GuloVeCwziyU9pQWmGddWI5fmiQ 869407584@qq.com
The key's randomart image is:
+---[RSA 2048]----+
| o ..O@. |
| o = *O + |
| + *E++ . |
| + = oO.= |
| o B S+B . |
| . = + .+ |
| . + o . |
| o o |
| . |
+----[SHA256]-----+
三、添加密钥到GitHub
打开 Github,登录自己的账号后
点击自己的头像->settings->SSH And GPG Keys->New SSH key
将本地 id_rsa.pub 中的内容粘贴到 Key 文本框中,随意输入一个 title(不要有中文),点击 Add Key 即可
四、centos里测试验证
[root@localhost hyx]# ssh git@github.com
The authenticity of host 'github.com (52.74.223.119)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
RSA key fingerprint is MD5:16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? y
Please type 'yes' or 'no': yes
Warning: Permanently added 'github.com,52.74.223.119' (RSA) to the list of known hosts.
PTY allocation request failed on channel 0
Hi a869407584! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.
表明验证成功
五、git常用命令参考
git remote -v/–verbose:显示出详细的url地址名和对应的别名.
git clone < address >:复制代码库到本地;
git add < file > :添加文件到代码库中;
git rm < file > :删除代码库的文件;
git commit -m < message >:提交更改,在修改了文件以后,使用这个命令提交修改。
git pull:从远程同步代码库到本地。
git push:推送代码到远程代码库。
git branch:查看当前分支。带*是当前分支。
git branch < branch-name >:新建一个分支。
git branch -d < branch-name >:删除一个分支。
git checkout < branch-name >:切换到指定分支。
git log:查看提交记录(即历史的 commit 记录)。
git status:当前修改的状态,是否修改了还没提交,或者那些文件未使用。
git reset < log >:恢复到历史版本。
六、Git实例
操作步骤:
1、远程仓库ceshi.git为空,把本地代码上传到远程仓库
[hyx@localhost ~]$ echo "# test.c">>ceshi.md
[hyx@localhost ~]$ git init
Initialized empty Git repository in /home/hyx/.git/
[hyx@localhost ~]$ git add ceshi.md.md
fatal: pathspec 'ceshi.md.md' did not match any files
[hyx@localhost ~]$ git add ceshi.md
[hyx@localhost ~]$ git commit -m "first commit"
[master (root-commit) 0102632] first commit
Committer: hyx <hyx@localhost.localdomain>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:
git config --global --edit
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
1 file changed, 6 insertions(+)
create mode 100644 ceshi.md
[hyx@localhost ~]$ git remote add origin https://github.com/a869407584/ceshi.git
[hyx@localhost ~]$ git push -u origin master
Username for 'https://github.com': hyx822012^H^H^H^H^H^H^H^H^H^H^H^H^C
[hyx@localhost ~]$ git push -u origin master
Username for 'https://github.com': a869407584
Password for 'https://a869407584@github.com':
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 226 bytes | 226.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/a869407584/ceshi.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
2、更新本地代码到远程仓库
git add ceshi.md
git commit -m "first commit"
git push -u origin master
3、获取远程仓库中的代码到本地
git clone https://github.com/a869407584/ceshi.git
4、从远程仓库同步代码更新本地代码
git pull origin master
echo "# testc" >> ceshi.md
git initgit add ceshi.md
git commit -m "first commit"
git remote add origin https://github.com/a869407584/ceshi.git
git push -u origin master