GitHub-创建仓库与本地同步

时间:2022-07-26
本文章向大家介绍GitHub-创建仓库与本地同步,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1. 在Linux上安装Git

1 [root@mini05 ~]# yum install -y git
2 ………………

2. 本地创建ssh-key信息

 1 [root@mini05 ~]# ssh-keygen -t rsa
 2 Generating public/private rsa key pair.
 3 Enter file in which to save the key (/root/.ssh/id_rsa): 
 4 Created directory '/root/.ssh'.
 5 Enter passphrase (empty for no passphrase): 
 6 Enter same passphrase again: 
 7 Your identification has been saved in /root/.ssh/id_rsa.
 8 Your public key has been saved in /root/.ssh/id_rsa.pub.
 9 The key fingerprint is:
10 SHA256:ACvHut11cMKDaL1QzsrL3x23OyUl9cd7+kLfFW30cLM root@mini05
11 The key's randomart image is:
12 +---[RSA 2048]----+
13 |    . .          |
14 |   . O o      o.o|
15 |  . B * = .  . *=|
16 |   * o o =  . .EB|
17 |  . o . S .  o .+|
18 |   + o . .  . o.o|
19 |  . + .   . .+ o+|
20 |     . . . o..o o|
21 |      . . . oo o.|
22 +----[SHA256]-----+
23 [root@mini05 ~]# ll -d .ssh/
24 drwx------ 2 root root 38 Sep 17 22:26 .ssh/
25 [root@mini05 ~]# cd .ssh/
26 [root@mini05 .ssh]# ll
27 total 8
28 -rw------- 1 root root 1679 Sep 17 22:26 id_rsa
29 -rw-r--r-- 1 root root  393 Sep 17 22:26 id_rsa.pub
30 [root@mini05 .ssh]# cat id_rsa.pub 
31 ssh-rsa AAAAB3NzaC1yc2EXXXXDAQABAAABAQDZZMXPdVVnTsZYXXXXXKKKK/JPorL9qc9YN/ydrFBrROXxC0lz0ote8asbMMgvtYeXS01+APWyuQUtm+Pne1BeXHkKYBxa3etgiNNnXhJoCJi/y9qsOkii+NPhkcPM/wrMAIp6Eh1BY2v60LTu0cyuLTdIeNfC5D1T84fYMYRrF6u1R27uTjIxaubwcOJyzpmog1RWqp8oex3gq1Z5H7GmTKQuu/gKcmVPQoXMy+UwyTRBd3nmDd2GkLgG6OaDSw01JAfJjgEdGXSB/z1zvUh46mGx9zjU94b+bFQHBX8E8qMqSkrkMzhl09LZQhKvyOSvTBbCEmhf123k2X8iledrR4f/ root@mini05

3. 在GitHub上添加SSH Keys信息

4. 在GitHub上新建仓库并本地克隆仓库

4.1. GitHub新建仓库

git@github.com:zhanglianghhh/zhangtest.git

4.2. 本地克隆GitHub仓库

 1 [root@mini05 git_repository]# pwd  # 注意目录 
 2 /opt/git_repository
 3 [root@mini05 git_repository]# git clone git@github.com:zhanglianghhh/zhangtest.git  
 4 Cloning into 'zhangtest'...
 5 The authenticity of host 'github.com (192.30.253.113)' can't be established.
 6 RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
 7 RSA key fingerprint is MD5:16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
 8 Are you sure you want to continue connecting (yes/no)? yes  # 如果是第一次连接,就会出现这种情况
 9 Warning: Permanently added 'github.com,192.30.253.113' (RSA) to the list of known hosts.
10 remote: Counting objects: 3, done.
11 remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
12 Receiving objects: 100% (3/3), done.
13 [root@mini05 git_repository]# ll
14 total 0
15 drwxr-xr-x 3 root root 35 Sep 17 22:38 zhangtest

5. 本地建仓库并同步到GitHub上

5.1. 本地建仓库

 1 [root@mini05 gittest]# pwd
 2 /opt/git_repository/gittest
 3 [root@mini05 gittest]# git init 
 4 Initialized empty Git repository in /opt/git_repository/gittest/.git/
 5 [root@mini05 gittest]# ll -a
 6 total 0
 7 drwxr-xr-x 3 root root  18 Oct  1 15:22 .
 8 drwxr-xr-x 6 root root  67 Oct  1 15:22 ..
 9 drwxr-xr-x 7 root root 119 Oct  1 15:22 .git
10 [root@mini05 gittest]# cat test.info 
11 111
12 222
13 333
14 [root@mini05 gittest]# git add .  # 添加到暂存区 
15 [root@mini05 gittest]# git commit -m "add"  # 提交到仓库
16 [master (root-commit) 1c47050] add
17  1 file changed, 3 insertions(+)
18  create mode 100644 test.info

5.3. GitHub新建仓库

5.3. 本地仓库关联GitHub

 1 [root@mini05 gittest]# git remote add origin git@github.com:zhanglianghhh/gittest.git  
 2 [root@mini05 gittest]# git push -u origin master  # 第一次提交时 后续提交:  git push origin master  
 3 Counting objects: 3, done.
 4 Writing objects: 100% (3/3), 220 bytes | 0 bytes/s, done.
 5 Total 3 (delta 0), reused 0 (delta 0)
 6 remote: 
 7 remote: Create a pull request for 'master' on GitHub by visiting:
 8 remote:      https://github.com/zhanglianghhh/gittest/pull/new/master
 9 remote: 
10 To git@github.com:zhanglianghhh/gittest.git
11  * [new branch]      master -> master
12 Branch master set up to track remote branch master from origin.

6. Git仓库的更新

6.1. GitHub有更新之后本地更新仓库

 1 [root@mini05 zhangtest]# pwd  # 注意所在目录
 2 /opt/git_repository/zhangtest
 3 [root@mini05 zhangtest]# ll
 4 total 4
 5 -rw-r--r-- 1 root root 29 Sep 17 23:11 README.md
 6 [root@mini05 zhangtest]# cat README.md 
 7 # zhangtest
 8 zhangtest 
 9 [root@mini05 zhangtest]# git pull  # 本地更新
10 remote: Counting objects: 3, done.
11 remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
12 Unpacking objects: 100% (3/3), done.
13 From github.com:zhanglianghhh/zhangtest
14  * branch            HEAD       -> FETCH_HEAD
15 Updating e730676..d4fb57e
16 Fast-forward
17  README.md | 1 +
18  1 file changed, 1 insertion(+)
19 [root@mini05 zhangtest]# cat README.md 
20 # zhangtest
21 zhangtest
22 张三

6.2. 本地更新并提交到本地仓库

 1 [root@mini05 zhangtest]# pwd
 2 /opt/git_repository/zhangtest
 3 [root@mini05 zhangtest]# git status  # 查看仓库当前的状态----可见文件没有修改,也没有需要提交的
 4 # On branch master
 5 # Your branch is ahead of 'origin/master' by 1 commit.
 6 #   (use "git push" to publish your local commits)
 7 #
 8 nothing to commit, working directory clean
 9 [root@mini05 zhangtest]# cat README.md  # 更新的文件
10 # zhangtest
11 zhangtest
12 张三
13 Git is a distributed version control system.  # 新增的行
14 Git is free software.  # 新增的行
15 [root@mini05 zhangtest]# git status  # 查看仓库当前的状态----可见文件有修改,但是没有提交到本地仓库
16 # On branch master
17 # Your branch is ahead of 'origin/master' by 1 commit.
18 #   (use "git push" to publish your local commits)
19 #
20 # Changes not staged for commit:
21 #   (use "git add <file>..." to update what will be committed)
22 #   (use "git checkout -- <file>..." to discard changes in working directory)
23 #
24 #    modified:   README.md
25 #
26 no changes added to commit (use "git add" and/or "git commit -a")
27 [root@mini05 zhangtest]# git diff README.md  # 查看仓库中的文件和当前文件的区别
28 diff --git a/README.md b/README.md
29 index a1c15c2..4346fe7 100644
30 --- a/README.md
31 +++ b/README.md
32 @@ -1,3 +1,5 @@
33  # zhangtest
34  zhangtest
35  张三
36 +Git is a distributed version control system.
37 +Git is free software.
38 [root@mini05 zhangtest]# git add README.md  # 将文件内容添加到索引(将修改添加到暂存区)。也就是将要提交的文件的信息添加到索引库中。
39 # git add .  说明:如果是 . 表示当前目录
40 [root@mini05 zhangtest]# git status  # 查看仓库当前的状态----有文件需要提交
41 # On branch master
42 # Your branch is ahead of 'origin/master' by 1 commit.
43 #   (use "git push" to publish your local commits)
44 #
45 # Changes to be committed:
46 #   (use "git reset HEAD <file>..." to unstage)
47 #
48 #    modified:   README.md
49 #
50 [root@mini05 zhangtest]# git commit -m "add info"  # 提交到本地仓库
51 [master 65a58f2] add info
52  1 files changed, 2 insertions(+)
53  create mode 100644 test.info
54 [root@mini05 zhangtest]# git status  # 查看仓库当前的状态----可见文件没有修改,也没有需要提交的
55 # On branch master
56 # Your branch is ahead of 'origin/master' by 2 commits.
57 #   (use "git push" to publish your local commits)
58 #
59 nothing to commit, working directory clean

6.3. 本地仓库推送到Git远程仓库

1 [root@mini05 zhangtest]# git push origin master 
2 Counting objects: 5, done.
3 Delta compression using up to 4 threads.
4 Compressing objects: 100% (3/3), done.
5 Writing objects: 100% (3/3), 360 bytes | 0 bytes/s, done.
6 Total 3 (delta 1), reused 0 (delta 0)
7 remote: Resolving deltas: 100% (1/1), completed with 1 local object.
8 To git@github.com:zhanglianghhh/zhangtest.git
9    3b7db1c..c752e64  master -> master