git和Github

一、git

1、简介

git说白了就是用来管理项目的

Git(读音为/gɪt/)是一个开源的分布式版本控制系统,可以有效、高速地处理从很小到非常大的项目版本管理。

Git 是 Linus Torvalds为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。

Torvalds 开始着手开发 Git 是为了作为一种过渡方案来替代 BitKeeper

2、特点

分布式相比于集中式的最大区别在于开发者可以提交到本地,每个开发者通过克隆(git clone),在本地机器上拷贝一个完整的Git仓库。

下图是经典的git开发过程。
在这里插入图片描述

3、功能特性:

(1)、从一般开发者的角度来看,git有以下功能:
1、从服务器上克隆完整的Git仓库(包括代码和版本信息)到单机上。

2、在自己的机器上根据不同的开发目的,创建分支,修改代码。

3、在单机上自己创建的分支上提交代码。

4、在单机上合并分支。

5、把服务器上最新版的代码fetch下来,然后跟自己的主分支合并。

6、生成补丁(patch),把补丁发送给主开发者。

7、看主开发者的反馈,如果主开发者发现两个一般开发者之间有冲突(他们之间可以合作解决的冲突),就会要求他们先解决冲突,然后再由其中一个人提交。如果主开发者可以自己解决,或者没有冲突,就通过。

8、一般开发者之间解决冲突的方法,开发者之间可以使用pull 命令解决冲突,解决完冲突之后再向主开发者提交补丁。

(2)、从主开发者的角度(假设主开发者不用开发代码)看,git有以下功能:
1、查看邮件或者通过其它方式查看一般开发者的提交状态。

2、打上补丁,解决冲突(可以自己解决,也可以要求开发者之间解决以后再重新提交,如果是开源项目,还要决定哪些补丁有用,哪些不用)。

3、向公共服务器提交结果,然后通知所有开发人员。

(3)、git优点:
1、适合分布式开发,强调个体。

2、公共服务器压力和数据量都不会太大。

3、速度快、灵活。

4、任意两个开发者之间可以很容易的解决冲突。

5、离线工作。

(4)、git缺点:
1、资料少(起码中文资料很少)。

2、学习周期相对而言比较长。

3、不符合常规思维。

4、代码保密性差,一旦开发者把整个库克隆下来就可以完全公开所有代码和版本信息。

(5)、 小结
1、git是有一个项目管理服务,很多开发人员喜欢使用这个来实现代码托管、融合,实现协同统一性的开发

2、可以实现环境统一,git可以实现权限管理,可以把代码提交到git上

3、在使用git之前,都是使用ftp,架构师去提取出来,然后进行处理

4、git的这种分布式相比于集中式的最大区别在于开发者可以提交到本地,放到缓存区,通过克隆,在本地机器上拷贝一个完整的git仓库

5、有点像挂载使用push回去拉取本地提交的

6、git使用的是ssh协议方式

7、master是一个默认的分支,可以创建多个分支,代表多个项目

8、每个人都是server和client,本地从目标服务端拉取一个git,克隆到本地

9、将代码提交到本地上,git会自动提交到github上

二、部署git

部署git服务器项目仓库,先完成git部署
git服务器是搭建在内网的仓库
source是开发者编写代码的服务器

2.1、修改主机名为git,清空防火墙,关闭核心防护

[root@lamp ~]# hostnamectl set-hostname git
[root@lamp ~]# su
[root@git ~]# systemctl stop firewalld
[root@git ~]# systemctl disable firewalld
[root@git ~]# setenforce 0

顺便在简单修改另一台主机环境

[root@nginx ~]# hostnamectl set-hostname source
[root@nginx ~]# su
[root@source ~]# systemctl stop firewalld
[root@source ~]# systemctl disable firewalld
[root@source ~]# setenforce 0

2.2、git服务器安装git服务

[root@git ~]# yum install git -y

2.3、git服务器创建程序服务用户git

[root@git ~]# useradd git
[root@git ~]# passwd git
Changing password for user git.
New password: 123123
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 123123
passwd: all authentication tokens updated successfully.

2.4、服务默认提交以git身份去提交,使用git身份去创建仓库/home/git/repos/

[root@git ~]# su git
[git@git root]$ cd ~
[git@git ~]$ pwd
/home/git
[git@git ~]$ mkdir repos
[git@git ~]$ ls
repos

2.5、仓库为repos,里面可以再创建

[git@git repos]$ mkdir demo.git
[git@git repos]$ ls
demo.git
[git@git repos]$ cd demo.git/
[git@git demo.git]$ 
 
备注:后缀必须要git

2.6、仓库初始化git init --bare(生成初始化文件)

[git@git demo.git]$ git init --bare
Initialized empty Git repository in /home/git/repos/demo.git/
[git@git demo.git]$ ls
branches  config  description  HEAD  hooks  info  objects  refs

2.7、git常用命令

最常用的 git 命令有:
   add        添加文件内容至索引
   bisect     通过二分查找定位引入 bug 的变更
   branch     列出、创建或删除分支
   checkout   检出一个分支或路径到工作区
   clone      克隆一个版本库到一个新目录
   commit     记录变更到版本库
   diff       显示提交之间、提交和工作区之间等的差异
   fetch      从另外一个版本库下载对象和引用
   grep       输出和模式匹配的行
   init       创建一个空的 Git 版本库或重新初始化一个已存在的版本库
   log        显示提交日志
   merge      合并两个或更多开发历史
   mv         移动或重命名一个文件、目录或符号链接
   pull       获取并合并另外的版本库或一个本地分支
   push       更新远程引用和相关的对象
   rebase     本地提交转移至更新后的上游分支中
   reset      重置当前HEAD到指定状态
   rm         从工作区和索引中删除文件
   show       显示各种类型的对象
   status     显示工作区状态
   tag        创建、列出、删除或校验一个GPG签名的 tag 对象

2.8、此时demo.git就不再是文件夹了,变成了代码仓库

[git@git demo.git]$ ls
branches  config  description  HEAD  hooks  info  objects  refs

对方(source)若是使用仓库的话,克隆仓库,需要配置ssh

2.9、配置主机之间的免密登录

souce免密登录git服务器
[root@source ~]# ssh-keygen -t rsa		//-t rsa 非堆成密钥
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
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:RADvFbl3IeI6MzfDraYwBuzeiozLGFhMTCpTPFcHwH0 root@source
The key's randomart image is:
+---[RSA 2048]----+
[root@source ~]# ls -a
.                .bashrc    .ICEauthority         .viminfo     桌面
..               .cache     initial-setup-ks.cfg  .Xauthority  模板
anaconda-ks.cfg  .config    .local                下载         视频
.bash_history    .cshrc     .mozilla              公共         音乐
.bash_logout     .dbus      .ssh                  图片
.bash_profile    .esd_auth  .tcshrc               文档
[root@source ~]# cd ssh
bash: cd: ssh: No such file or directory
[root@source ~]# cd .ssh
[root@source .ssh]# ll
total 12
-rw-------. 1 root root 1675 May 31 13:29 id_rsa
-rw-r--r--. 1 root root  393 May 31 13:29 id_rsa.pub
-rw-r--r--. 1 root root  175 Dec 26 15:44 known_hosts


然后把本地密钥推送进去
[root@source .ssh]# ssh-copy-id git@192.168.221.10	
//也可以ssh-copy-id -i id_rsa.pub git@192.168.221.10
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.247.206 (192.168.247.206)' can't be established.
ECDSA key fingerprint is SHA256:dXWxtS2ShXQgfb7R672V7+l3i7rGqHBbIB5MTcFnAws.
ECDSA key fingerprint is MD5:59:fb:20:f0:28:96:5e:14:90:82:63:c9:ae:67:d6:e9.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
git@192.168.221.10's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'git@192.168.247.206'"and check to make sure that only the key(s) you wanted were added.
#ssh-agent bash  #免密登录,这条指令也可以刷一下
#ssh-add 
[root@source .ssh]# ssh 'git@192.168.247.206'	#连接测试
Last login: Sun May 31 13:20:30 2020
[git@git ~]$ exit
logout
Connection to 192.168.221.10 closed.
[root@source .ssh]# ssh 192.168.221.10		#若是不指定用户git就是默认使用root登录,需要输入密码
root@192.168.221.10's password: 
Last login: Sun May 31 13:20:16 2020
[root@git ~]# exit
logout
Connection to 192.168.221.10 closed.
[root@source .ssh]# ssh git@192.168.221.10
Last login: Sun May 31 13:32:09 2020 from 192.168.221.10
[git@git ~]$ exit
logout
Connection to 192.168.221.10 closed.

2.9、开始克隆仓库

[git@git demo.git]$ pwd
/home/git/repos/demo.git

本地日常使用编写代码端也需要安装git
[root@source .ssh]# yum install git -y
[root@source .ssh]# cd ~
[root@source ~]# git clone git@192.168.247.206:/home/git/repos/demo.git
Cloning into 'demo'...
warning: You appear to have cloned an empty repository.
警告:您似乎克隆了一个空版本库
[root@source ~]# ls
anaconda-ks.cfg  demo


[root@source ~]# cd demo/
[root@source demo]# ls -a
.  ..  .git
[root@source demo]# cd .git/	#这个是隐藏目录
[root@source .git]# ls	
branches  config  description  HEAD  hooks  info  objects  refs


2.10、拷贝一个项目进source来做演示

[root@source .git]# cd ..
[root@source demo]# pwd
/root/demo
[root@source demo]# cp -rp /abc/Apple/* /root/demo/	
[root@source demo]# ls
pom.xml  src  target

2.11、把项目提交到本地库

add添加,commit是提交到本地库

-m 指定名称

[root@source demo]# git add *	#add,添加文件内容至缓存层
[root@source demo]# git commit -m "gsygsy"	#commit是提交到本地库

*** Please tell me who you are.	
#请告诉我你是谁
Run
#运行一下命令验证身份
  git config --global user.email "you@example.com"
  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@source.(none)')

2.12、设置用户邮箱

[root@source demo]# git config --global user.email "965483130@qq.com"
[root@source demo]# git commit -m "gsygsy"
[master (root-commit) f8063bc] gsygsy
 14 files changed, 122 insertions(+)

2.13、这个是本地提交,接下来远程提交remote到git服务端中

[root@source demo]# git remote -v	#检查是否与远程仓库关联成功
origin  git@192.168.247.206:/home/git/repos/demo.git (fetch)
origin  git@192.168.247.206:/home/git/repos/demo.git (push)
[root@source demo]# git remote add origin git@192.168.247.206:/home/git/repos/demo.git		#远程提交
fatal: remote origin already exists.
[root@source demo]# git remote rm origin
[root@source demo]# git remote add origin git@192.168.247.206:/home/git/repos/demo.git
[root@source demo]# ls
pom.xml  src  target

2.14、将源码推送至远程仓库

[root@source demo]# git push origin master
Counting objects: 47, done.
Compressing objects: 100% (19/19), done.
Writing objects: 100% (47/47), 5.46 KiB | 0 bytes/s, done.
Total 47 (delta 1), reused 0 (delta 0)
To git@192.168.247.206:/home/git/repos/demo.git
 * [new branch]      master -> master

2.15、将源码推送至远程仓库

[root@source demo]# git push origin master
Counting objects: 47, done.
Compressing objects: 100% (19/19), done.
Writing objects: 100% (47/47), 5.46 KiB | 0 bytes/s, done.
Total 47 (delta 1), reused 0 (delta 0)
To git@192.168.247.206:/home/git/repos/demo.git
 * [new branch]      master -> master

2.16、切换到git上面查看

[git@git demo.git]$ ll
total 16
drwxrwxr-x.  2 git git    6 May 31 13:24 branches
-rw-rw-r--.  1 git git   66 May 31 13:24 config
-rw-rw-r--.  1 git git   73 May 31 13:24 description
-rw-rw-r--.  1 git git   23 May 31 13:24 HEAD
drwxrwxr-x.  2 git git  242 May 31 13:24 hooks
drwxrwxr-x.  2 git git   21 May 31 13:24 info
drwxrwxr-x. 48 git git 4096 May 31 16:22 objects		#被修改过
drwxrwxr-x.  4 git git   31 May 31 13:24 refs
[git@git demo.git]$ cd objects/	
02/   16/   1c/   2b/   37/   43/   56/   69/   81/   9b/   b0/   c4/   e4/   f3/   f9/   pack/ 
07/   18/   1d/   32/   40/   46/   63/   76/   82/   9c/   b6/   d2/   e6/   f5/   ff/   
09/   19/   28/   34/   42/   51/   66/   77/   9a/   af/   c2/   e3/   f1/   f8/   info/ 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值