您的位置:首页 > 编程语言

mac下使用github 上传代码(转)

2016-03-09 14:34 429 查看
提起github相信大家都不会陌生,在这里就不再赘述了。作为开源代码库以及版本控制系统,使用好了确实会非常受益,再说的势利点,你找工作时给面试官说你经常维护自己的技术博客和github,相信你给他的印象会好很多。

windows底下如何使用github,很多好心人都已经给了非常详细的解决方案,你只需问下度娘,她会帮你解决,这里对于windows底下的使用我也就不再赘述了。这里主要讲讲如何在mac底下使用github,我刚开始使用时,还是费了一点功夫的,因为网上的资料比较杂,有些不是太准确。故将自己的安装过程比较详细的分享下,方便有需要的人,攒点人品。

工欲善其事必先利器,首先你得完成如下两个工作:

下载安装git客户端 http://code.google.com/p/git-osx-installer/downloads/list?can=3

注册github账号 https://github.com/ -->Pricing and Signup -->Create a free account

创建ssh:

接下来打开终端(不知道终端在哪儿的,就直接在spotlight里搜terminal):

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


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

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

$ssh-keygen -t rsa -C xxxxx@gmail.com(注册github时的email)
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/twer/.ssh/id_rsa):
Created directory '/Users/twer/.ssh'.
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:
18:16:11:c9:01:6c:48:09:7f:27:c6:43:0d:7f:3f:84 xxxxx@gmail.com
The key's randomart image is:
+--[ RSA 2048]----+
|.o.++===         |
|.ooo.+. .       |
|  ..* = E .      |
|   o = + o       |
|      . S o      |
|           .     |
|                 |
|                 |
|                 |
+-----------------+


如果要修改ssh生成目录,在粗体位置处输入要生成的路径,选择默认的话,会生成在 ~/.ssh下

在github中添加ssh(如图示)
登陆github,选择Account Settings-->SSH Keys 添加ssh
Title:xxxxx@gmail.com
Key:打开你生成的id_rsa.pub文件,将其中内容拷贝至此。



打开终端,先测试一下你的帐号跟github连上没有:ssh -T git@github.com 如果出现如下提示,表示你连已经连上了.(因为有了第一步,所以不用自己做过多的连接github的操作了,另外,下一次要连接github的时候记得打开第一步的工具).

Hi MiracleHe! You've successfully authenticated, but GitHub does not provide shell access.


接下来就可以上传你的代码了,在github下建自己的Repository。Create a New Repository如下:



Repository name:通常就写自己自己要建的工程名。

Description:就是你对工程的描述了。

选择Public。

点击 “Create repository”,出现如下图:



按照上面的步骤来就行了(注意记得在终端中输入时把目录切换到你要上传的工程目录下)。简单分析下上面的步骤:

touch README.md //新建一个记录提交操作的文档
git init //初始化本地仓库
git add README.md //添加
git commit -m "first commit"//提交到要地仓库,并写一些注释
git remote add origin git@github.com:youname/Test.git //连接远程仓库并建了一个名叫:origin的别名
git push -u origin master //将本地仓库的东西提交到地址是origin的地址,master分支下


需要注意的问题,进行如下操作时:

Key:打开你生成的id_rsa.pub文件,将其中内容拷贝至此


这里的id_rsa.pub文件是在.ssh目录下,可能有小伙伴会发现自己的mac下没看见.ssh目录,难道是我忽悠大家啦?当然不会啦,这么败人品的事我怎么可能会做。没看见.ssh目录,是因为mac把这个目录给隐藏了,如果你不信,你可以进行如下操作把隐藏文件给显示出来。

显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles YES

隐藏Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles NO

输完单击Enter键,退出终端,重新启动Finder就可以了


其实虽然你可能看不到.ssh目录,但你还是可以通过终端命令来查看到.ssh目录的,操作如下:



通过cat id_rsa.pub查看里面是一大串云里雾里的东西,不需要看懂,复制到“key”里就行了。

相信我已经讲的还比较清楚了,有不清楚的小伙伴可以留言。

疑难问题分析和解决:

如果输入$ 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:\Users\ASUS\AppData\Local\GitHub\PortableGit_ca477551eeb4aea0e4ae9fcd3358bd96720bb5c8\etc

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

如果输入$ 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文件的内容时有没有产生多余的空格或空行,有些编辑器会帮你添加这些的。

如果输入$ 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

使用git在本地创建一个项目的过程

$ makdir ~/hello-world    //创建一个项目hello-world
$ cd ~/hello-world       //打开这个项目
$ git init             //初始化
$ touch README
$ git add README        //更新README文件
$ git commit -m 'first commit'     //提交更新,并注释信息“first commit”
$ git remote add origin git@github.com:defnngj/hello-world.git     //连接远程github项目
$ git push -u origin master     //将本地项目更新到github项目上去


//删除文件夹下的所有 .svn 文件
find . -name ".svn" | xargs rm -Rf
//删除文件夹下的所有 .git 文件
find . -name ".git" | xargs rm -Rf
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: