Github远程仓库

Github

  • 概述
    • GitHub是一个面向开源及私有软件项目的托管平台,因为只支持git 作为唯一的版本库格式进行托管。
  • 功能
    • 代码托管
    • 提供了订阅、讨论组、文本渲染、在线文件编辑器、协作图谱(报表)、代码片段分享(Gist)等功能

Git本地仓库上传到Github

  • 准备:

    linux主机 | IP:192.168.100.203
    — | —

    • Github创建仓库

    创建仓库名hello wrold,选择私有仓库

    创建后显示

    linux主机生成秘钥复制到Github

    [root@localhost ~]# 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:
    SHA256:3vlxpFBcufj9iMUYZI6WnRAPnstSt1eaZVMk3yEN718 root@localhost.localdomain
    The key's randomart image is:
    +---[RSA 2048]----+
    |          o. o=+o|
    |         ..=o.+++|
    |          +O=o +*|
    |         o++*.o*.|
    |        S.+ .==oE|
    |       . o o.++ +|
    |        . o oo..o|
    |           ..o. .|
    |            .    |
    +----[SHA256]-----+
    [root@localhost ~]# cat /root/.ssh/id_rsa.pub 
    ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQClrxAabHK6unM+7HpIhf6fQTMx0byvGRmCBYxPTucGq+TsSFY+XDAfXwD1181Dm8tSUpWs/4m7zhZPeCMrIag7I7BBIk6yBD0o1JslHF7O31qgQgn1Bd7WV0aKKYZDeye77i1IeBJ0KklUJpYcTulyeuyFXQL93Uy50vMwUpGw7KOq9L4/m6dYkD/Ex4pVk6RQnD2xE2pJ686WQ+fNTy5T2k5TAZNWW8cLAFcCir5ECeg9mUgkfc7q4HeZut6gDfcMw1ufcRX5pD5q1nizmXdqhrXblGclWIFRaZ4DZ2t5F5QHU4UDbD3uA47nHx24f/MbpMYSLBT6AM0bjUYyGZDZ root@localhost.localdomain
    # 把上面一行公钥复制到github




    复制github的url

    git@github.com:LMK-hacker/hello-world.git

    添加远程仓库

    [root@localhost test]# git remote add origin git@github.com:LMK-hacker/hello-world.git  # origin 指定源文件地址

    上传本地仓库的文件
    [root@localhost test]# ls
    hello.txt

    [root@localhost test]# git push -u origin master # 把文件传到袁成成主机的master主干上 -u 第一次上传时需要写入,以后就不用了
    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: 3, done.
    Writing objects: 100% (3/3), 202 bytes | 0 bytes/s, done.
    Total 3 (delta 0), reused 0 (delta 0)
    To git@github.com:LMK-hacker/hello-world.git
     * [new branch]      master -> master
    分支 master 设置为跟踪来自 origin 的远程分支 master。

    创建本地git仓库—> 添加暂存区—>提交到本地仓库—>从本地仓库上传到远程仓库 (创建本地仓库上一章有写)

Git仓库克隆

[root@localhost ~]# git clone git@github.com:LMK-hacker/hello-world.git
正克隆到 'hello-world'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
接收对象中: 100% (3/3), done.
[root@localhost ~]# ls hello-world/
hello.txt

如果在操作时把github远程仓库上的某个文件删除了,再次上传push时的时候会不允许上传,出现这种情况可能是数据紊乱

以下是出现错误时操作

[root@localhost ~]# cd hello-world/
[root@localhost hello-world]# echo 123 >> hello.txt 
[root@localhost hello-world]# git add hello.txt # 添加到暂存区
[root@localhost hello-world]# git commit -m "123" # 添加到本地仓库
[master a056585] 123
 1 file changed, 1 insertion(+)
[root@localhost hello-world]# git push origin master # 提交到远程仓库
Warning: Permanently added the RSA host key for IP address '52.74.223.119' to the list of known hosts.
To git@github.com:LMK-hacker/hello-world.git
 ! [rejected]        master -> master (fetch first)
error: 无法推送一些引用到 'git@github.com:LMK-hacker/hello-world.git'
提示:更新被拒绝,因为远程版本库包含您本地尚不存在的提交。这通常是因为另外
提示:一个版本库已推送了相同的引用。再次推送前,您可能需要先合并远程变更
提示:(如 'git pull')。
提示:详见 'git push --help' 中的 'Note about fast-forwards' 小节。
[root@localhost hello-world]#

解决方法

git pull
git push origin master

git push origin master --force # --force可能导致数据不稳定

标签管理

tag 对于仓库来说就相当于虚拟机的快照

[root@localhost hello-world]# git tag # 查看标签
[root@localhost hello-world]# git tag v0.1 # 创建标签
[root@localhost hello-world]# git tag # 查看标签
v0.1
[root@localhost hello-world]# git push origin v0.1 # 将标签上传到远程
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:LMK-hacker/hello-world.git
 * [new tag]         v0.1 -> v0.1

查看github仓库的标签

通过标签实现回溯代码

# 在上一章创建的本地仓库上传到远程仓库 仓库地址/tmp/test
[root@localhost test]# echo lll > lll.txt
[root@localhost test]# git add lll.txt # 添加到暂存区
[root@localhost test]# git commit -m "2" # 提交到本地仓库
[master b62d93d] 2
1 file changed, 1 insertion(+)
create mode 100644 lll.txt
[root@localhost test]# git push origin master # 上传文件到远程仓库
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (6/6), 424 bytes | 0 bytes/s, done.
Total 6 (delta 0), reused 0 (delta 0)
To git@github.com:LMK-hacker/hello-world.git
+ a056585...b62d93d master -> master (forced update)
[root@localhost test]# ls
hello.txt  lll.txt
[root@localhost test]# git tag v0.2 # 创建标签
[root@localhost test]# git push origin v0.2 # 上传标签
Total 0 (delta 0), reused 0 (delta 0) 
To git@github.com:LMK-hacker/hello-world.git
* [new tag]         v0.2 -> v0.2
[root@localhost test]# git tag # 查看标签
v0.2

查看标签

点击标签可以回溯到标签的状态

分支管理

Git每个分支可以放不同的代码,创建其他分支是为了保证代码没有错误,然后再合并到主分支

创建分支

[root@localhost test]# git branch # 查看分支
* master
[root@localhost test]# git branch salve # 创建 salve分支
[root@localhost test]# git branch # 查看分支
* master
  salve
[root@localhost test]# git checkout salve # 切换分支
切换到分支 'salve'

分支创建内容并上传到github

[root@localhost test]# echo lmkl > lmk.txt
[root@localhost test]# git add lmk.txt
[root@localhost test]# git commit -m "lmk"
[salve e6172f6] lmk
 1 file changed, 1 insertion(+)
 create mode 100644 lmk.txt
[root@localhost test]# git push origin salve
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 286 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: 
remote: Create a pull request for 'salve' on GitHub by visiting:
remote:      https://github.com/LMK-hacker/hello-world/pull/new/salve
remote: 
To git@github.com:LMK-hacker/hello-world.git
 * [new branch]      salve -> salve


查看分支内的东西

合并分支

[root@localhost test]# git branch # 查看分支
  master
* salve
[root@localhost test]# git checkout master  # 切换分支
切换到分支 'master'
[root@localhost test]# git branch  # 查看分支
* master
  salve
[root@localhost test]# git merge salve  # 合并分支 在master合并salve分支
更新 b62d93d..e6172f6
Fast-forward
 lmk.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 lmk.txt

合并分支后切换到master分支

[root@localhost test]# ls # 现在主分支多了个文件
hello.txt  lll.txt  lmk.txt
[root@localhost test]# git push origin master  上传到远程仓库
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:LMK-hacker/hello-world.git
   b62d93d..e6172f6  master -> master

  • 在linux主机合并分支后,只是合并的本地仓库的文件,需要再把主分支的文件在push到远程仓库.
  • 如果没有合并分支,其他分支无论怎么删除和修改,都不影响主分支

小结

不知不觉写了一下午了现在是晚上九点,效率有点慢呀哈哈,万事开头难,加油吧!

下一章

  • Gitlab远程仓库

本博客所有文章是以学习为目的,如果有不对的地方可以一起交流沟通共同学习 邮箱:1248287831@qq.com!