使用Git分布式控制系统

简介: Git 是一个分布式版本控制软件,与CVS、Subversion一类的集中式版本控制工具不同,它采用了分布式版本库的作法,不需要服务器端软件,就可以运作版本控制,使得源代码的发布和交流极其方便!

问题1:为什么要有这个Git?


个人理解:开发者们通过邮件向Linus发送着自己编写的源代码文件,由Linus本人通过手工的方式将代码合并。能不能每个人本地都有一个服务端的源码数据库,这样不必再完全依赖于服务端的源码数据库,使得源代码的发布和合并更加方便。所以git诞生了!


实例:GitHub是通过Git进行版本控制的软件源代码托管服务,用户可以免费创建公开的代码仓库。(程序员都知道吧!)


问题2:什么是Git?


答:Git 是一个分布式版本控制软件,与CVS、Subversion一类的集中式版本控制工具不同,它采用了分布式版本库的作法,不需要服务器端软件,就可以运作版本控制,使得源代码的发布和交流极其方便!


一:Git服务程序运行思路


文字:


1.在工作目录中修改数据文件。

2.将文件的快照放入暂存区域。

3.将暂存区域的文件快照提交到Git仓库中。


图片:


微信图片_20220507225627.png


二:Git服务基础命令:


个人的用户名称和电子邮件地址:


git config --global user.name “Name”

git config --global user.email “root@email”


提交数据:


git add readme.txt


将暂存区的文件提交到Git版本仓库:


git commit -m “解释说明”


查看下当前Git版本仓库的状态:


git status


三:部署Git服务器


Git是分布式的版本控制系统,我们只要有了一个原始的Git仓库,就可以让其他主机克隆我们的原始版本仓库,从而使得一个Git版本仓库可以被同时分布到不同的主机之上。


1.安装git服务


[root@caq ~]# yum install git


2.创建Git版本仓库,要以.git为后缀:


[root@caq ~]# mkdir caq.git


3.修改Git版本仓库的所有者与所有组


[root@caq ~]# chown -Rf git:git caq.git/


4.初始化Git版本仓库:


[root@caq ~]# cd caq.git/
[root@caq caq.git]# git --bare init Initialized empty Git repository in /root/caq.git/


5.服务器上的Git仓库此时已经部署好了,但是还不能分享给其他人。需要有个协议来限制。猜到了吧,是ssh协议!

在客户端生成ssh密钥


[root@caq ~]# ssh-keygen
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:
65:4a:53:0d:4f:ee:49:4f:94:24:82:16:7a:dd:1f:28 root@caq.com
The key’s randomart image is:
±-[ RSA 2048]----+
| .o+oo.o. |
| .oo *.+. |
| …+ E * o |
| o = + = . |
| S o o |
| |
| |
| |
| |
±----------------+


将客户机的公钥传递给Git服务器:


[root@caq ~]# ssh-copy-id 192.168.10.10 root@192.168.10.10’s password: Number of key(s) added: 1 Now try logging into the machine, with: “ssh ‘192.168.10.10’” and check to make sure that only the key(s) you wanted were added.


6.现在客户端就可以克隆服务端的版本仓库了。


[root@caq ~]# git clone root@192.168.10.10:/root/caq.git Cloning into ‘caq’… warning: You appear to have cloned an empty repository.


7.初始化下Git工作环境:


[root@caq ~]# git config --global user.name “Liu Chuan” [root@caq ~]# git config --global user.email “root@caq.com” [root@caq ~]# git config --global core.editor vim


8.向Git版本仓库中提交一个新文件:


[root@caq caq]# echo “I am fine” > readme.txt
[root@caq caq]# git add readme.txt
[root@caq caq]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use “git rm --cached …” to unstage)
#
# new file: readme.txt
#
[root@caq caq ]# git commit -m “Clone the Git repository”
[master (root-commit) c3961c9] Clone the Git repository
Committer: root
1 file changed, 1 insertion(+)
create mode 100644 readme.txt
[root@linuxprobe caq]# git status
# On branch master
nothing to commit, working directory clean


9.向服务器发送文件


[root@caq caq]# git remote add server root@192.168.10.10:/root/caq.git
[root@caq caq]# git push -u server master
Counting objects: 3, done.
Writing objects: 100% (3/3), 261 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To root@192.168.10.10:/root/caq.git
[new branch] master -> master
Branch master set up to track remote branch master from server.


10.查看远程服务器的仓库有无客户端发来的文件(我是又克隆了一份,当然也可以直接切换到服务器共享仓库查看)


[root@caq caq]# cd …/~
[root@caq ~]# git clone root@192.168.10.10:/root/caq.git
Cloning into ‘caq’…
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
[root@caq ~]# cd caq/
[root@caq caq]# cat readme.txt I am fine


git内容知识点很多,这篇要是看的人多的话我在出剩余的知识吧。

相关文章
|
5月前
|
开发工具 git
【git 实用指南】git 上传代码
【git 实用指南】git 上传代码
107 2
|
5月前
|
存储 算法 开发工具
git是什么?git的五个命令,git和svn的区别
git是什么?git的五个命令,git和svn的区别
81 0
|
2月前
|
开发工具 git
[git]记配置本地git到gitlab并推送
[git]记配置本地git到gitlab并推送
|
5月前
|
Linux 开发工具 git
git是什么,git常用命令
git是什么,git常用命令
39 0
|
Linux 开发工具 git
git 相关的一些命令?
git 相关的一些命令?
|
开发工具 git
【git】常用的命令
【git】常用的命令
|
存储 开发工具 git
Git(超详细)
1.Git概述 Git简介 Git是一个分布式版本控制工具,通常用来对软件开发过程中的源代码文件进行管理。通过Git仓库来存储和管理这些文件,Git仓库分为两种: 本地仓库:开发人员自己电脑上的Git 仓库 远程仓库:远程服务器上的Git仓库
|
安全 Unix Linux
|
Shell Linux 网络安全
git详解
git详解
238 0
git详解
|
开发工具 git
Git 这些小技巧你知道吗?
Git 这些小技巧你知道吗?
116 0
Git 这些小技巧你知道吗?
下一篇
无影云桌面