MAC下GitHub命令操作

由于GitHub实在太有用了~~ ,各种源代码,开源工程,经常需要下载下来使用和学习,或者自己的代码需要上传之类的,尽管有“GitHub for Mac”工具,但是作为一名程序猿!!还是研究一下终端命令的用法!! 这样显得高端大气上档次! 哈哈!

首先: 创建GitHub账号:

 https://github.com/  ---> Pricing and Signup ---> Create a free account

创建SSH公钥:

打开终端

$cd ~/.ssh  //检查是否已经存在ssh

 如果存在,先将已有的ssh备份,或者将新建的ssh生成到另外的目录下

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

$ssh-keygen -t rsa -C xxxxx@xxxx.com(注册github时的email)
        Generating public/private rsa key pair.
        Enter file in which to save the key (/Users/twer/.ssh/id_rsa): /users/rdc-hankang/Desktop/某一个文件夹
(此处最好直接回车!!!有时候需要保存在/Users/twer/.ssh/目录下才可以,否则最后始终无法连接上GetHub官网,可能和使用黑苹果有关,使用MAC 的cp命令将.pub文件拷贝出来复制key粘贴于github官网,key和ssh关联的时候使用另一个,即没有.pub后缀的)
        
        Enter passphrase (empty for no passphrase): 创建一个密码
        Enter same passphrase again: 重复密码
        Your identification has been saved in /Users/twer/.ssh/id_rsa.
        Your public key has been saved in /Users/twer/.ssh/id_rsa.pub.
        The key fingerprint is:
        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxx@xxxx.com
        The key's randomart image is:
        +--[ RSA 2048]----+
        |.r.+-=+=         |
        |.rro.+. .       |
        |  ..* = A .      |
        |   o = + E       |
        |      . S o      |
        |           .     |
        |                 |
        |                 |
        |                 |
       +-----------------+
复制代码

在github中添加ssh

       登陆github,选择Account Settings-->SSH  Keys 添加ssh
       Title:xxxxx@xxxx.com //不知道这边有什么用,不确定是一个新邮箱还是注册gitHub的邮箱,感觉没什么关系
       Key:打开你生成的id_rsa.pub文件,将其中所有的内容拷贝至此,需要用文本开打。像下面这样
ssh-rsaAAAAB3NzaC1yc2EAAAADAQABAAABAQDAR+fdAAGdaRG8J1M+JpkjSOnMMyVee7Lq5BaABZpdCRaKgYWLlSLkEYjXZelXkdT2yZImb41fjqCvzZkzhzK0GnldY8xD5umCmkjOY2eF0kjR0y7ZLU/bLtt5tA6niVz49MKSBTgsjYVBcyv16oNcUa9sOKPjgv7EDIznWjvAOWJaH4eDbpoFAMz8wHWnOpOchJDI3WLZ7OFKW5/Wj5d0TVVt9KnU0ZCwZGRfPmVEQC1chKw8ePicNJtJrtyw0VlNgRoZev67aTvZICQx7uLOmNa78tNEfZSeyupByDbr6jEpIFLxZHmvbPM4TDZi/UnDf+Z1jQjYplkzjiyU2lr/ xxxxxxx@qq.com
 

测试账号与GitHub的链接情况:

      ssh -T git@github.com 此处是GitHub官网,如果出现如下提示,表示你连已经连上了.
Hi HaleyHan! You've successfully authenticated, but GitHub does not provide shell access.
 
接下来就可以管理你的代码。
 

远程建工程并与本地交互:

在github下建自己的Repository。Create a New Repository如下:

  • Repository name:通常就写自己自己要建的工程名。
  • Description:就是你对工程的描述了。
  • 选择Public。
  • 点击 “Create repository”,出现如下图:

常用命令及用法一览:

当电脑第一次使用(意思是输入一次,本电脑终身有用):
[objc] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. git init  //git 初始化 (进本地目录以后)  
[objc] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. git remote add origin url     //url : 新建工程的url  

提交:
[objc] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. git add .   //本目录下所有修改工程提交至本地仓库  
[objc] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. git commit -m "up"  //必须步骤,为更新做描述  
[objc] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. git push origin master  //将本地仓库更新至远程仓库  

添加新文件:
[objc] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. git add .     //添加本目录下所有新添加的文件  
[objc] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. git commit -m "up"  //描述添加文件  
[objc] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. git push origin master   //更新  
删除文件:
[objc] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. git add .   //添加本目录下所有新更新的文件  
[objc] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. git commit -m "de"    //描述文件  
[objc] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. git rm "文件"    //删除文件  
[objc] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. git push origin master //更新  
下载工程:
[objc] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. git clone url   //url 是远程url  
远程相对本地的更新:
[objc] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. git pull origin master  
 
以上命令基本够用了~~
 

过程中的BUG解决方法:

1 :如果输入$ Git remote add origin git@github.com:djqiang(github帐号名)/gitdemo(项目名).git 

    提示出错信息:fatal: remote origin already exists.

    解决办法如下:

    1、先输入$ git remote rm origin

    2、再输入$ git remote add origin git@github.com:djqiang/gitdemo.git 就不会报错了!

    3、如果输入$ git remote rm origin 还是报错的话,error: Could not remove config section 'remote.origin'. 我们需要修改gitconfig文件的内容

    4、找到你的github的安装路径,我的是C:UsersASUSAppDataLocalGitHubPortableGit_ca477551eeb4aea0e4ae9fcd3358bd96720bb5c8etc

    5、找到一个名为gitconfig的文件,打开它把里面的[remote "origin"]那一行删掉就好了!

  2 :如果输入$ ssh -T git@github.com

    出现错误提示:Permission denied (publickey).因为新生成的key不能加入ssh就会导致连接不上github

    解决办法如下:

    1、先输入$ ssh-agent,再输入$ ssh-add ~/.ssh/id_key,这样就可以了。

    2、如果还是不行的话,输入ssh-add ~/.ssh/id_key 命令后出现报错Could not open a connection to your authentication agent.解决方法是key用Git Gui的ssh工具生成,这样生成的时候key就直接保存在ssh中了,不需要再ssh-add命令加入了,其它的user,token等配置都用命令行来做。

    3、最好检查一下在你复制id_rsa.pub文件的内容时有没有产生多余的空格或空行,有些编辑器会帮你添加这些的。

 3 : 如果输入$ git push origin master

    提示出错信息:error:failed to push som refs to .......

    解决办法如下:

    1、先输入$ git pull origin master //先把远程服务器github上面的文件拉下来

    2、再输入$ git push origin master

    3、如果出现报错 fatal: Couldn't find remote ref master或者fatal: 'origin' does not appear to be a git repository以及fatal: Could not read from remote repository.

    4、则需要重新输入$ git remote add origingit@github.com:djqiang/gitdemo.git

在新建Xcode工程时,如果发现目录下有.svn和.git ,可选择性的删除:

//删除文件夹下的所有 .svn 文件

find . -name ".svn" | xargs rm -Rf

//删除文件夹下的所有 .git 文件
 
find . -name ".git" | xargs rm -Rf
 
-转载
原文地址:https://www.cnblogs.com/wenJiaQi/p/6091400.html