github介绍与操作

Posted jiawei2527

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了github介绍与操作相关的知识,希望对你有一定的参考价值。

Github公有仓库使用

   Github顾名思义是一个Git版本库的托管服务,是目前全球最大的软件仓库,拥有上百万的开发者用户,也是软件开发和寻找资源的最佳途径,Github不仅可以托管各种Git版本仓库,还拥有了更美观的Web界面,代码文件可以被任何人克隆,使得开发者为开源项贡献代码变得更加容易,当然也可以付费购买私有库,这样高性价比的私有库真的是帮助到了很多团队和企业。

   github 是一个基于 git 的代码托管平台,付费用户可以建私人仓库,我们一般的免费用户只能使用公共仓库,也就是代码要公开。

   GitHub 的地址:https://github.com/

1. 建立空仓库

   登录github官网,创建空仓库

技术图片

技术图片

2. 配置Github

    对 Github 进行配置,实现我们的本地客户端和Github 无密码登录。
    需要配置 Github 的 SSH KEY。首先在客户端生成 key,在 linux 和 windows 均可使用 ssh-keygen 命令生成,需要注意的是在 windows 下只能生成 rsa 加密方式的 key

1)在Linux客户端上生成key

[root@cache01 ~]# ssh-keygen -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:ffK/uOTzV2lf2aSGxGSjL8A1JyO30KmK0I2EKf6y24o root@cache01
The key‘s randomart image is:
+---[RSA 2048]----+
| o . . |
|. o . o O = |
|.. o o . * @ . |
| .. o . +.o o .|
| .. . .S.oo.. o+|
| . .. . .+o o++|
| o .o.. +|
|... o.o o|
|Eoo. +++o |
+----[SHA256]-----+

[root@cache01 ~]# ll /root/.ssh/id_rsa.pub
-rw-r--r-- 1 root root 394 Jun 20 21:41 /root/.ssh/id_rsa.pub

2)将生成好的ssh公钥复制到远程仓库上

技术图片

技术图片

3 推送本地仓库到远程仓库

第一步:为本地仓库添加远程仓库

[root@cache01 git_test]# git remote add origin git@github.com:jwx552708/git_test.git

[root@cache01 git_test]# git remote

origin

第二步:将本地仓库数据推送到远程仓库上

#先拉取

[root@cache01 git_test]# git pull origin master

warning: no common commits
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:jwx552708/git_test
* branch master -> FETCH_HEAD
Merge made by the ‘recursive‘ strategy.
README.md | 1 +
1 file changed, 1 insertion(+)
create mode 100644 README.md

#再推送

[root@cache01 git_test]# git push -u origin master
Warning: Permanently added the RSA host key for IP address ‘13.229.188.59‘ to the list of known hosts.
Counting objects: 31, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (22/22), done.
Writing objects: 100% (30/30), 2.63 KiB | 0 bytes/s, done.
Total 30 (delta 6), reused 0 (delta 0)
remote: Resolving deltas: 100% (6/6), done.
To git@github.com:jwx552708/git_test.git
35a628f..0647e50 master -> master
Branch master set up to track remote branch master from origin.

推送完成后,在 Github 的 git_test 远程仓库里已经可以看我们本地仓库的内容,如下

4 克隆远程仓库到本地

   Github 上的仓库克隆一份到对应的客户端上即,克隆之前首先需要打通客户端与 Github 之前的认证,具体可参见前面的相关内容,然后我们点击绿色按钮
技术图片
复制里面的仓库地址(如果我们已经配置了 sshkey,直接使用 ssh 方式即可)

   在web01上使用git clone将远程仓库数据下载下来

[root@web01 opt]# ll
total 0
[root@web01 opt]# git clone git@github.com:jwx552708/git_test.git
Cloning into ‘git_test‘...
The authenticity of host ‘github.com (52.74.223.119)‘ 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,52.74.223.119‘ (RSA) to the list of known hosts.
remote: Enumerating objects: 33, done.
remote: Counting objects: 100% (33/33), done.
remote: Compressing objects: 100% (17/17), done.
remote: Total 33 (delta 6), reused 30 (delta 6), pack-reused 0
Receiving objects: 100% (33/33), done.
Resolving deltas: 100% (6/6), done.
[root@web01 opt]# ll
total 0
drwxr-xr-x 3 root root 74 Jun 20 22:23 git_test

[root@web01 opt]# cd git_test/
[root@web01 git_test]# ll
total 16
-rw-r--r-- 1 root root 27 Jun 20 22:23 a.txt
-rw-r--r-- 1 root root 26 Jun 20 22:23 b.txt
-rw-r--r-- 1 root root 24 Jun 20 22:23 c.txt
-rw-r--r-- 1 root root 10 Jun 20 22:23 README.md

[root@web01 git_test]# touch test

[root@web01 git_test]# git add test

[root@web01 git_test]# git commit -m "add test"

[master cfccc21] add test
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test

# 推送到远程仓库

[root@web01 git_test]# git push -u origin

查看远程仓库状态

技术图片

5 git fetch使用

[root@cache01 git_test]# ll

total 16
-rw-r--r-- 1 root root 27 Jun 20 18:53 a.txt
-rw-r--r-- 1 root root 26 Jun 20 20:22 b.txt
-rw-r--r-- 1 root root 24 Jun 20 20:10 c.txt
-rw-r--r-- 1 root root 10 Jun 20 22:13 README.md

[root@cache01 git_test]# touch test.txt     # 创建一个新文件

[root@cache01 git_test]# git add test.txt   # 提交到暂存区

[root@cache01 git_test]# git commit -m "add test.txt"   #提交到本地仓库

[master 40ce134] add test.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.txt

[root@cache01 git_test]# 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)

To git@github.com:jwx552708/git_test.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to ‘git@github.com:jwx552708/git_test.git‘
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge 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@cache01 git_test]# git fetch                 # 拉取远程仓库到本地仓库
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:jwx552708/git_test
0647e50..cfccc21 master -> origin/master

[root@cache01 git_test]# git branch -r     # 查看远程分支
origin/master
[root@cache01 git_test]# git branch -a    # 查看所有分支
linux
* master
remotes/origin/master

[root@cache01 git_test]# ll
total 16
-rw-r--r-- 1 root root 27 Jun 20 18:53 a.txt
-rw-r--r-- 1 root root 26 Jun 20 20:22 b.txt
-rw-r--r-- 1 root root 24 Jun 20 20:10 c.txt
-rw-r--r-- 1 root root 10 Jun 20 22:13 README.md
-rw-r--r-- 1 root root 0 Jun 20 22:42 test.txt

# 将远程仓库拉取出来的数据合并到本地仓库
[root@cache01 git_test]# git merge origin/master

Merge made by the ‘recursive‘ strategy.
test | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test

#合并之后再进行推送

[root@cache01 git_test]# 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)

Counting objects: 6, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 517 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 git@github.com:jwx552708/git_test.git
cfccc21..6b9d0ef master -> master
Branch master set up to track remote branch master from origin.

远程仓库查看

技术图片

以上是关于github介绍与操作的主要内容,如果未能解决你的问题,请参考以下文章

48个值得掌握的JavaScript代码片段(上)

github介绍与操作

源代码管理工具——github介绍

源代码管理工具GitHub介绍

源代码管理工具GitHub介绍

GitHub介绍