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

首先需要注册 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.

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

若出现  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:[email protected]/username/repository_name.git #修改后

4. 加入团队远程仓库

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

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

git clone htttps://username:[email protected]/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

猜你喜欢

转载自www.cnblogs.com/jessica216/p/12682469.html