美文网首页
远程仓库GitHub使用指南

远程仓库GitHub使用指南

作者: 蜗牛Michael | 来源:发表于2019-12-22 18:26 被阅读0次

官网:github
帮助:help

要参与任何一个 Git 项目的协作,必须要了解该如何管理远程仓库。远程仓库是指托管在网络上的项目仓库,可能会有好多个,其中有些你只能读,另外有些可以写。同他人协作开发某个项目时,需要管理这些远程仓库,以便推送或拉取数据,分享各自的工作进展。 管理远程仓库的工作,包括添加远程库,移除废弃的远程库,管理各式远程库分支,定义是否跟踪这些分支,等等。本节我们将详细讨论远程库的管理和使用。

一. 添加GitHub认证秘钥

从GitHub上看出可使用的秘钥有两种:

  1. SSH
  2. GPG
屏幕快照 2018-06-13 17.05.08.png

GitHub上文件的传输是通过SSH加密传输的,因此需要创建一个SSH公开秘钥。

1.1 创建SSH Key的方法

ssh-keygen -t rsa -b 4096 -C "softarclee@gmail.com" // 此邮箱必须是github的注册邮箱账号

提示输入密码:yourpasscode 然后就会在~/.ssh目录下生成密钥文件。

屏幕快照 2017-09-04 下午9.51.08.png

~/.ssh目录下生成id_rsa(私钥)和id_rsa.pub(公钥)文件。将公开密钥(public key)复制粘贴添加至github的SSH Key。在网页版github中,依次点击Account settings(右上角倒数第二个图标) -> SSH Keys -> Add SSH Key,将id_rsa.pub文件中的字符串复制进去,注意字符串中没有换行和空格。

屏幕快照 2017-09-04 下午8.40.13.png

测试密钥是否成功:

ssh -T git@github.com
michael@bogon:~$ ssh -T git@github.com
The authenticity of host 'github.com (13.229.188.59)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
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.
Enter passphrase for key '/Users/michael/.ssh/id_rsa': yourpasscode(创建sshkey是输入的密码,并非主机密码或github账号密码)
Hi aeoloslee! You've successfully authenticated, but GitHub does not provide shell access.
michael@bogon:~$ ssh -T git@github.com
Enter passphrase for key '/Users/michael/.ssh/id_rsa': yourpasscode
Hi aeoloslee! You've successfully authenticated, but GitHub does not provide shell access.
证明密钥上传成功。

二. 远程代码仓库管理

2.1 本地已经有仓库

git协议

git remote add origin git@github.com:aeoloslee/firstgit.git
git push -u origin master

https协议

git remote add origin https://github.com/aeoloslee/firstgit.git
git push -u origin master

2.2 创建新的仓库

echo "# firstgit" >> README.md
git init
git add README.md
git commit -m "first commit"
// 连接到git@github.com:aeoloslee/firstgit.git这个地址对应的github远程仓库,并给仓库赋予别名origin
// 建议远程仓库的别名总是设置为origin,便于记忆。之后push 或者pull 的时候就需要使用到这个 origin 别名。
// 可以使用git remote -v来查看当前远程仓库的信息
git remote add origin git@github.com:aeoloslee/firstgit.git
// 创建一个 upStream (上传流),并将本地代码通过这个 upStream 推送到别名为 origin 的仓库中的 master 分支上。-u,
// 就是创建 upStream 上传流,如果没有这个上传流就无法将代码推送到 github;同时,这个upStream只需要在初次推送代码的时候
// 创建,以后就不用创建了。另外,在初次 push 代码的时候,可能会因为网络等原因导致命令行终端上的内容一直没有变化,耐心等待一会就好。
git push -u origin master

注意:master指的是分支(branch)名字。一个仓库中默认的分支名字就是master,以后,你可以有别的branch。

echo "# firstgit" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/aeoloslee/firstgit.git
git push -u origin master

2.3 支持不同的协议传输

“Git 支持多种数据传输协议。 如https:// 协议,不过你也可以使用 git:// 协议或者使用 SSH 传输协议,比如 user@server:path/to/repo.git 。”

在GitHub上,Https和Git协议地址都可以从仓库这里获取:

屏幕快照 2017-09-04 下午8.27.14.png
git remote -v
origin https://github.com/aeoloslee/VIM (fetch)
origin https://github.com/aeoloslee/VIM (push)

可以看到现在使用的是Https协议地址:

在下载的时候也可以使用git:// 地址。那么如何切换地址类型呢?

Lees-MacBook-Pro:githubprojects michael$ git clone git@github.com:aeoloslee/SlidingUpPanelLayout.git
Cloning into 'SlidingUpPanelLayout'...
Warning: Permanently added the RSA host key for IP address '192.30.253.112' to the list of known hosts.
Enter passphrase for key '/Users/michael/.ssh/id_rsa': 输入密码yourpasscode
remote: Counting objects: 327, done.
remote: Total 327 (delta 0), reused 0 (delta 0), pack-reused 327
Receiving objects: 100% (327/327), 7.29 MiB | 734.00 KiB/s, done.
Resolving deltas: 100% (110/110), done.
Checking connectivity... done.
Lees-MacBook-Pro:githubprojects michael$ git remote -v
Lees-MacBook-Pro:githubprojects michael$ git remote 
Lees-MacBook-Pro:githubprojects michael$ git remote add git@github.com:aeoloslee/SlidingUpPanelLayout.git
usage: git remote add [<options>] <name> <url>

    -f, --fetch           fetch the remote branches
    --tags                import all tags and associated objects when fetching
                          or do not fetch any tag at all (--no-tags)
    -t, --track <branch>  branch(es) to track
    -m, --master <branch>
                          master branch
    --mirror[=<push|fetch>]
                          set up remote as a mirror to push to or fetch from

Lees-MacBook-Pro:githubprojects michael$ git remote add origin git@github.com:aeoloslee/SlidingUpPanelLayout.git
Lees-MacBook-Pro:githubprojects michael$ git remote -v
origin  git@github.com:aeoloslee/SlidingUpPanelLayout.git (fetch)
origin  git@github.com:aeoloslee/SlidingUpPanelLayout.git (push)

因此,切换Https到Git协议地址的方式是:

  1. 先删除原来的https认证方式:去掉远程仓库的https认证方式

    git remote rm origin
    git remote -v 此时得到的是空,因为关联的远程仓库已经删除
    
  2. 再添加ssh认证方式的地址:重新建立与远程仓库的关联。

    git remote add origin git@github.com:aeoloslee/VIM.git
    

“Git 网络传输 HTTP 协议的开发与崛起。书中的大多数例子都已经从 SSH 切换到 HTTP,因为它更简单。”

三. 常见问题

3.1 无法跟踪的内容 untracked content

提交代码到服务器后发现Git clone下来的有些目录是空的。查看服务器的目录果然是空的。看本季git add . 后查看git status

modified: xxx(modified content, untracked content)

大概意思是xxx目录没有被跟踪。那自然push上去的时候是空的了。解决办法:后来发现这主要是xxx目录下有一个.git 目录,可能是被人给你这个目录的时候里面有了.git目录。删除.git目录。重新git add .就可以。

3.2 Github认证与git全局设置的账号和密码

michael@bogon:~$ git config --list
credential.helper=osxkeychain
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
user.name= yourusername
user.email= youremail@gmail.com
git config --global user.name yourusername
git config --global user.email youremail@gmail.com

相关文章

  • 远程仓库GitHub使用指南

    官网:github帮助:help 要参与任何一个 Git 项目的协作,必须要了解该如何管理远程仓库。远程仓库是指托...

  • 《人工智能 | TensorFlow》

    TensorFlow 使用指南(中国) Github仓库 博客同步

  • github | 仓库和分支管理

    1. 仓库管理 提示:仓库一般就指的是远程仓库即github的仓库.参阅:github 创建远程分支以及远程分支无...

  • git 基本用法

    注册github账号 用法 1. 远程仓库-->本地仓库(克隆) a)创建远程仓库gitDemob)从远程仓库克隆...

  • Git 远程仓库及分支管理

    前提:已有远程仓库项目 远程仓库信息: 来源:github远程仓库(YOUR_FORK)地址: https://g...

  • git

    添加远程仓库 从远程仓库克隆 将本地仓库内容提交github 从远程更新

  • git常用指令

    1) 远程仓库相关命令 检出仓库: 检出仓库: 直接检出分支 2.0.0,指定远程仓库名称为 github (默认...

  • GitHub入门(二)——远程仓库

    二、GitHub远程仓库 1.连接远程库 本地Git仓库和GitHub仓库之间的传输是通过SSH加密传输的,需要配...

  • pycharm与远程仓库进行连接

    个人操作远程仓库的步骤 一 .github创建远程仓库------->克隆到本地仓库--------->维护本地代...

  • 【转载】git使用教程-涂根华

    文章来源部分图片来源 五.远程仓库 (一).提交到网上的远程仓库 一.前提: github仓库 ,本地git仓库 ...

网友评论

      本文标题:远程仓库GitHub使用指南

      本文链接:https://www.haomeiwen.com/subject/logonctx.html