远程库最好和本地库的名字一致。
Public:仓库类型是公有的,所有人都可以看到,你可以选择谁能提交。
Private:仓库类型是私有的,可以选择谁能看到,谁能提交。
远程库提供两种链接方式:HTTPS和SSH。
命令名称 | 作用 |
---|---|
git remote -v | 查看当前所有远程地址别名 |
git remote add 别名 远程地址 | 起别名 |
git push 别名 分支 | 推送本地分支上的内容到远程仓库 |
git clone 远程地址 | 将远程仓库的内容克隆到本地 |
git pull 远程库地址别名 远程分支名 | 将远程仓库对于分支最新内容拉下来后与当前本地分支直接合并 |
git remote -v 查看当前所有远程地址别名
git remote add 别名 远程地址
1. L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/GitToGitHub (master)
2. $ git remote -v
3.
4. L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/GitToGitHub (master)
5. $ git remote add GitToGitHub https://github.com/XXX/GitToGitHub.git
6.
7. L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/GitToGitHub (master)
8. $ git remote -v
9. GitTo-GitHub https://github.com/XXX/GitToGitHub.git (fetch)
10. GitTo-GitHub https://github.com/XXX/GitToGitHub.git (push)
git push 别名 分支
1. L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/GitToGitHub (master)
2. $ git push GitToGitHub master
3. Enumerating objects: 27, done.
4. Counting objects: 100% (27/27), done.
5. Delta compression using up to 8 threads
6. Compressing objects: 100% (20/20), done.
7. Writing objects: 100% (27/27), 2.44 KiB | 624.00 KiB/s, done.
8. Total 27 (delta 6), reused 0 (delta 0), pack-reused 0
9. remote: Resolving deltas: 100% (6/6), done.
10. To https://github.com/SmallGarden/GitToGitHub.git
11. * [new branch] master -> master
*Note:本地库与远程库第一次建立联系需要其中的代码拉取过,或者远程库中为空。
git pull 别名 分支
1. $ git pull GitToGitHub master
2. remote: Enumerating objects: 5, done.
3. remote: Counting objects: 100% (5/5), done.
4. remote: Compressing objects: 100% (3/3), done.
5. remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
6. Unpack-ing objects: 100% (3/3), 682 bytes | 4.00 KiB/s, done.
7. From https://github.com/SmallGarden/GitToGitHub
8. * branch master -> FETCH_HEAD
9. 85d73d2..87c88ca master -> GitToGitHub/master
10. Updating 85d73d2..87c88ca
11. Fast-forward
12. hello.txt | 1 +
13. 1 file changed, 1 insertion(+)
在远程库中修改一个文件,然后拉取到本地库中,将覆盖本地库中的文件。
克隆代码不需要登入账号。
git clone 远程地址
1. L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/Test
2. $ git clone https://github.com/XXX/GitToGitHub.git
3. Cloning into 'GitToGitHub'...
4. remote: Enumerating objects: 30, done.
5. remote: Counting objects: 100% (30/30), done.
6. remote: Compressing objects: 100% (17/17), done.
7. remote: Total 30 (delta 7), reused 26 (delta 6), pack-reused 0
8. Receiving objects: 100% (30/30), done.
9. Resolving deltas: 100% (7/7), done.
这个地址为远程仓库地址,克隆结果:顺便初始化本地仓库,并创建远程仓库别名。
1. L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/Test/GitToGitHub (master)
2. $ git remote -v
3. origin https://github.com/XXX/GitToGitHub.git (fetch)
4. origin https://github.com/XXX/GitToGitHub.git (push)
小结:clone会做如下操作:1、拉取代码;2、初始化本地库;3、创建别名。
邀请不是自己团队的人员进行维护代码。
编辑修改代码:可以在线下或者线上编辑,然后将代码重新提交到自己的远程库中;(团队内协作可以直接提交到维护者的远程库中)
我们可以看到远程仓库中还有一个SSH的地址,因此我们也可以实用SSH 进行访问。
L@DESKTOP-C3ML9EI MINGW64 ~
$ rm -rvf .ssh
removed '.ssh/id_rsa'
removed '.ssh/id_rsa.pub'
removed '.ssh/known_hosts'
removed '.ssh/known_hosts.old'
removed directory '.ssh'
1. L@DESKTOP-C3ML9EI MINGW64 ~
2. $ ssh-keygen -t rsa -C [email protected]
3. Generating public/private rsa key pair.
4. Enter file in which to save the key (/c/Users/L/.ssh/id_rsa):
5. Created directory '/c/Users/L/.ssh'.
6. Enter passphrase (empty for no passphrase):
7. Enter same passphrase again:
8. Your identification has been saved in /c/Users/L/.ssh/id_rsa
9. Your public key has been saved in /c/Users/L/.ssh/id_rsa.pub
10. The key fingerprint is:
11. SHA256:+vCLX8bRSFxe2Dx9giYUvWzGT31LWd7ZPSShU/VcrX4 270717808@qq.com
12. The key's randomart image is:
13. +---[RSA 3072]----+
14. | .oo.B+oo|
15. | ..oBo=oB|
16. | oB..o*@|
17. | . oB o=B|
18. | S oo.+. +|
19. | . . . o.E|
20. | o + . |
21. | = o |
22. | ..=. |
23. +----[SHA256]-----+
L@DESKTOP-C3ML9EI MINGW64 ~/.ssh
$ cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2 ......
L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/GitToGitHub (master)
$ git pull [email protected]:SmallGarden/GitToGitHub.git master
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warn-ing: Permanently added 'github.com' (ED25519) to the list of known hosts.
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 1), reused 0 (delta 0), pack-reused 0
Unpack-ing objects: 100% (4/4), 1.27 KiB | 2.00 KiB/s, done.
From github.com:SmallGarden/GitToGitHub
* branch master -> FETCH_HEAD
Updating 87c88ca..efe977b
Fast-forward
hello.txt | 1 +
1 file changed, 1 insertion(+)
L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/GitToGitHub (master)
$ git push [email protected]:SmallGarden/GitToGitHub.git master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 322 bytes | 322.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:SmallGarden/GitToGitHub.git
efe977b..b6f9e99 master -> master