github介绍与操作
Git远程仓库
Git 是分布式版本控制系统,同一个 Git 仓库,可以分布到不同的机器上,但开发参与者必须在同一个网络中,且必须有一个项目的原始版本,通常的办法是让一台电脑充当服务器的角色,每天 24 小时开机,其他每个人都从这个“服务器”仓库克隆一份到自己的电脑上,并且各自把各自的提交推送到服务器仓库里,也从服务器仓库中拉取别人的提交。完全可以自己搭建一台运行 Git 的服务器但现在更适合的做法是使用免费的托管平台。
Git代码托管平台,首先推荐的是GitHub,好多好的开源项目都来自GitHub,但是GitHub只能新建公开的 Git 仓库,私有仓库要收费,有时候访问比较卡,如果你做的是一个开源项目,可以首选 GitHub、coding。如果是公司自己内部使用的代码托管建议使用 Gitlab
1.Github公有仓库使用
github 是一个基于 git 的代码托管平台,付费用户可以建私人仓库,我们一般的免费
用户只能使用公共仓库,也就是代码要公开。
GitHub 的地址:https://github.com/
1.1.1 建立空仓库
新建完仓库后可以根据提示的三种方式使用远程仓库
1.1.2 配置Github
在完成上面提到的任务之前,我们配置对 Github 进行配置,实现我们的本地客户端和Github 无密码登录,我们需要配置 Github 的 SSH KEY。
首先我们在客户端生成 key,在 linux 和 windows 均可使用 ssh-keygen 命令生成,需要注意的是在 windows 下只能生成 rsa 加密方式的 key
[root@ci-node1 git_data]# ssh-keygen -t rsa
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:8ENhUEuWnMzQNx4wcAGzFgyj+YQz9DamSRzhr0Q33Ao root@ci-node1
The key's randomart image is:
+---[RSA 2048]----+
| +.ooB@@= |
| + B o.OB++ |
| E X = oo o |
| o & = + . |
| + + S |
| . . . |
| . |
| |
| |
+----[SHA256]-----+
[root@ci-node1 git_data]# ll /root/.ssh/id_rsa.pub
-rw-r--r-- 1 root root 395 Mar 29 15:27 /root/.ssh/id_rsa.pub
将生成好的ssh公钥复制到远程仓库上
1.1.3 推送本地仓库到远程仓库
第一步:为本地仓库添加远程仓库
[root@ci-node1 git_data]# git remote add origin git@github.com:yjiu1990/git_data.git
[root@ci-node1 git_data]# git remote #查看远程仓库
origin
第二步:将本地仓库数据推送到远程仓库上
[root@ci-node1 git_data]# git push -u 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.
Counting objects: 33, done.
Compressing objects: 100% (24/24), done.
Writing objects: 100% (33/33), 2.80 KiB | 0 bytes/s, done.
Total 33 (delta 5), reused 0 (delta 0)
remote: Resolving deltas: 100% (5/5), done.
To github.com:yjiu1990/git_data.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
推送完成后,在 Github 的 git_test 远程仓库里已经可以看我们本地仓库的内容,如下
1.1.4 克隆远程仓库到本地
如果我们需要在其他的客户端上使用上面的仓库,这时候我们将 Github 上的仓库克隆一份到对应的客户端上即,克隆之前首先需要打通客户端与 Github 之前的认证,具体可参见前面的相关内容,然后我们点击绿色按钮
复制里面的仓库地址(如果我们已经配置了 sshkey,直接使用 ssh 方式即可)
#在ci-node2上使用git clone将远程仓库数据下载下来
[root@ci-node2 opt]# git clone https://github.com/yjiu1990/git_data.git
Cloning into 'git_data'...
remote: Enumerating objects: 33, done.
remote: Counting objects: 100% (33/33), done.
remote: Compressing objects: 100% (19/19), done.
remote: Total 33 (delta 5), reused 33 (delta 5), pack-reused 0
Unpacking objects: 100% (33/33), done.
[root@ci-node2 opt]# ls
git_data
[root@ci-node2 opt]# cd git_data/
[root@ci-node2 git_data]# ls
a.txt b.txt c.txt
[root@ci-node2 git_data]# git remote
origin
#在node2上创建文件
[root@ci-node2 git_data]# touch test
#提交到暂存区
[root@ci-node2 git_data]# git add test
#提交到本地仓库
[root@ci-node2 git_data]# git commit -m "add test"
[master 9edc6f6] add test
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test
#因没有配置ssh,使用http方式进行推送到远程仓库
[root@ci-node2 git_data]# git push -u origin
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)
Username for 'https://github.com': yjiu1990@163.com
Password for 'https://yjiu1990@163.com@github.com':
Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 242 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/yjiu1990/git_data.git
746b436..9edc6f6 master -> master
Branch master set up to track remote branch master from origin.
查看远程仓库状态
1.1.5 git fetch使用
[root@ci-node1 git_data]# ls
a.txt b.txt c.txt
[root@ci-node1 git_data]# touch ab # 创建一个新文件
[root@ci-node1 git_data]# git add ab # 提交到暂存区
[root@ci-node1 git_data]# git commit -m "add ab" #提交到本地仓库
[master d91c292] add ab
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 ab
[root@ci-node1 git_data]# git push -u origin #将本地仓库推送到远程仓库,但是会报错,需要把远程仓库拉到本地仓库后进行合并后再进行推送
To github.com:yjiu1990/git_data.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:yjiu1990/git_data.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
[root@ci-node1 git_data]# git fetch # 拉取远程仓库到本地仓库
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 2 (delta 1), reused 2 (delta 1), pack-reused 0
Unpacking objects: 100% (2/2), done.
From github.com:yjiu1990/git_data
746b436..9edc6f6 master -> origin/master
[root@ci-node1 git_data]# git branch -r #表示查看远程分支
origin/master
[root@ci-node1 git_data]# git branch -a #表示查看所有分支
* master
tesing
remotes/origin/master
[root@ci-node1 git_data]# ll .
total 8
-rw-r--r-- 1 root root 0 Mar 29 15:59 ab
-rw-r--r-- 1 root root 21 Mar 29 14:02 a.txt
-rw-r--r-- 1 root root 0 Mar 28 20:24 b.txt
-rw-r--r-- 1 root root 4 Mar 29 11:28 c.txt
[root@ci-node1 git_data]# git merge origin/master # 将远程仓库拉取出来的数据合并到本地仓库
Merge made by the 'recursive' strategy.
test | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test
[root@ci-node1 git_data]# git push -u origin #合并之后再进行推送
Counting objects: 4, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 426 bytes | 0 bytes/s, done.
Total 4 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 1 local object.
To github.com:yjiu1990/git_data.git
9edc6f6..42330e2 master -> master
Branch master set up to track remote branch master from origin.
再查远程仓库数据变化