git和github的上传项目

一:github入门

https://github.com/

1.1 登录github官网

使用谷歌浏览器可以帮助翻译成中文
在这里插入图片描述

1.2 注册

在这里插入图片描述

1.3 点击创建

在这里插入图片描述

选择自己偏向方向

完成设定

在这里插入图片描述

验证邮箱
在这里插入图片描述
在这里插入图片描述

点击跳过
在这里插入图片描述

1.4 测试创建仓库

在这里插入图片描述

此时便在新建仓库内

在这里插入图片描述

二:了解一下git

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

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

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

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

2.1 git特点

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

下图是经典的git开发过程。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mHby6fxD-1591197082841)(G:\GSY\Documents\typora图片)]

2.2 Git的功能特性:

2.2.1 从一般开发者的角度来看,git有以下功能:

1、从服务器上克隆完整的Git仓库(包括代码和版本信息)到单机上。

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

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

4、在单机上合并分支。

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

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

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

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

2.2.2 从主开发者的角度(假设主开发者不用开发代码)看,git有以下功能:

1、查看邮件或者通过其它方式查看一般开发者的提交状态。

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

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

2.3 git优点:

  • 适合分布式开发,强调个体。

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

  • 速度快、灵活。

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

  • 离线工作。

2.4 git缺点:

  • 资料少(起码中文资料很少)。

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

  • 不符合常规思维。

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

2.5 小结

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

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

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

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

有点像挂载

使用push回去拉取本地提交的

git使用的是ssh协议方式

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

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

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

三:在自己的服务器上部署git

部署git服务器项目仓库,先完成git部署

git服务器是搭建在内网的仓库
source是开发者编写代码的服务器

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

[root@lamp ~]# hostnamectl set-hostname git
[root@lamp ~]# su
[root@git ~]# iptables -F
[root@git ~]# setenforce 0
[root@git ~]# sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

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

[root@nginx ~]# hostnamectl set-hostname source
[root@nginx ~]# su
[root@source ~]# iptables -F
[root@source ~]# setenforce 0
[root@source ~]# sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
[root@source ~]# 

3.2 git服务器安装git服务

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

3.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.

3.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

3.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

3.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

3.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 对象

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

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

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

3.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 [email protected]		
//也可以ssh-copy-id -i id_rsa.pub [email protected]
/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
[email protected]'s password: 

Number of key(s) added: 1

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

3.10 开始克隆仓库

[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 [email protected]:/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

3.11 拷贝一个项目进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

3.12 把项目提交到本地库

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 "[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@source.(none)')

请告诉我你是谁

运行下面中的指令

3. 13 设置用户邮箱

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

此时收到了邮件,不过是英文,看不懂
在这里插入图片描述

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

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

或者换个名字

3.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 [email protected]:/home/git/repos/demo.git
 * [new branch]      master -> master

3.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/ 

四:在github上创建一个新的仓库用以提交

#或者使用最开始创建的仓库2020531

创建一个新的仓库

github会基于两种方式提交

https或者ssh

在这里插入图片描述

4.1 把项目推到github上去

4.1.1 需要修改一下本地的参数

[root@source demo]# ls -a
.  ..  .git  pom.xml  src  target
[root@source demo]# cd .git/
[root@source .git]# ls
branches  COMMIT_EDITMSG  config  description  HEAD  hooks  index  info  logs  objects  refs
[root@source .git]# vim config 
修改url,去指向github
        url = [email protected]:gsy-0519/2020531.git

或者使用https

4.1.2 然后先把项目进行本地提交add

[root@source .git]# git add .
fatal: This operation must be run in a work tree
[root@source .git]# cd /root/demo/
[root@source demo]# git add .

4.1.3 commit -m起名字

[root@source demo]# git commit -m "2020532"
# On branch master
nothing to commit, working directory clean
[root@source demo]# git push origin master
The authenticity of host 'github.com (13.229.188.59)' 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)? yes
Warning: Permanently added 'github.com,13.229.188.59' (RSA) to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

4.1.4 ssh 不行,说没有权限,使用https

[root@source demo]# vim .git/config 
        url = https://github.com/gsy-0519/2020531.git
[root@source demo]# git push origin master		#上传
Username for 'https://github.com': gsy-0519
Password for 'https://[email protected]': 
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)
remote: Resolving deltas: 100% (1/1), done.
To https://github.com/gsy-0519/2020531.git
 * [new branch]      master -> master

4.1.5 可以了,到网站上查看

回到web

在这里插入图片描述
成功

4.1.6 下载拉取项目直接用pull

[root@source demo]# git pull https://github.com/gsy-0519/2020531.git
Username for 'https://github.com': gsy-0519
Password for 'https://[email protected]': 
From https://github.com/gsy-0519/2020531
 * branch            HEAD       -> FETCH_HEAD
Already up-to-date.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章