Git 远程仓库

以 GitHub/Gitee 为例,作为简述 Git 中的远程仓库,以及常用命令。


配置 SSH

本地 Git 仓库和 GitHub/Gitee 仓库之间的传输是通过 SSH 加密的:

  1. 创建 SSH Key

    shell 复制代码
    # 检查本地主机是否已存在 ssh key
    $ cd ~/.ssh
    $ ls # 若没有文件 id_rsa 和 id_rsa.pub,则输入如下命令:
    
    $ ssh-keygen -t rsa -C "youremail@example.com" # 可以一直按 enter 键,即可生成两个文件。

    ~/.ssh 目录中有 SSH Key 的秘钥对(id_rsaid_rsa.pub)两个文件。其中,id_rsa 是私钥,不能泄露;id_rsa.pub 是公钥。

  2. 将公钥添加至代码托管平台。登陆 GitHub/Gitee,打开设置SSH Keys 页面,将 id_rsa.pub 文件的内容粘贴至公钥框中。

  3. 测试

    shell 复制代码
    $ ssh -T git@github.com
    
    The authenticity of host 'gitee.com (xxx.xx.xxx.xx)' can't be established.
    ECDSA key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
    Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
    
    Warning: Permanently added 'gitee.com,xxx.xx.xxx.xx' (ECDSA) to the list of known hosts.
    Hi gitee_username(@gitee_username)! You've successfully authenticated, but GITEE.COM does not provide shell access.

设置后,在 git clone 仓库时要使用 ssh 的 url,而不是 https

创建 GitHub/Gitee 仓库

设置仓库名称路径 ,勾选开源,其他不需要填写或勾选。

推送本地仓库内容至 Gitee

shell 复制代码
$ git remote add origin https://gitee.com/gitee_username/repository_name.git
$ git push -u origin "master"
# 按如下提示输入 gitee 用户名和密码即可
Username for 'https://gitee.com': gitee_username
Password for 'https://gitee_username@gitee.com':

推送本地仓库内容至 GitHub 时,按以下命令输入,一直报错。

shell 复制代码
$ git remote add origin https://github.com/GitHub_username/learngit.git
$ git branch -M main
$ git push -u origin main

报错信息:

shell 复制代码
fatal: unable to access 'https://github.com/GitHub_username/learngit.git/': Failed to connect to github.com port 443: Connection timed out

尝试取消代理,仍未能解决:解决git报错:'fatal: unable to access 'https://XXX

输入命令前,使用 Watt Tollkit 来加速访问 GitHub。解决使用git时遇到Failed to connect to github.com port 443

常用命令

git remote

查看、添加、重命名和删除 Git 仓库中的远程仓库

以下是 git remote 命令的常见用法:

命令 说明
git remote 列出当前仓库中已配置的远程仓库
git remote -v 列出当前仓库中已配置的远程仓库,并显示其 URL
git remote add <remote_name> <remote_url> 指定一个远程仓库的名称和 URL,将其添加到当前仓库中
git remote rename <old_name> <new_name> 将已配置的远程仓库重命名
git remote remove <remote_name> 从当前仓库中删除指定的远程仓库
git remote set-url <remote_name> <new_url> 修改指定远程仓库的 URL
git remote show <remote_name> 显示指定远程仓库的详细信息,包括 URL 和跟踪分支。

git clone

克隆远程 Git 仓库到本地,并在当前目录下创建一个与远程仓库同名的文件夹。urlhttpsssh,其中 ssh 协议速度更快。

shell 复制代码
$ git clone [url]

git push

将本地的分支版本上传到远程并合并

shell 复制代码
$ git push <远程主机名> <本地分支名>:<远程分支名>
# 如果本地分支名与远程分支名相同,则可以省略冒号:
$ git push <远程主机名> <本地分支名>

将本地的 master 分支推送到 origin 主机(远程仓库链接的别名)的 master 分支:

shell 复制代码
$ git push origin master
$ git push origin master:master

git push -u origin master 推送 master 分支的所有内容

加了参数 -u 后,以后即可直接用 git push 代替 git push origin master

通常来说,每次使用 git push 命令,需要输入用户名和密码,避免输入的操作如下:

shell 复制代码
# 进入本地仓库文件夹内
$ git config  credential.helper store # 无 --global 表示只对这个仓库生效

在下一次执行 push 操作仍需要输入一次用户名和密码,下下次提交则不再需要。

关于配置 SSH 的原因

SSH 验证原理:SSH 登录安全性由非对称加密保证。一次产生一个公钥和一个私钥,在 Git 中一般命名为 id_rsa.pubid_rsa。其中公钥放到远程主机。

当本地主机需要登录远程主机时,本地主机向远程主机发送一个登录请求,远程收到消息后,随机生成一个字符串并用公钥加密,发回给本地。本地拿到该字符串,用存放在本地的私钥进行解密,再次发送到远程,远程比对该解密后的字符串与源字符串是否等同,如果等同则认证成功。

如果多人协作开发,那么每台开发主机都需要增加 SSH Key 到远程仓库中, 利用 SSH 密钥可以连接到 GitHub,而无需在每次访问时都提供用户名和 personal access token,还可以使用 SSH 密钥对提交进行签名。另一方面,因为只能推送(push)自己的或有权限的远程仓库中,所以远程仓库中需要配置所有需要推送代码的主机的 SSH。


参考:

Github配置ssh key的步骤(大白话+包含原理解释)

关于 SSH - GitHub 文档

相关推荐
fengyehongWorld6 小时前
Git 认证凭据管理
git
暮云星影15 小时前
git版本发布
git·gitee·github·gitea
乌夷15 小时前
Git Flow 分支模型
git
独隅16 小时前
VS Code Git 工作树多分支并行开发实战评测
git
暮云星影21 小时前
git仓库分支管理
git·gitee·github·gitea
互联网中的一颗神经元2 天前
ZZ — Git 速查表
大数据·git·elasticsearch
早安试言2 天前
git使用
git
燐妤2 天前
梦绘片场 ProjectDream故事总纲场景剧本篇:从项目到成片的 AI 漫剧工作台
人工智能·git·ai·项目开发
JackieDYH2 天前
Git 与 GitHub 新电脑配置指南(从零开始)
git·github·工具·使用教程
Jeanyia3 天前
GitLab 配套 Git 常用命令
git·gitlab