天天看点

git安装&远程连接仓库

目录

​​一、电脑第一次安装git,并将项目推送到github​​​​​​​​

​​二、clone下来一个别人的仓库,修改后,推送到自己的仓库​​

一、电脑第一次安装git,并将项目推送到github

1.安装git

下载git ​​Git - Downloads​​

安装完成后,还需要最后一步设置,在命令行输入:

$ git config --global user.name "Your Name"
$ git config --global user.email "[email protected]"      
git安装&远程连接仓库

2.生成SSH密钥:

1.查看是否已经有了ssh密钥:cd ~/.ssh

如果没有密钥则不会有此文件夹,有则备份删除

2.生成密钥:

$ ssh-keygen -t rsa -C '[email protected]'

按3个回车,密码为空。

git 公钥和私钥位置。进入 

cd ~/.ssh      

id_rsa.pub 是公钥

3.添加公钥到远程仓库(github)

1、查看你生成的公钥: 

linux :

$ cat ~/.ssh/id_rsa.pub

win:

C:\Users\it\.ssh\id_rsa.pub

mac:

cd ~/.ssh      

2、添加公钥

登陆你的github帐户。点击你的头像,然后 Settings -> 左栏点击 SSH and GPG keys -> 点击 New SSH key

然后你复制上面的公钥内容,粘贴进“Key”文本域内。 title域,自己随便起个名字

点击 Add key

git安装&远程连接仓库

完成以后,验证下这个key是不是正常工作:

$ ssh -T ​​[email protected]​​ Attempts to ssh to github

git安装&远程连接仓库

如果,看到:

Hi xxx! You’ve successfully authenticated, but GitHub does not # provide shell access.

恭喜你,你的设置已经成功了。

4.本地库与远程库关联

我们在本地库上使用命令​

​git remote add​

​把它和github的远程库关联:

git remote add origin [email protected]:liaoxuefeng/learngit.git      

[email protected]:liaoxuefeng/learngit.git  与github上保持一致。

git安装&远程连接仓库

之后,就可以正常地用​

​git push​

​​和​

​git pull​

​推送了!

如果在使用命令​

​git remote add​

​时报错:

git remote add origin [email protected]:liaoxuefeng/learngit.git
fatal: remote origin already exists.      
git安装&远程连接仓库

这说明本地库已经关联了一个名叫​

​origin​

​​的远程库,此时,可以先用​

​git remote -v​

​查看远程库信息:

git remote -v
origin  [email protected]:michaelliao/learngit.git (fetch)
origin  [email protected]:michaelliao/learngit.git (push)      

可以看到,本地库已经关联了​

​origin​

​的远程库,并且,该远程库指向GitHub。

我们可以删除已有的GitHub远程库:

git remote rm origin      

再次关联github远程库(注意路径中需要填写正确的用户名):

git remote add origin [email protected]:liaoxuefeng/learngit.git      

5.推送

第一次推:

$ git push -u origin master      

以后推

$ git push origin master      

6.拉取

6.1通过git clone拉取(不需要权限)

 git clone https://github.com/18713341733/crawler.git

git安装&远程连接仓库
git安装&远程连接仓库

6.2通过git pull拉取(需要权限)

git pull origin/master

git安装&远程连接仓库

git 公钥和私钥位置

C:\Users\it\.ssh

id_rsa.pub 是公钥

git安装&远程连接仓库

7.从远程库克隆

git clone https://github.com/18713341733/crawler.git

git安装&远程连接仓库

9.报错解决:

9.1 error: remote origin already exists.

$ git remote add origin [email protected]:18713341733/crawler.git

error: remote origin already exists.

我们可以删除已有的GitHub远程库:

git remote rm origin      

再次关联github远程库(注意路径中需要填写正确的用户名):

git remote add origin [email protected]:liaoxuefeng/learngit.git      

9.2  src refspec master does not match any

$ git push origin master

error: src refspec master does not match any

error: failed to push some refs to 'github.com:18713341733/crawler.git'

git安装&远程连接仓库

我把命令暂时换成了:

git push origin main

git安装&远程连接仓库

原因:

发现现在建的 github 工程默认名为了 main

(后面发现由于受到"Black Lives Matter"运动的影响,GitHub 从今年 10 月 1 日起,在该平台上创建的所有新的源代码仓库将默认被命名为 “main”,而不是原先的"master"。)

git安装&远程连接仓库

所以 pull 和 push 都会报错。

估计是由于仓库名称不一样,导致远程和本地的仓库不能关联上。统一远程和本地的仓库名称即可。

1、把本地的 master 仓库名称修改为远端的 main

重命名命令: git branch -m oldBranchName newBranchName

git安装&远程连接仓库

2、然后,push 就好了。

git安装&远程连接仓库

注意此时我们的push命令,不再是

git push origin master

而是

二、clone下来一个别人的仓库,修改后,推送到自己的仓库