使用 Git@OSC 管理代码

本文详细介绍了如何在开源中国创建项目并通过SSH密钥验证方式将本地Git项目推送到git.oschina.net。主要内容包括SSH密钥的生成与配置、本地项目初始化及提交流程,以及解决push过程中遇到的non-fast-forward问题。

开源中国的 git 服务的地址是:http://git.oschina.net/

以下记录 push 本地已有的项目至 git@osc 的过程。

① 注册登录之后,创建一个自己的项目:

 

创建好的默认项目中包含一个 README.md 文件,用于项目的描述,使用 Markdown 编辑。

 

② 生成 ssh-key

在 linux 下生成 ssh 公钥:

[root@localhost ~]# ssh-keygen

公钥保存的位置是 ~/.ssh/id_rsa.pub

查看并复制公钥:

[root@localhost ~]# cat ~/.ssh/id_rsa.pub

把公钥的内容复制到 oschina 的 SSH 公钥中:

保存。

可以通过下面的命令查看公钥是否配置成功。

[root@localhost ~]# ssh -T git@git.oschina.net

输出:

 

③ 提交代码

进入本地项目,将目录转化为 Git 版本库

[root@localhost practise]# git init

 

添加项目中所有的 file 至版本库

[root@localhost practise]# git add .

 

提交:

[root@localhost practise]# git commit -m 'add practise files'

 

添加到远程版本库,也就是 git@osc

[root@localhost practise]# git remote add origin git@git.oschina.net:dee0912/practise.git
[root@localhost practise]# git push -u origin master

 

如果版本库中已有文件,在使用 push 命令的时候报错:

To git@git.oschina.net:dee0912/practise.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@git.oschina.net:dee0912/practise.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.  See the 'Note about
fast-forwards' section of 'git push --help' for details.

则先要把远程仓库中的文件 fetch 到本地然后再 merge,这两步相当于 git pull

[root@localhost practise]# git fetch
[root@localhost practise]# git merge

如果出现以下信息:

[root@localhost practise]# git merge
usage: git merge [options] <remote>...
   or: git merge [options] <msg> HEAD <remote>

则输入以下命令即可:

[root@localhost practise]# git config branch.master.remote origin
[root@localhost practise]# git config branch.master.merge refs/heads/master
[root@localhost practise]# git pull

说明:

1 当你处于 master branch,默认的 remote 就是 origin

2 当你在 master branch 上使用 git pull 时,没有指定 remote 和 branch,那么 git 就会采用默认的 remote(也就是origin)来 merge 在 master branch 上所有的改变

 

参考:

http://git.oschina.net/oschina/git-osc/wikis/home

http://git.oschina.net/wzw/git-quick-start

配置ssh公钥访问oschina

github上传时出现error: src refspec master does not match any解决办法

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

git pull 失败 ,提示:fatal: refusing to merge unrelated histories

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值