git是目前世界上最先进的分布式版本控制系统。出自linux之父之手
SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以首先要从中央服务器哪里得到最新的版本,然后干活,干完后,需要把自己做完的活推送到中央服务器。集中式版本控制系统是必须联网才能工作,如果在局域网还可以,带宽够大,速度够快,如果在互联网下,如果网速慢的话,就纳闷了。
Git是分布式版本控制系统,那么它就没有中央服务器的,每个人的电脑就是一个完整的版本库,这样,工作的时候就不需要联网了,因为版本都是在自己的电脑上。既然每个人的电脑都有一个完整的版本库,那多个人如何协作呢?比如说自己在电脑上改了文件A,其他人也在电脑上改了文件A,这时,你们两之间只需把各自的修改推送给对方,就可以互相看到对方的修改了。
[root@localhost ~]# yum install git
Loaded plugins: fastestmirror
Determining fastest mirrors
base | 3.6 kB 00:00:00
epel | 5.3 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/4): extras/7/x86_64/primary_db | 153 kB 00:00:00
(2/4): epel/x86_64/updateinfo | 1.0 MB 00:00:00
(3/4): updates/7/x86_64/primary_db | 2.8 MB 00:00:00
(4/4): epel/x86_64/primary_db | 6.9 MB 00:00:00
Package git-1.8.3.1-20.el7.x86_64 already installed and latest version
Nothing to do
[root@localhost ~]# git version
git version 1.8.3.1
客户端安装git
windows客户端下载git
小乌龟下载地址
安装完毕后鼠标右键菜单中会有git Bash,git GUI 等选项,打开git Bash设置用户名邮箱
因为Git是分布式版本控制系统,所以需要填写用户名和邮箱作为一个标识。
每一个 Git 的提交都会使用这些信息,并且它会写入到你的每一次提交中,不可更改。如果使用了 --global 选项,那么该命令只需要运行一次,因为之后无论你在该系统上做任何事情, Git 都会使用那些信息。 当你想针对特定项目使用不同的用户名称与邮件地址时,可以在那个项目目录下运行没有 --global 选项的命令来配置。
打开git bash
如果想要检查你的配置,可以使用 git config --list 命令来列出所有 Git 当时能找到的配置。
创建 git 用户,用来管理 Git 服务,并为 git 用户设置密码
[root@localhost ~]# useradd git
[root@localhost ~]# passwd git
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
#禁止git用户ssh登录
[root@localhost ~]# vim /etc/passwd
git:x:500:500::/home/git:/usr/bin/git-shell
创建 /home/git/.ssh/authorized_keys文件存放ssh公钥
在 /home/git/ 下创建目录 .ssh
[root@localhost git]# chown -R git:git .ssh
[root@localhost git]# chmod 700 .ssh
[root@localhost git]# touch .ssh/authorized_keys
[root@localhost git]#chmod 600 .ssh/authorized_keys
将客户端公钥id_rsa.pub文件的内容写到服务器端 /home/git/.ssh/authorized_keys 文件里
5. 创建版本库
随便找个文件夹
[root@iZwz9ifm754y5p5n4kw6zlZ 0242]# pwd
/www/wwwroot/git/0242
[root@iZwz9ifm754y5p5n4kw6zlZ 0242]# git init --bare 0242.git
Initialized empty Git repository in /www/wwwroot/git/0242/0242.git/
[root@iZwz9ifm754y5p5n4kw6zlZ 0242]# ll -al
total 12
drwxr-xr-x 3 root root 4096 Nov 2 18:36 .
drwxr-xr-x 3 root root 4096 Nov 2 18:32 ..
drwxr-xr-x 7 root root 4096 Nov 2 18:36 0242.git
git init
该命令将创建一个名为 .git 的子目录,是隐藏的,这个子目录含有你初始化的 Git 仓库中所有的必须文件,这些文件是 Git 仓库的骨干。
git init --bare xxxx.git
这样初始化的仓库并没有.git目录,只有.git目录下的文件。
不使用–bare选项时,就会生成.git目录以及其下的版本历史记录文件,这些版本历史记录文件就存放在.git目录下;而使用–bare选项时,不再生成.git目录,而是只生成.git目录下面的版本历史记录文件,这些版本历史记录文件也不再存放在.git目录下面,而是直接存放在版本库的根目录下面
把生成的仓库文件所有人改为git用户
[root@iZwz9ifm754y5p5n4kw6zlZ 0242]# chown -R git:git 0242.git
[root@iZwz9ifm754y5p5n4kw6zlZ 0242]# ll -al
total 12
drwxr-xr-x 3 root root 4096 Nov 2 18:36 .
drwxr-xr-x 3 root root 4096 Nov 2 18:32 ..
drwxr-xr-x 7 git git 4096 Nov 2 18:36 0242.git
[root@iZwz9ifm754y5p5n4kw6zlZ 0242]#
[root@iZj6ccn4rzv6zts66ne4btZ sl.baidu.com]# git clone [email protected]:/www/wwwroot/git/0242/0242.git 0242
Cloning into '0242'...
The authenticity of host 'xx.xxx.xxx.xxx (xx.xxx.xxx.xxx) ' can't be established.
ECDSA key fingerprint is SHA256:W9f3HJap6HEGlbH61hbdxxKR3+c7c1oQmojREZSD9lI.
ECDSA key fingerprint is MD5:69:e7:85:51:ea:b0:34:de:de:0b:57:33:48:2c:41:24.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'xx.xxx.xxx.xxx' (ECDSA) to the list of known hosts.
[email protected]'s password:
Could not chdir to home directory /home/git: Permission denied
remote: warning: unable to access '/home/git/.config/git/attributes': Permission denied
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
克隆成功,如果克隆失败,可能是该服务器公钥未加入git服务器authorized_keys文件
[root@iZj6ccn4rzv6zts66ne4btZ .ssh]# ssh-keygen -t rsa -C "随便填"
[root@iZj6ccn4rzv6zts66ne4btZ sl.baidu.com]# cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC2jJXJOXoZpD0F1jlGTOrnSt22TWZCHTMeNEPdEV3rKT+THSe74yAhBN3qNlXbNAPp4iF7U3Aykbh2is332Cbf+R9NKCQ6t8/RucE348sKaQhv4Pok9IFFjeDD1vW11iM8KlsrVNtWyP0OUY63LPvDSzGHHt/9v/60/GetCTlk5EBGbohYcYe1/y8I+NNaNOyIqCbdcgEeZWypECc2Usa+XWjI8KuZApmPHpw/EKxv8+5c7JbAH+i0KVbxc2WVfqsEXP6Nc342haoqkrmmo3uNumohKx34teChL9C56Zerz0oU8lpgBQbB+iCqgcZKHUUFiFrs9+28+FmrYZRdXKC9 phpsolo@163.com
复制~/.ssh/id_rsa.pub文件里的内容加入到git服务器authorized_keys文件内
修改新服务器里的test.txt内容,并推送到远程仓库,然后本地拉取查看是否能成功同步
[root@iZj6ccn4rzv6zts66ne4btZ 0242]# git add test.txt
[root@iZj6ccn4rzv6zts66ne4btZ 0242]# git commit -m "测试推送2"
*** Please tell me who you are.
Run
git config --global user.email "[email protected]"
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@iZj6ccn4rzv6zts66ne4btZ.(none)')
出示此报错,按照提示设置其邮箱以及用户名,设置完毕再次commit,commit成功,然后push至远程仓库
[root@iZj6ccn4rzv6zts66ne4btZ 0242]# git config --global user.email "[email protected]"
[root@iZj6ccn4rzv6zts66ne4btZ 0242]# git config --global user.name "hhs"
[root@iZj6ccn4rzv6zts66ne4btZ 0242]# git commit -m "测试推送2"
[master 2178928] 测试推送2
1 file changed, 1 insertion(+), 1 deletion(-)
[root@iZj6ccn4rzv6zts66ne4btZ 0242]# git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:
git config --global push.default matching
To squelch this message and adopt the new behavior now, use:
git config --global push.default simple
See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)
git@xx.xxx.xxx.xxx's password:
Could not chdir to home directory /home/git: Permission denied
Counting objects: 5, done.
Writing objects: 100% (3/3), 250 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@xx.xxx.xxx.xxx:/www/wwwroot/git/0242/0242.git
9396e5c..2178928 master -> master
本地服务器拉取,test.txt内容已改变
本次测试两台服务器之间形成了一种多人协作开发的关系,这就是分布式版本管理工具的作用