首页 > 其他 > 详细

Git 本地仓库与远程仓库建立连接

时间:2020-04-12 00:02:40      阅读:406      评论:0      收藏:0      [点我收藏+]

首先需要注册 github 账号及在本地安装配置 git,使其本地可用。

1. 创建公钥 (ssh-key)

ssh-keygen -t rsa -C “your email address”

一路回车选择默认值后生成文件夹 ~/.ssh。

cd ~/.ssh

可以看到该文件夹下生成了 id_rsa.pub 文件。

2.添加公钥到 Github 中

登录 github 账号,选中并打开 setting,选择 SSH and GPG keys,选择 New SSH key,在 Title 中填入题目,在 Key 中填入id_rsa.pub 文件中的公钥。

              技术分享图片                    技术分享图片

 

                                                                        技术分享图片

注意不能在 vim 中复制 id_rsa.pub 文件中的内容,否则会报错:

Key is invalid. You must supply a key in OpenSSH public key format.

可用如下命令验证上述配置是否成功:

ssh -T git@github.com 

若出现  Are you sure you want to continue connecting (yes/no)? 选择 yes。

出现 You‘ve successfully authenticated, but GitHub does not provide shell access. 说明配置成功。

 3.在本地建立新仓库并将其关联到远程仓库

(1)建立本地新仓库

建立新的文件夹当本地仓库。

 mkdir repository_name #建立新的本地仓库
cd repository_name
git init
git add filename # (or git add *.*)
git commit -m "first commit"

在 Github 上相应的创建新远程仓库。

点击 New 进入创建新仓库界面,在创建仓库界面填入相应的信息。

                                                                                                        技术分享图片    

                                                                   技术分享图片

将本地仓库与远程仓库关联起来。

git remote add origin https://github.com/username/repository_name.git
git push -u origin master

 若出现错误:error: The requested URL returned error: 403 Forbidden while accessing ...

修改 .git/config 文件中的 url

url = https://github.com/username/repository_name.git #修改前
url = https://username:password@github.com/username/repository_name.git #修改后

4. 加入团队远程仓库

首先团队仓库是存在的,并且已经收到团队仓库的加入邀请或许可。

团队仓库一般是 private,先把团队仓库的代码拷贝到本地

git clone htttps://username:password@github.com/team_name/team_repository_name

抓取所需分支

git fetch origin remote_branch_name:local_branch_name

转到所需分支

git checkout local_branch_name

5.git其他常用操作

(1) 建立新的分支

git checkout -b new_branch

(2) 添加文件并备注信息

git commit -a -m "commit information"

(3) 向远程添加分支

git push origin local_branch_name:remote_branch_name

 

Git 本地仓库与远程仓库建立连接

原文:https://www.cnblogs.com/jessica216/p/12682469.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!