注册github账号 https://github.com/
-->Pricing and Signup -->Create a
free account
创建ssh:
在local打开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):
//这个密码是push的时候需要输入的
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
Public Keys 添加ssh
Title:xxxxx@gmail.com
Key:打开你生成的id_rsa.pub文件,将其中vi其内容拷贝至此
测试SSH:
$ssh git@github.com
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
Warning: Permanently added
'github.com,207.97.227.239' (RSA) to the list of known hosts.
PTY allocation request failed on channel 0
Hi xianfuying! You've successfully
authenticated, but GitHub does not provide shell access.
Connection to github.com
closed.
在蓝色位置处输入 yes
设置本地git个人信息:
$git config --global user.name "your real
name"
$git config --global user.email
"xxxxx@gmail.com"
至此,git和github的设置就完成了,下面就是如何将本地代码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中
当要push代码到git时,出现提示:
error:failed to push some refs to ...
Dealing with “non-fast-forward” errors
From time to time you may encounter this error while
pushing:
-
$ git push origin master
-
To ../remote/
-
! [rejected] master -> master (non-fast forward)
-
error: failed to push some refs to '../remote/'
To
prevent you from losing history, non-fast-forward updates were
rejected
Merge
the remote changes before pushing again. See the
'non-fast forward'
section
of 'git push --help' for details.
This
error can be a bit overwhelming at first, do not fear. Simply put,
git cannot make the change on the remote without losing commits, so
it refuses the push. Usually this is caused by another user pushing
to the same branch. You can remedy this by fetching and merging the
remote branch, or using pull to perform both at
once.
In
other cases this error is a result of destructive changes made
locally by using commands like git commit --amend or git rebase.
While you can override the remote by adding --force to the push
command, you should only do so if you are absolutely certain this
is what you want to do. Force-pushes can cause issues for other
users that have fetched the remote branch, and is considered bad
practice. When in doubt, don’t force-push.
http://my.csdn.net/uploads/201204/19/1334803176_7047.png- 下git与github简单使用 (出错处理)" />
问题(Non-fast-forward)的出现原因在于:git仓库中已经有一部分代码,所以它不允许你直接把你的代码覆盖上去。于是你有2个选择方式:
1,强推,即利用强覆盖方式用你本地的代码替代git仓库内的内容
git push -f
2,先把git的东西fetch到你本地然后merge后再push
$ git fetch
$ git merge
这2句命令等价于
可是,这时候又出现了如下的问题:
http://my.csdn.net/uploads/201204/19/1334803661_1873.png- 下git与github简单使用 (出错处理)" />
上面出现的
[branch "master"]是需要明确(.git/config)如下的内容
[branch
"master"]
remote = origin
merge = refs/heads/master
这等于告诉git2件事:
1,当你处于master branch, 默认的remote就是origin。
2,当你在master
branch上使用git
pull时,没有指定remote和branch,那么git就会采用默认的remote(也就是origin)来merge在master
branch上所有的改变
如果不想或者不会编辑config文件的话,可以在bush上输入如下命令行:
-
$ git config branch.master.remote origin
-
$ git config branch.master.merge refs/heads/master
之后再重新git pull下。最后git push你的代码吧。it works now~
DIY Demo(以后用就这么几步)
mac:github $ git init
Initialized empty Git repository in
/Users/loveuu/github/.git/
mac:github $ git add Snapshots/
mac:github $ git commit -a
mac:github $ git remote add alias
git@github.com:braveryhui/MyXcodeSnapShot.git
mac:github $ git push alias master
假如那个repository是新建的里面没有内容就需要这么来push是第一次
touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:braveryhui/UItextViewMoveUp.git
git push -u origin master
假如git push的时候出现提示密码
那么就就需要重新执行这个的时候不输入密码
Enter passphrase (empty for no passphrase):
//这个密码是push的时候需要输入的
Enter same passphrase
again:
参考
http://apps.hi.baidu.com/share/detail/16068052
http://blog.csdn.net/banxi1988/article/details/6555293
http://apps.hi.baidu.com/share/detail/31697631
安装引用http://blog.163.com/xianfuying@126/blog/static/21960005201181482518631/
错误应用连接http://blog.csdn.net/chain2012/article/details/7476493