git连接github

配置本机git

git config --global user.name "username"
git config --global user.email youremail

生成密钥

ssh-keygen -t rsa -C "youremail"

将生成密钥.pub内容添加到github帐号。进入github帐号account setting,添加ssh key
检验是否连接上github

ssh git@github.com

Permission denied (publickey).解决办法
ssh-add ~/.ssh/git_id_rsa(ssh-add之前首先运行ssh-agent bash, 否则出现错误Could not open a connection to your authentication agent)

#create a new repository on the command line
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/username/project.git
git push -u origin master
#push an existing repository from the command line
git remote add origin https://github.com/username/project.git
git push -u origin master
git remote add origin git@github.com:username/project.git
git push origin master

查看提交地址

git remot -v

 
http://my.oschina.net/alvin404/blog/205745
http://www.cnblogs.com/plinx/archive/2013/04/08/3009159.html
http://blog.aboutc.net/linux/60/github-login-error
http://www.linuxidc.com/Linux/2011-04/35036.htm

Git错误non-fast-forward后的冲突解决

问题(Non-fast-forward)的出现原因在于:git仓库中已经有一部分代码,所以它不允许你直接把你的代码覆盖上去。于是你有2个选择方式:
1,强推,即利用强覆盖方式用你本地的代码替代git仓库内的内容
git push -f
2,先把git的东西fetch到你本地然后merge后再push
$ git fetch
$ git merge
这2句命令等价于

$ git pull

可是,这时候又出现了如下的问题:

上面出现的 [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~
解决github push错误The requested URL returned error: 403 Forbidden while accessing
解决方案:
vim .git/config
修改

[remote "origin"]
	url = https://github.com/wangz/example.git

为:

[remote "origin"]
	url = https://wangz@github.com/wangz/example.git

再次git push,弹出框输入密码,即可提交

Leave a Reply

Your email address will not be published. Required fields are marked *