mac下为已有项目配置git,并提交到github

一、配置git

1.先退出xcode

2.打开终端

  2.1 cd到项目所在目录

  2.2  初始化  git init

  2.3  添加项目 git add ./

  2.4  提交  git commit -m "添加项目"

在添加项目过程中,可能也会遇到一些问题。

如2.3 git add ./可能出现

<pre name="code" class="plain">$ git add ./
warning: You ran 'git add' with neither '-A (--all)' or '--ignore-removal',
whose behaviour will change in Git 2.0 with respect to paths you removed.
Paths like 'inc/jquery2.1.1/jquery.min.js' that are
removed from your working tree are ignored with this version of Git.

* 'git add --ignore-removal <pathspec>', which is the current default,
  ignores paths you removed from your working tree.

* 'git add --all <pathspec>' will let you also record the removals.

Run 'git status' to check the paths you removed from your working tree.

 
解决方法很简单: 

$ git add -A  #或 git add -all
2.4  提交  git commit -m "添加项目",可能会出现如下错误:

$ git commit -a -m 'commit'
 
*** Please tell me who you are.
 
Run
 
  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"
 
to set your account's default identity.
Omit --global to set the identity only in this repository.
 
fatal: unable to auto-detect email address (got 'haipeng@whp.(none)')
解决方法:

按照错误提示添加自己的邮箱和name,随便填一个有效的邮箱即可:

git config --global user.email "you@example.com"
   git config --global user.name "Your Name"

二、将git项目提交到github

Github的理念源自于Git,但是与Git相比已经有了一些异化的功能特性(比如Pull Request),所以二者并非完全一致的。但是从版本控制方面来说,差别不大。所以利用Xcode内置的Git管理功能即可与Github连接,而无需额外安装Github客户端。

Github的仓库地址有两种方式:

一种是SSH连接方式,形式为:git@github.com:jingwanli6666/JWeibo.git

一种是HTTP形式,形式为:https://github.com:jingwanli6666/JWeibo.git

这两种仓库地址使用的认证方式也不一样。对于SSH连接,使用非对称公钥认证方法;对于HTTP连接,使用帐号密码认证方式。

2.1 使用SSH方式

# 检查本机是否已经存在ssh公钥

$ cd ~/.ssh

如果以前已经生成过ssh密钥对,那么就存在这个.ssh目录,目录下有id_rsa.pub公钥文件。如果还记得这个密钥对生成的细节(比如passphrase),就可以直接拿来用;否则就再多花费半分钟时间,生成新的密钥对(记得先备份旧的密钥文件,说不定其它什么项目或程序在使用):

#创建新的SSH密钥对

$ ssh-keygen -t rsa -C"your_email@example.com"

Enter passphrase (empty for no passphrase):[输入密码]

Enter same passphrase again: [再次输入密码]

到这里,SSH密钥对就生成了,接下来将id_rsa.pub文件用文本编辑器打开,将其中的全部字符串拷贝,并粘贴到web版的github.com中SSH公钥设置内。这样Xcode就可以利用SSH认证与github连接。先测试一下:

$ ssh -T git@github.com

本机就会向github发出一个连接请求,随后Mac OS会弹出一些安全认证和请求获取授权的提示框,要选择允许。如果github的服务器返回:

Hi username! You've successfullyauthenticated, but GitHub does not provide shell access.

这就说明本地的git已经能够成功与github服务器通信了。(不用担心上面的访问被拒绝的提示)。在确定能够与github服务器连接后,即可为JWeibo的本地git仓库添加远程地址:

打开命令行cd到JWeibo所在目录,在输入如下图红色框所示的命令:

git remote add origin https://github.com/jingwanli6666/JWeibo.git


(如果没有就先在github.com的仓库管理页面新建一个名为JWeibo的仓库)


Username与Password不用输入,关闭Orgnizer视图。在File –> SourceControl中即可将本地仓库的代码Push到github上了。

2.2 使用HTTP方式

2.2.1 打开命令行cd到JWeibo所在目录,在输入如下图红色框所示的命令:

git remote add origin https://github.com/jingwanli6666/JWeibo.git(如果没有就先在github.com的仓库管理页面新建一个名为JWeibo的仓库)(如2.1一样)。

2.2.2 再输入push项目命令

git push -u origin master

          输入github用户名和密码即可。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值