Mac下使用git和github

本文详细介绍了如何安装Git,配置GitHub账号,生成SSH密钥,以及如何将本地代码推送到GitHub仓库的过程。此外,还提供了从GitHub拉取代码的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.git的安装

  安装新版的Xcode,会自带的安装git。在终端中输入git --version,若提示下文:

Agreeing to the Xcode/iOS license requires admin privileges, please re-run as root via sudo.
直接打开xcode,按照提示同意xcode/IOS SDk license profile就ok了。

2.在github网站(gtihub网站)注册一个免费的账号

3.配置git和github

在local打开terminal: $cd ~/.ssh     检查是否已经存在ssh, 若果没有提示:No such file or directory说明不是第一次使用git,存在一份ssh密钥


    若存在ssh,就将原来的备份,或者新建ssh放于其他目录,这里提供将原有的备份并清除掉的命令:

            


    如果不存在,通过默认的参数直接生成ssh

       生成过程如下:
       $ ssh-keygen -t rsa -C " xxxxx@gmail.com"         (注册github时的email,然后一直enter,并输入密码)
   
 
4.在github中添加ssh:
       登陆github,选择右上角用户(倒三角)Settings-->SSH Keys 添加ssh
       Title:xxx@163.com
       Key:打开你生成的id_rsa.pub文件,将其中内容拷贝至此

5.测试SSH连接是否成功:
        $ssh git@github.com
      若出现了ssh: connect to host 163.com port 22: Connection refused,那说明当前用户没用配置远程登录。在系统偏好设置中进行配置,添加远程登录的中允许的用户,可以用:"+"添加当前系统使用的用户,或者选择“All users”。


 重新测试:

        The authenticity of host 'github.com (207.97.227.239)' can't be established.
        RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
        Are you sure you want to continue connecting (yes/no)? yes     在蓝色位置处输入 yes
        Warning: Permanently added 'github.com,207.97.227.239' (RSA) to the list of known hosts.
        PTY allocation request failed on channel 0
        Hi XXX(你在github中的昵称)! You've successfully authenticated, but GitHub does not provide shell access.
                Connection to github.com closed.
       
设置本地git个人信息:
     

至此,git和github的设置就完成了

6.是如何将本地代码push到github上,以及如何从github上pull代码了:
在github中创建Repository:
      https://github.com/ --> New Repository 输入Repository信息 projectName
在本地创建代码库:
      创建一个文件夹作为local repository
      $mkdir test
      创建一个文件
       $cd test
       $vi test.txt
       将文件添加至local repository
       $git add test.txt
       初始化local repository
       $git init
       commit文件
       $git commit -a
       定义远程服务器别名
       $git remote add alias git@github.com:xxxxx/projectName.git
       将本地数据push到github上
       $git push alias master
这样就可以将本地的代码push到github的repository中了

从github中pull代码:
      在github中搜到你想要pull的代码,如https://github.com/edgecase/ruby_koans
      选择fork,将此repository fock到你的repository下
      在本地创建local repository并初始化
      使用命令:
      $git pull git@github.com:xxxxx/ruby_koans.git
      将github上的代码pull到local repository中


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值